Android中常用的bitmap处理方法 (bitmap工具类)

2024-06-09 23:18

本文主要是介绍Android中常用的bitmap处理方法 (bitmap工具类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

收集了很多bitmap相关的处理方法,几乎全部应用在项目中,所以特记录下!

[java]  view plain copy
  1. package com.tmacsky.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.content.Context;  
  7. import android.content.res.Resources;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.Matrix;  
  12. import android.graphics.Paint;  
  13. import android.graphics.PixelFormat;  
  14. import android.graphics.PorterDuffXfermode;  
  15. import android.graphics.Rect;  
  16. import android.graphics.RectF;  
  17. import android.graphics.Bitmap.Config;  
  18. import android.graphics.PorterDuff.Mode;  
  19. import android.graphics.drawable.BitmapDrawable;  
  20. import android.graphics.drawable.Drawable;  
  21. import android.view.View;  
  22. import android.view.View.MeasureSpec;  
  23.   
  24. public class ImageUtils {  
  25.   
  26.     //--->bitmap相关  
  27.     //参考网站http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-drawable.html  
  28.     // 见博客:http://blog.sina.com.cn/s/blog_afb547c60101j7qn.html  
  29.     /** 
  30.      * View转成bitmap 
  31.      * @param view 
  32.      * @return 
  33.      */  
  34.     public static Bitmap convertViewToBitmap(View view) {  
  35.         view.setDrawingCacheEnabled(true);  
  36.         view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  37.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  38.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  39.         view.buildDrawingCache();  
  40.         return view.getDrawingCache();  
  41.     }  
  42.     /** 
  43.      * 缩放Drawable 
  44.      * @param drawable 
  45.      * @param w  缩放后需要的宽度 
  46.      * @param h  缩放后需要的高度 
  47.      * @return 
  48.      */  
  49.     public static Drawable zoomDrawable(Drawable drawable, int w, int h) {  
  50.         int width = drawable.getIntrinsicWidth();  
  51.         int height = drawable.getIntrinsicHeight();  
  52.         // drawable转换成bitmap  
  53.         Bitmap oldbmp = drawableToBitmap(drawable);  
  54.         // 创建操作图片用的Matrix对象  
  55.         Matrix matrix = new Matrix();  
  56.         // 计算缩放比例  
  57.         float sx = ((float) w / width);  
  58.         float sy = ((float) h / height);  
  59.         // 设置缩放比例  
  60.         matrix.postScale(sx, sy);  
  61.         // 建立新的bitmap,其内容是对原bitmap的缩放后的图  
  62.         Bitmap newbmp = Bitmap.createBitmap(oldbmp, 00, width, height,  
  63.                 matrix, true);  
  64.         return new BitmapDrawable(newbmp);  
  65.     }  
  66.       
  67.     /** 
  68.      * 缩放bitmap 
  69.      * @param oldBitmap 输入bitmap 
  70.      * @param newWidth  
  71.      * @param newHeight 
  72.      * @return 
  73.      */  
  74.     public static Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {  
  75.         // 获得图片的宽高  
  76.         int width = oldBitmap.getWidth();  
  77.         int height = oldBitmap.getHeight();  
  78.         // 计算缩放比例  
  79.         float scaleWidth = ((float) newWidth) / width;  
  80.         float scaleHeight = ((float) newHeight) / height;  
  81.         // 取得想要缩放的matrix参数  
  82.         Matrix matrix = new Matrix();  
  83.         matrix.postScale(scaleWidth, scaleHeight);  
  84.         // 得到新的图片  
  85.         Bitmap newbm = Bitmap.createBitmap(oldBitmap, 00, width, height, matrix,  
  86.                 true);  
  87.         return newbm;  
  88.     }  
  89.     /** 
  90.      * 缩放网络图片 依赖于zoomBitmap 
  91.      * @param img 
  92.      * @param newWidth 
  93.      * @param newHeight 
  94.      * @return 
  95.      */  
  96.     public static Bitmap zoomImg(String img, int newWidth, int newHeight) {  
  97.         // 图片源  
  98.         Bitmap bm = BitmapFactory.decodeFile(img);  
  99.         if (null != bm) {  
  100.             return zoomBitmap(bm, newWidth, newHeight);  
  101.         }  
  102.         return null;  
  103.     }  
  104.     /** 
  105.      * 缩放网络图片 依赖于zoomBitmap 
  106.      * @param context 
  107.      * @param img 
  108.      * @param newWidth 
  109.      * @param newHeight 
  110.      * @return 
  111.      */  
  112.     public static Bitmap zoomImg(Context context, String img, int newWidth,  
  113.             int newHeight) {  
  114.         // 图片源  
  115.         try {  
  116.             Bitmap bm = BitmapFactory.decodeStream(context.getAssets()  
  117.                     .open(img));  
  118.             if (null != bm) {  
  119.                 return zoomBitmap(bm, newWidth, newHeight);  
  120.             }  
  121.         } catch (IOException e) {  
  122.             // TODO Auto-generated catch block  
  123.             e.printStackTrace();  
  124.         }  
  125.         return null;  
  126.     }  
  127.     /** 
  128.      * 判断bitmap是否存在 
  129.      * @param bitmap 
  130.      * @return 
  131.      */  
  132.     public static boolean bitmapAvailable(Bitmap bitmap) {  
  133.         return bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0;  
  134.     }  
  135.     /** 
  136.      * drawable 转成bitmap 
  137.      * @param drawable 
  138.      * @return 
  139.      */  
  140.     public static Bitmap drawableToBitmap(Drawable drawable) {  
  141.         // 取 drawable 的长宽  
  142.         int w = drawable.getIntrinsicWidth();  
  143.         int h = drawable.getIntrinsicHeight();  
  144.         // 取 drawable 的颜色格式  
  145.         Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  146.                 : Bitmap.Config.RGB_565;  
  147.         // 建立对应 bitmap  
  148.         Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
  149.         // 建立对应 bitmap 的画布  
  150.         Canvas canvas = new Canvas(bitmap);  
  151.         drawable.setBounds(00, w, h);  
  152.         // 把 drawable 内容画到画布中  
  153.         drawable.draw(canvas);  
  154.         return bitmap;  
  155.     }  
  156.     /** 
  157.      * Bitmap转换成Drawable 
  158.      * @param context 
  159.      * @param bitmap 
  160.      * @return 
  161.      */  
  162.     public static Drawable bitmapToDrawable(Context context,Bitmap bitmap){  
  163.         //因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。  
  164.         BitmapDrawable bd= new BitmapDrawable(context.getResources(), bitmap);  
  165.         return bd;  
  166.     }  
  167.       
  168.     /** 
  169.      * 从资源中获取Bitmap 
  170.      * @param context 
  171.      * @param req  R.drawable.icon(eg.) 
  172.      * @return 
  173.      */  
  174.     public Bitmap getBitmapFromResources(Context context,int req){  
  175.           Resources res = context.getResources();  
  176.           Bitmap bmp = BitmapFactory.decodeResource(res, req);  
  177.           return bmp;  
  178.     }  
  179.       
  180.     /** 
  181.      * Byte[] -> Bitmap的转换 
  182.      */  
  183.     public Bitmap Bytes2Bimap(byte[] b) {  
  184.         if (b.length != 0) {  
  185.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  186.         } else {  
  187.             return null;  
  188.         }  
  189.     }  
  190.     /** 
  191.      * Bitmap->Byte[]的转换 
  192.      * @param bm 
  193.      * @return 
  194.      */  
  195.      public byte[] Bitmap2Bytes(Bitmap bm) {  
  196.          ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  197.          bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  198.          return baos.toByteArray();  
  199.     }  
  200.     /** 
  201.      * 获取圆角图片 
  202.      * @param bitmap 
  203.      * @param roundPx 圆角的弧度 
  204.      * @return 
  205.      */  
  206.     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {  
  207.         int w = bitmap.getWidth();  
  208.         int h = bitmap.getHeight();  
  209.         Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
  210.         Canvas canvas = new Canvas(output);  
  211.         final int color = 0xff424242;  
  212.         final Paint paint = new Paint();  
  213.         final Rect rect = new Rect(00, w, h);  
  214.         final RectF rectF = new RectF(rect);  
  215.         paint.setAntiAlias(true);  
  216.         canvas.drawARGB(0000);  
  217.         paint.setColor(color);  
  218.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  219.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  220.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  221.         return output;  
  222.     }  
  223. }  

转自:http://blog.csdn.net/tmacsky/article/details/38121283


这篇关于Android中常用的bitmap处理方法 (bitmap工具类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1046579

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum