热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android图片Bitmap的剪切的示例代码

一、什么是Android中的Bitmap Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图

一、什么是Android中的Bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。

二、Bitmap的剪切基本操作

代码如下:

public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图

参数说明:

Bitmap source:要从中截图的原始位图

int x:起始x坐标

int y:起始y坐标

int width:要截的图的宽度

int height:要截的图的宽度

Bitmap.Config  config:一个枚举类型的配置,可以定义截到的新位图的质量

返回值:返回一个剪切好的Bitmap

三、Bitmap剪切的封装

实际使用中,因为项目需要时常需要对基本功能进行封装,下面是一段封装的代码,仅供参考。

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.RectF; 
 
public class BitmapCut 
{ 
 
 /** 
 * 通过资源id转化成Bitmap 
 * 
 * @param context 
 * @param resId 
 * @return 
 */ 
 public static Bitmap ReadBitmapById(Context context, int resId) 
 { 
 BitmapFactory.Options opt = new BitmapFactory.Options(); 
 opt.inPreferredCOnfig= Bitmap.Config.RGB_565; 
 opt.inPurgeable = true; 
 opt.inInputShareable = true; 
 
 InputStream is = context.getResources().openRawResource(resId); 
 return BitmapFactory.decodeStream(is, null, opt); 
 } 
 
 /** 
 * 设置背景为圆角 
 * 
 * @param bitmap 
 * @param pixels 
 * @return 
 */ 
 public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels) 
 { 
 int width = bitmap.getWidth(); 
 int height = bitmap.getHeight(); 
 
 Bitmap creBitmap = Bitmap.createBitmap(width, height, 
  android.graphics.Bitmap.Config.ARGB_8888); 
 Canvas canvas = new Canvas(creBitmap); 
 
 Paint paint = new Paint(); 
 float roundPx = pixels; 
 RectF rectF = new RectF(0, 0, bitmap.getWidth() - pixels, 
  bitmap.getHeight() - pixels); 
 paint.setAntiAlias(true); 
 
 canvas.drawARGB(0, 0, 0, 0); 
 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
 
 canvas.drawBitmap(bitmap, 0, 0, paint); 
 if (!bitmap.isRecycled()) 
  bitmap.recycle(); 
 
 return creBitmap; 
 } 
 
 /** 
 * 按正方形裁切图片 
 */ 
 public static Bitmap ImageCrop(Bitmap bitmap, boolean isRecycled) 
 { 
 
 if (bitmap == null) 
 { 
  return null; 
 } 
 
 int w = bitmap.getWidth(); // 得到图片的宽,高 
 int h = bitmap.getHeight(); 
 
 int wh = w > h ? h : w;// 裁切后所取的正方形区域边长 
 
 int retX = w > h ? (w - h) / 2 : 0;// 基于原图,取正方形左上角x坐标 
 int retY = w > h ? 0 : (h - w) / 2; 
 
 Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, 
  false); 
 if (isRecycled && bitmap != null && !bitmap.equals(bmp) 
  && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
  bitmap = null; 
 } 
 
 // 下面这句是关键 
 return bmp;// Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, 
   // false); 
 } 
 
 /** 
 * 按长方形裁切图片 
 * 
 * @param bitmap 
 * @return 
 */ 
 public static Bitmap ImageCropWithRect(Bitmap bitmap) 
 { 
 if (bitmap == null) 
 { 
  return null; 
 } 
 
 int w = bitmap.getWidth(); // 得到图片的宽,高 
 int h = bitmap.getHeight(); 
 
 int nw, nh, retX, retY; 
 if (w > h) 
 { 
  nw = h / 2; 
  nh = h; 
  retX = (w - nw) / 2; 
  retY = 0; 
 } else 
 { 
  nw = w / 2; 
  nh = w; 
  retX = w / 4; 
  retY = (h - w) / 2; 
 } 
 
 // 下面这句是关键 
 Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, 
  false); 
 if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
  bitmap = null; 
 } 
 return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, 
   // false); 
 } 
 
 /** 
 * Bitmap --> byte[] 
 * 
 * @param bmp 
 * @return 
 */ 
 public static byte[] readBitmap(Bitmap bmp) 
 { 
 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos); 
 try 
 { 
  baos.flush(); 
  baos.close(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 return baos.toByteArray(); 
 } 
 
 
 /** 
 * 将图像裁剪成圆形 
 * 
 * @param bitmap 
 * @return 
 */ 
 public static Bitmap toRoundBitmap(Bitmap bitmap) 
 { 
 if (bitmap == null) 
 { 
  return null; 
 } 
 
 int width = bitmap.getWidth(); 
 int height = bitmap.getHeight(); 
 float roundPx; 
 float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; 
 if (width <= height) 
 { 
  roundPx = width / 2; 
  top = 0; 
  bottom = width; 
  left = 0; 
  right = width; 
  height = width; 
  dst_left = 0; 
  dst_top = 0; 
  dst_right = width; 
  dst_bottom = width; 
 } else 
 { 
  roundPx = height / 2; 
  float clip = (width - height) / 2; 
  left = clip; 
  right = width - clip; 
  top = 0; 
  bottom = height; 
  width = height; 
  dst_left = 0; 
  dst_top = 0; 
  dst_right = height; 
  dst_bottom = height; 
 } 
 
 Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
 Canvas canvas = new Canvas(output); 
 
 final int color = 0xff424242; 
 final Paint paint = new Paint(); 
 final Rect src = new Rect((int) left, (int) top, (int) right, 
  (int) bottom); 
 final Rect dst = new Rect((int) dst_left, (int) dst_top, 
  (int) dst_right, (int) dst_bottom); 
 final RectF rectF = new RectF(dst); 
 
 paint.setAntiAlias(true); 
 
 canvas.drawARGB(0, 0, 0, 0); 
 paint.setColor(color); 
 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
 canvas.drawBitmap(bitmap, src, dst, paint); 
 if (bitmap != null && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
  bitmap = null; 
 } 
 return output; 
 } 
 
 // 将图片变成带圆边的圆形图片 
 public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height) 
 { 
 if (bitmap == null) 
 { 
  return null; 
 } 
 // 将图片变成圆角 
 Bitmap roundBitmap = Bitmap.createBitmap(width, height, 
  Config.ARGB_8888); 
 Canvas canvas = new Canvas(roundBitmap); 
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 int len = (width > height) &#63; height : width; 
 canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true); 
 canvas.drawBitmap(scaledBitmap, 0, 0, paint); 
 // 将图片加圆边 
 Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
 canvas = new Canvas(outBitmap); 
 paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 paint.setColor(0xffffffff); 
 canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER)); 
 canvas.drawBitmap(roundBitmap, 0, 0, paint); 
 bitmap.recycle(); 
 bitmap = null; 
 roundBitmap.recycle(); 
 roundBitmap = null; 
 scaledBitmap.recycle(); 
 scaledBitmap = null; 
 return outBitmap; 
 } 
 
 // 将图片变成带圆边的圆形图片 
 public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height, 
  int color) 
 { 
 if (bitmap == null) 
 { 
  return null; 
 } 
 // 将图片变成圆角 
 Bitmap roundBitmap = Bitmap.createBitmap(width, height, 
  Config.ARGB_8888); 
 Canvas canvas = new Canvas(roundBitmap); 
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 int len = (width > height) &#63; height : width; 
 canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true); 
 canvas.drawBitmap(scaledBitmap, 0, 0, paint); 
 // 将图片加圆边 
 Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
 canvas = new Canvas(outBitmap); 
 paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 paint.setColor(color); 
 canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER)); 
 canvas.drawBitmap(roundBitmap, 0, 0, paint); 
 bitmap.recycle(); 
 bitmap = null; 
 roundBitmap.recycle(); 
 roundBitmap = null; 
 scaledBitmap.recycle(); 
 scaledBitmap = null; 
 return outBitmap; 
 } 
 
 /** 
 * function:图片转圆角 
 * 
 * @param bitmap 
 *  需要转的bitmap 
 * @param pixels 
 *  转圆角的弧度 
 * @return 转圆角的bitmap 
 */ 
 public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) 
 { 
 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
  bitmap.getHeight(), Config.ARGB_8888); 
 Canvas canvas = new Canvas(output); 
 final int color = 0xff424242; 
 final Paint paint = new Paint(); 
 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
 final RectF rectF = new RectF(rect); 
 final float roundPx = pixels; 
 paint.setAntiAlias(true); 
 canvas.drawARGB(0, 0, 0, 0); 
 paint.setColor(color); 
 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
 canvas.drawBitmap(bitmap, rect, rect, paint); 
 if (bitmap != null && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
 } 
 return output; 
 } 
 
 /** 
 * 获取指定的圆角图片 
 * 
 * @param bitmap 
 * @return 
 */ 
 public static Bitmap getRadiusBitmap(Bitmap bitmap) 
 { 
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
 paint.setColor(0xffffffff); 
 Bitmap radiusBitmap = Bitmap.createBitmap(bitmap.getWidth(), 
  bitmap.getHeight(), Config.ARGB_8888); 
 Canvas canvas = new Canvas(radiusBitmap); 
 RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
 canvas.drawRoundRect(rectF, 7, 7, paint); 
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
 canvas.drawBitmap(bitmap, 0, 0, paint); 
 if (bitmap != null && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
 } 
 return radiusBitmap; 
 } 
 
 // 获得指定大小的圆边的bitmap数组 
 public static ArrayList getRadiusBitmapList(String[] pathArray, 
  int size, int len, float radius, int color) 
 { 
 Bitmap canvasBitmap = null; 
 Canvas canvas = null; 
 Paint paint = null; 
 RectF rectF = new RectF(0, 0, len - radius, len - radius); 
 File file = null; 
 FileInputStream fis = null; 
 Bitmap bitmap = null; 
 Bitmap scaledBitmap = null; 
 
 ArrayList list = new ArrayList(); 
 for (int i = 0; i  h) 
 { 
  if (h > w * num2 / num1) 
  { 
  nw = w; 
  nh = w * num2 / num1; 
  retX = 0; 
  retY = (h - nh) / 2; 
  } else 
  { 
  nw = h * num1 / num2; 
  nh = h; 
  retX = (w - nw) / 2; 
  retY = 0; 
  } 
 } else 
 { 
  if (w > h * num2 / num1) 
  { 
  nh = h; 
  nw = h * num2 / num1; 
  retY = 0; 
  retX = (w - nw) / 2; 
  } else 
  { 
  nh = w * num1 / num2; 
  nw = w; 
  retY = (h - nh) / 2; 
  retX = 0; 
  } 
 } 
 Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, 
  false); 
 if (isRecycled && bitmap != null && !bitmap.equals(bmp) 
  && !bitmap.isRecycled()) 
 { 
  bitmap.recycle(); 
  bitmap = null; 
 } 
 return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null, 
   // false); 
 } 
} 

示例代码:Bitmaptest_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
author-avatar
平凡无求
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有