Android 图片转化工具类BitmapUtil
发布日期:2021-06-30 22:34:35
浏览次数:2
分类:技术文章
本文共 5390 字,大约阅读时间需要 17 分钟。
/** * 图片转化工具 * @author chen.lin * */public class BitmapUtils { private static Map> imageCache = new HashMap >(); public static void addBitmapToCache(String path) { // 强引用的Bitmap对象 Bitmap bitmap = BitmapFactory.decodeFile(path); // 软引用的Bitmap对象 SoftReference softBitmap = new SoftReference (bitmap); // 添加该对象到Map中使其缓存 imageCache.put(path, softBitmap); } public static Bitmap getBitmapByPath(String path) { // 从缓存中取软引用的Bitmap对象 SoftReference softBitmap = imageCache.get(path); // 判断是否存在软引用 if (softBitmap == null) { return null; } // 取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空 Bitmap bitmap = softBitmap.get(); return bitmap; } /** * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } public static String getBase64(byte[] image) { String string = ""; try { BASE64Encoder encoder = new BASE64Encoder(); string = encoder.encodeBuffer(image).trim(); } catch (Exception e) { e.printStackTrace(); } return string; } /** * @param R_DRAWABLE_ID * @return */ @SuppressWarnings("deprecation") public static Drawable getDrawable(byte[] imgByte) { Bitmap bitmap; if (imgByte != null) { bitmap = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); Drawable drawable = new BitmapDrawable(bitmap); return drawable; } return null; } /** * 閫氳繃bitmap 鑾峰彇浜岃繘鍒舵暟鎹? * @param image * @return */ public static byte[] getDefaultIcon(Bitmap image) { // iconView.get // BitmapFactory. byte[] compressData = null; if (image != null) { compressData = getByteByBitmap(image); } return compressData; } private static byte[] getByteByBitmap(Bitmap bmp) { Bitmap output = Bitmap.createScaledBitmap(bmp, 150, bmp.getHeight() * 150 / bmp.getWidth(), true); // 涓?畾瑕佸垽鏂紝濡傛灉鍥剧墖鍘嬬缉鍓嶅悗width,height涓嶅彉锛? // 寮曠敤鍚屼竴涓璞★紝绯荤粺浼氭姤 // Canvas: trying to use a recycled bitmap android.graphics.Bitmap閿欒 if (bmp != output) { bmp.recycle(); bmp = null; } byte[] compressData = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { try { output.compress(Bitmap.CompressFormat.PNG, 100, baos); } catch (Exception e) { e.printStackTrace(); } compressData = baos.toByteArray(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return compressData; } /** * @param bitmap * @param _width * @param _height * @return */ public static Bitmap setBitmapSize(Bitmap bitmap, int _width, int _height) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = _width; int newHeight = _height; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } /** * @param bitmap * @param _width * @param _height * @return */ public static Bitmap setBitmapSize(String bitmapPath, int _width, int _height) { Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath); if (bitmap == null) { Logger.i("bitmap", "bitmap------------>发生未知异常!"); return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = _width; int newHeight = _height; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } /** * @param bitmap * @return */ public static Bitmap setBitmapSize(String bitmapPath) { Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath); if (bitmap == null) { Logger.i("bitmap", "bitmap------------>发生未知异常!"); return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = width; int newHeight = height; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); }}
转载地址:https://lovoo.blog.csdn.net/article/details/51204283 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月09日 18时57分02秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python 之 Scatter散点图
2019-04-30
Python实现决策树 Desision Tree & 可视化
2019-04-30
决策树 Decision tree
2019-04-30
nominal和ordinal & 数据处理中四种基本数据类型
2019-04-30
Grid SearchCV(网格搜索)& Python实现
2019-04-30
单目深度估计 monodepth2模型 代码
2019-04-30
位图索引Bitmap indexes
2019-04-30
YOLO算法(二)—— Yolov2 & yolo9000
2019-04-30
Python的__future__模块
2019-04-30
计算机视觉中的cost-volume的概念具体指什么(代价体积)
2019-04-30
启发函数heuristic 与 A*
2019-04-30
Image Pyramid(图像金字塔)
2019-04-30
Oracle 作业记录
2019-04-30
putty连接AWS配置(multimedia project)
2019-04-30