Android 图片Bitmap的剪切

http://blog.csdn.net/stoppig/article/details/23202129


目录(?)[+]

一、什么是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剪切的封装

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

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8.   
  9. import android.content.Context;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.Bitmap.Config;  
  12. import android.graphics.BitmapFactory;  
  13. import android.graphics.Canvas;  
  14. import android.graphics.Paint;  
  15. import android.graphics.PorterDuff;  
  16. import android.graphics.PorterDuff.Mode;  
  17. import android.graphics.PorterDuffXfermode;  
  18. import android.graphics.Rect;  
  19. import android.graphics.RectF;  
  20.   
  21.   
  22. public class BitmapCut  
  23. {  
  24.   
  25.     /** 
  26.      * 通过资源id转化成Bitmap 
  27.      *  
  28.      * @param context 
  29.      * @param resId 
  30.      * @return 
  31.      */  
  32.     public static Bitmap ReadBitmapById(Context context, int resId)  
  33.     {  
  34.         BitmapFactory.Options opt = new BitmapFactory.Options();  
  35.         opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  36.         opt.inPurgeable = true;  
  37.         opt.inInputShareable = true;  
  38.   
  39.         InputStream is = context.getResources().openRawResource(resId);  
  40.         return BitmapFactory.decodeStream(is, null, opt);  
  41.     }  
  42.   
  43.       
  44.   
  45.     /** 
  46.      * 设置背景为圆角 
  47.      *  
  48.      * @param bitmap 
  49.      * @param pixels 
  50.      * @return 
  51.      */  
  52.     public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels)  
  53.     {  
  54.         int width = bitmap.getWidth();  
  55.         int height = bitmap.getHeight();  
  56.   
  57.         Bitmap creBitmap = Bitmap.createBitmap(width, height,  
  58.                 android.graphics.Bitmap.Config.ARGB_8888);  
  59.         Canvas canvas = new Canvas(creBitmap);  
  60.   
  61.         Paint paint = new Paint();  
  62.         float roundPx = pixels;  
  63.         RectF rectF = new RectF(00, bitmap.getWidth() - pixels,  
  64.                 bitmap.getHeight() - pixels);  
  65.         paint.setAntiAlias(true);  
  66.   
  67.         canvas.drawARGB(0000);  
  68.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  69.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  70.   
  71.         canvas.drawBitmap(bitmap, 00, paint);  
  72.         if (!bitmap.isRecycled())  
  73.             bitmap.recycle();  
  74.   
  75.         return creBitmap;  
  76.     }  
  77.   
  78.     /** 
  79.      * 按正方形裁切图片 
  80.      */  
  81.     public static Bitmap ImageCrop(Bitmap bitmap, boolean isRecycled)  
  82.     {  
  83.   
  84.         if (bitmap == null)  
  85.         {  
  86.             return null;  
  87.         }  
  88.   
  89.         int w = bitmap.getWidth(); // 得到图片的宽,高  
  90.         int h = bitmap.getHeight();  
  91.   
  92.         int wh = w > h ? h : w;// 裁切后所取的正方形区域边长  
  93.   
  94.         int retX = w > h ? (w - h) / 2 : 0;// 基于原图,取正方形左上角x坐标  
  95.         int retY = w > h ? 0 : (h - w) / 2;  
  96.   
  97.         Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null,  
  98.                 false);  
  99.         if (isRecycled && bitmap != null && !bitmap.equals(bmp)  
  100.                 && !bitmap.isRecycled())  
  101.         {  
  102.             bitmap.recycle();  
  103.             bitmap = null;  
  104.         }  
  105.   
  106.         // 下面这句是关键  
  107.         return bmp;// Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null,  
  108.                     // false);  
  109.     }  
  110.   
  111.     /** 
  112.      * 按长方形裁切图片 
  113.      *  
  114.      * @param bitmap 
  115.      * @return 
  116.      */  
  117.     public static Bitmap ImageCropWithRect(Bitmap bitmap)  
  118.     {  
  119.         if (bitmap == null)  
  120.         {  
  121.             return null;  
  122.         }  
  123.   
  124.         int w = bitmap.getWidth(); // 得到图片的宽,高  
  125.         int h = bitmap.getHeight();  
  126.   
  127.         int nw, nh, retX, retY;  
  128.         if (w > h)  
  129.         {  
  130.             nw = h / 2;  
  131.             nh = h;  
  132.             retX = (w - nw) / 2;  
  133.             retY = 0;  
  134.         } else  
  135.         {  
  136.             nw = w / 2;  
  137.             nh = w;  
  138.             retX = w / 4;  
  139.             retY = (h - w) / 2;  
  140.         }  
  141.   
  142.         // 下面这句是关键  
  143.         Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,  
  144.                 false);  
  145.         if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled())  
  146.         {  
  147.             bitmap.recycle();  
  148.             bitmap = null;  
  149.         }  
  150.         return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,  
  151.                     // false);  
  152.     }  
  153.   
  154.     /** 
  155.      * Bitmap --> byte[] 
  156.      *  
  157.      * @param bmp 
  158.      * @return 
  159.      */  
  160.     public static byte[] readBitmap(Bitmap bmp)  
  161.     {  
  162.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  163.         bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);  
  164.         try  
  165.         {  
  166.             baos.flush();  
  167.             baos.close();  
  168.         } catch (IOException e)  
  169.         {  
  170.             e.printStackTrace();  
  171.         }  
  172.         return baos.toByteArray();  
  173.     }  
  174.   
  175.       
  176.     /** 
  177.      * 将图像裁剪成圆形 
  178.      *  
  179.      * @param bitmap 
  180.      * @return 
  181.      */  
  182.     public static Bitmap toRoundBitmap(Bitmap bitmap)  
  183.     {  
  184.         if (bitmap == null)  
  185.         {  
  186.             return null;  
  187.         }  
  188.   
  189.         int width = bitmap.getWidth();  
  190.         int height = bitmap.getHeight();  
  191.         float roundPx;  
  192.         float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;  
  193.         if (width <= height)  
  194.         {  
  195.             roundPx = width / 2;  
  196.             top = 0;  
  197.             bottom = width;  
  198.             left = 0;  
  199.             right = width;  
  200.             height = width;  
  201.             dst_left = 0;  
  202.             dst_top = 0;  
  203.             dst_right = width;  
  204.             dst_bottom = width;  
  205.         } else  
  206.         {  
  207.             roundPx = height / 2;  
  208.             float clip = (width - height) / 2;  
  209.             left = clip;  
  210.             right = width - clip;  
  211.             top = 0;  
  212.             bottom = height;  
  213.             width = height;  
  214.             dst_left = 0;  
  215.             dst_top = 0;  
  216.             dst_right = height;  
  217.             dst_bottom = height;  
  218.         }  
  219.   
  220.         Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  221.         Canvas canvas = new Canvas(output);  
  222.   
  223.         final int color = 0xff424242;  
  224.         final Paint paint = new Paint();  
  225.         final Rect src = new Rect((int) left, (int) top, (int) right,  
  226.                 (int) bottom);  
  227.         final Rect dst = new Rect((int) dst_left, (int) dst_top,  
  228.                 (int) dst_right, (int) dst_bottom);  
  229.         final RectF rectF = new RectF(dst);  
  230.   
  231.         paint.setAntiAlias(true);  
  232.   
  233.         canvas.drawARGB(0000);  
  234.         paint.setColor(color);  
  235.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  236.   
  237.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  238.         canvas.drawBitmap(bitmap, src, dst, paint);  
  239.         if (bitmap != null && !bitmap.isRecycled())  
  240.         {  
  241.             bitmap.recycle();  
  242.             bitmap = null;  
  243.         }  
  244.         return output;  
  245.     }  
  246.   
  247.     // 将图片变成带圆边的圆形图片  
  248.     public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height)  
  249.     {  
  250.         if (bitmap == null)  
  251.         {  
  252.             return null;  
  253.         }  
  254.         // 将图片变成圆角  
  255.         Bitmap roundBitmap = Bitmap.createBitmap(width, height,  
  256.                 Config.ARGB_8888);  
  257.         Canvas canvas = new Canvas(roundBitmap);  
  258.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  259.         int len = (width > height) ? height : width;  
  260.         canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);  
  261.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  262.         Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);  
  263.         canvas.drawBitmap(scaledBitmap, 00, paint);  
  264.         // 将图片加圆边  
  265.         Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  266.         canvas = new Canvas(outBitmap);  
  267.         paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  268.         paint.setColor(0xffffffff);  
  269.         canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);  
  270.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));  
  271.         canvas.drawBitmap(roundBitmap, 00, paint);  
  272.         bitmap.recycle();  
  273.         bitmap = null;  
  274.         roundBitmap.recycle();  
  275.         roundBitmap = null;  
  276.         scaledBitmap.recycle();  
  277.         scaledBitmap = null;  
  278.         return outBitmap;  
  279.     }  
  280.   
  281.     // 将图片变成带圆边的圆形图片  
  282.     public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height,  
  283.             int color)  
  284.     {  
  285.         if (bitmap == null)  
  286.         {  
  287.             return null;  
  288.         }  
  289.         // 将图片变成圆角  
  290.         Bitmap roundBitmap = Bitmap.createBitmap(width, height,  
  291.                 Config.ARGB_8888);  
  292.         Canvas canvas = new Canvas(roundBitmap);  
  293.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  294.         int len = (width > height) ? height : width;  
  295.         canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);  
  296.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  297.         Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);  
  298.         canvas.drawBitmap(scaledBitmap, 00, paint);  
  299.         // 将图片加圆边  
  300.         Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  301.         canvas = new Canvas(outBitmap);  
  302.         paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  303.         paint.setColor(color);  
  304.         canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);  
  305.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));  
  306.         canvas.drawBitmap(roundBitmap, 00, paint);  
  307.         bitmap.recycle();  
  308.         bitmap = null;  
  309.         roundBitmap.recycle();  
  310.         roundBitmap = null;  
  311.         scaledBitmap.recycle();  
  312.         scaledBitmap = null;  
  313.         return outBitmap;  
  314.     }  
  315.   
  316.     /** 
  317.      * function:图片转圆角 
  318.      *  
  319.      * @param bitmap 
  320.      *            需要转的bitmap 
  321.      * @param pixels 
  322.      *            转圆角的弧度 
  323.      * @return 转圆角的bitmap 
  324.      */  
  325.     public static Bitmap toRoundCorner(Bitmap bitmap, int pixels)  
  326.     {  
  327.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
  328.                 bitmap.getHeight(), Config.ARGB_8888);  
  329.         Canvas canvas = new Canvas(output);  
  330.         final int color = 0xff424242;  
  331.         final Paint paint = new Paint();  
  332.         final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  333.         final RectF rectF = new RectF(rect);  
  334.         final float roundPx = pixels;  
  335.         paint.setAntiAlias(true);  
  336.         canvas.drawARGB(0000);  
  337.         paint.setColor(color);  
  338.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  339.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  340.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  341.         if (bitmap != null && !bitmap.isRecycled())  
  342.         {  
  343.             bitmap.recycle();  
  344.         }  
  345.         return output;  
  346.     }  
  347.   
  348.     /** 
  349.      * 获取指定的圆角图片 
  350.      *  
  351.      * @param bitmap 
  352.      * @return 
  353.      */  
  354.     public static Bitmap getRadiusBitmap(Bitmap bitmap)  
  355.     {  
  356.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  357.         paint.setColor(0xffffffff);  
  358.         Bitmap radiusBitmap = Bitmap.createBitmap(bitmap.getWidth(),  
  359.                 bitmap.getHeight(), Config.ARGB_8888);  
  360.         Canvas canvas = new Canvas(radiusBitmap);  
  361.         RectF rectF = new RectF(00, bitmap.getWidth(), bitmap.getHeight());  
  362.         canvas.drawRoundRect(rectF, 77, paint);  
  363.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  364.         canvas.drawBitmap(bitmap, 00, paint);  
  365.         if (bitmap != null && !bitmap.isRecycled())  
  366.         {  
  367.             bitmap.recycle();  
  368.         }  
  369.         return radiusBitmap;  
  370.     }  
  371.   
  372.     // 获得指定大小的圆边的bitmap数组  
  373.     public static ArrayList<Bitmap> getRadiusBitmapList(String[] pathArray,  
  374.             int size, int len, float radius, int color)  
  375.     {  
  376.         Bitmap canvasBitmap = null;  
  377.         Canvas canvas = null;  
  378.         Paint paint = null;  
  379.         RectF rectF = new RectF(00, len - radius, len - radius);  
  380.         File file = null;  
  381.         FileInputStream fis = null;  
  382.         Bitmap bitmap = null;  
  383.         Bitmap scaledBitmap = null;  
  384.   
  385.         ArrayList<Bitmap> list = new ArrayList<Bitmap>();  
  386.         for (int i = 0; i < pathArray.length; i++)  
  387.         {  
  388.             file = new File(pathArray[i]);  
  389.             if (!file.exists())  
  390.                 continue;  
  391.             try  
  392.             {  
  393.                 fis = new FileInputStream(file);  
  394.                 bitmap = BitmapFactory.decodeStream(fis);  
  395.                 if (bitmap != null)  
  396.                 {  
  397.                     canvasBitmap = Bitmap.createBitmap(len, len,  
  398.                             Config.ARGB_8888);  
  399.                     canvas = new Canvas(canvasBitmap);  
  400.                     paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  401.                     paint.setColor(color);  
  402.                     canvas.drawRoundRect(rectF, radius, radius, paint);  
  403.                     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  404.   
  405.                     scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len,  
  406.                             true);  
  407.                     canvas.drawBitmap(scaledBitmap, 00, paint);  
  408.                     list.add(canvasBitmap);  
  409.                 }  
  410.             } catch (FileNotFoundException e)  
  411.             {  
  412.             } finally  
  413.             {  
  414.                 if (fis != null)  
  415.                 {  
  416.                     try  
  417.                     {  
  418.                         fis.close();  
  419.                     } catch (IOException e1)  
  420.                     {  
  421.                     }  
  422.                 }  
  423.             }  
  424.             if (list.size() == size)  
  425.                 break;  
  426.         }  
  427.         if (scaledBitmap != null && !scaledBitmap.isRecycled())  
  428.         {  
  429.             scaledBitmap.recycle();  
  430.             scaledBitmap = null;  
  431.         }  
  432.         if (bitmap != null && !bitmap.isRecycled())  
  433.         {  
  434.             bitmap.recycle();  
  435.             bitmap = null;  
  436.         }  
  437.         return list;  
  438.     }  
  439.   
  440.       
  441.   
  442.       
  443.   
  444.     /** 
  445.      * 按照一定的宽高比例裁剪图片 
  446.      *  
  447.      * @param bitmap 
  448.      * @param num1 
  449.      *            长边的比例 
  450.      * @param num2 
  451.      *            短边的比例 
  452.      * @return 
  453.      */  
  454.     public static Bitmap ImageCrop(Bitmap bitmap, int num1, int num2,  
  455.             boolean isRecycled)  
  456.     {  
  457.         if (bitmap == null)  
  458.         {  
  459.             return null;  
  460.         }  
  461.         int w = bitmap.getWidth(); // 得到图片的宽,高  
  462.         int h = bitmap.getHeight();  
  463.         int retX, retY;  
  464.         int nw, nh;  
  465.         if (w > h)  
  466.         {  
  467.             if (h > w * num2 / num1)  
  468.             {  
  469.                 nw = w;  
  470.                 nh = w * num2 / num1;  
  471.                 retX = 0;  
  472.                 retY = (h - nh) / 2;  
  473.             } else  
  474.             {  
  475.                 nw = h * num1 / num2;  
  476.                 nh = h;  
  477.                 retX = (w - nw) / 2;  
  478.                 retY = 0;  
  479.             }  
  480.         } else  
  481.         {  
  482.             if (w > h * num2 / num1)  
  483.             {  
  484.                 nh = h;  
  485.                 nw = h * num2 / num1;  
  486.                 retY = 0;  
  487.                 retX = (w - nw) / 2;  
  488.             } else  
  489.             {  
  490.                 nh = w * num1 / num2;  
  491.                 nw = w;  
  492.                 retY = (h - nh) / 2;  
  493.                 retX = 0;  
  494.             }  
  495.         }  
  496.         Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,  
  497.                 false);  
  498.         if (isRecycled && bitmap != null && !bitmap.equals(bmp)  
  499.                 && !bitmap.isRecycled())  
  500.         {  
  501.             bitmap.recycle();  
  502.             bitmap = null;  
  503.         }  
  504.         return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,  
  505.                     // false);  
  506.     }  
  507. }  

示例代码:http://download.csdn.net/detail/stop_pig/7162695

0
0
 
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值