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

Android 图片Bitmap的剪切的示例代码,第1张

概述一、什么是Android中的BitmapBitmap是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,bitmap.getWIDth() - pixels,bitmap.getHeight() - pixels);  paint.setAntiAlias(true);   canvas.drawARGB(0,0);  canvas.drawRoundRect(rectF,roundPx,paint);  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));   canvas.drawBitmap(bitmap,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,false);  if (isRecycled && bitmap != null && !bitmap.equals(bmp)   && !bitmap.isRecycled())  {   bitmap.recycle();   bitmap = null;  }   // 下面这句是关键  return bmp;// Bitmap.createBitmap(bitmap,// 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,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,nw,false);  if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled())  {   bitmap.recycle();   bitmap = null;  }  return bmp;// Bitmap.createBitmap(bitmap,// 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,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);  paint.setcolor(color);  canvas.drawRoundRect(rectF,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 height)  {  if (bitmap == null)  {   return null;  }  // 将图片变成圆角  Bitmap roundBitmap = Bitmap.createBitmap(wIDth,Config.ARGB_8888);  Canvas canvas = new Canvas(roundBitmap);  Paint paint = new Paint(Paint.ANTI_AliAS_FLAG);  int len = (wIDth > height) ? 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,true);  canvas.drawBitmap(scaledBitmap,paint);  // 将图片加圆边  Bitmap outBitmap = Bitmap.createBitmap(wIDth,Config.ARGB_8888);  canvas = new Canvas(outBitmap);  paint = new Paint(Paint.ANTI_AliAS_FLAG);  paint.setcolor(0xffffffff);  canvas.drawCircle(wIDth / 2,len / 2 - 4,paint);  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));  canvas.drawBitmap(roundBitmap,paint);  bitmap.recycle();  bitmap = null;  roundBitmap.recycle();  roundBitmap = null;  scaledBitmap.recycle();  scaledBitmap = null;  return outBitmap;  }   // 将图片变成带圆边的圆形图片  public static Bitmap getRoundBitmap(Bitmap bitmap,int color)  {  if (bitmap == null)  {   return null;  }  // 将图片变成圆角  Bitmap roundBitmap = Bitmap.createBitmap(wIDth,Config.ARGB_8888);  canvas = new Canvas(outBitmap);  paint = new Paint(Paint.ANTI_AliAS_FLAG);  paint.setcolor(color);  canvas.drawCircle(wIDth / 2,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,bitmap.getWIDth(),bitmap.getHeight());  final RectF rectF = new RectF(rect);  final float roundPx = pixels;  paint.setAntiAlias(true);  canvas.drawARGB(0,paint);  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  canvas.drawBitmap(bitmap,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(),Config.ARGB_8888);  Canvas canvas = new Canvas(radiusBitmap);  RectF rectF = new RectF(0,bitmap.getHeight());  canvas.drawRoundRect(rectF,7,paint);  if (bitmap != null && !bitmap.isRecycled())  {   bitmap.recycle();  }  return radiusBitmap;  }   // 获得指定大小的圆边的bitmap数组  public static ArrayList<Bitmap> 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,len - radius,len - radius);  file file = null;  fileinputStream fis = null;  Bitmap bitmap = null;  Bitmap scaledBitmap = null;   ArrayList<Bitmap> List = new ArrayList<Bitmap>();  for (int i = 0; i < pathArray.length; i++)  {   file = new file(pathArray[i]);   if (!file.exists())   continue;   try   {   fis = new fileinputStream(file);   bitmap = BitmapFactory.decodeStream(fis);   if (bitmap != null)   {    canvasBitmap = Bitmap.createBitmap(len,Config.ARGB_8888);    canvas = new Canvas(canvasBitmap);    paint = new Paint(Paint.ANTI_AliAS_FLAG);    paint.setcolor(color);    canvas.drawRoundRect(rectF,radius,paint);    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));     scaledBitmap = Bitmap.createScaledBitmap(bitmap,true);    canvas.drawBitmap(scaledBitmap,paint);    List.add(canvasBitmap);   }   } catch (fileNotFoundException e)   {   } finally   {   if (fis != null)   {    try    {    fis.close();    } catch (IOException e1)    {    }   }   }   if (List.size() == size)   break;  }  if (scaledBitmap != null && !scaledBitmap.isRecycled())  {   scaledBitmap.recycle();   scaledBitmap = null;  }  if (bitmap != null && !bitmap.isRecycled())  {   bitmap.recycle();   bitmap = null;  }  return List;  }   /**  * 按照一定的宽高比例裁剪图片  *  * @param bitmap  * @param num1  *  长边的比例  * @param num2  *  短边的比例  * @return  */  public static Bitmap ImageCrop(Bitmap bitmap,int num1,int num2,boolean isRecycled)  {  if (bitmap == null)  {   return null;  }  int w = bitmap.getWIDth(); // 得到图片的宽,高  int h = bitmap.getHeight();  int retX,retY;  int nw,nh;  if (w > 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,false);  if (isRecycled && bitmap != null && !bitmap.equals(bmp)   && !bitmap.isRecycled())  {   bitmap.recycle();   bitmap = null;  }  return bmp;// Bitmap.createBitmap(bitmap,// false);  } } 

示例代码:Bitmaptest_jb51.rar

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

总结

以上是内存溢出为你收集整理的Android 图片Bitmap的剪切的示例代码全部内容,希望文章能够帮你解决Android 图片Bitmap的剪切的示例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1143165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存