对BitMap进行缩略

    xiaoxiao2021-03-25  73

    1,sdk提供的方法

    //sdk提供的的方法 Bitmap bmp = ThumbnailUtils.extractThumbnail(bitmap, 60, 60, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

    2,goole推荐的方法

    //指定采样率 来缩小图片的分辨率,降低了图片尺寸,有效避免OOM public Bitmap decodeBmpFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // 设置inJustDecodeBounds = true ,表示先不加载图片 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // 计算合适的 采样率inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 修改图片 编码格式 /* Bitmap.Config ARGB_8888:一个像素点占8+8+8+8=32位 Bitmap.Config ARGB_4444:一个像素点占4+4+4+4=16位 Bitmap.Config RGB_565:一个像素点占5+6+5=16位 Bitmap.Config ALPHA_8:只有透明度,没有颜色,那么一个像素点占8位。 */ options.inPreferredConfig = Bitmap.Config.RGB_565; // inJustDecodeBounds 置为 false 真正开始加载图片 options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } // 计算 采样率inSimpleSize的值的方法 /* 当inSimpleSize为1时,图片的分辨率就是原来的分辨率,也就是1200*800 当inSimpleSize为2时,表示图片的宽和高都是为原来的1/2,所整张图变成了原来的1/4 当inSimpleSize位4时,表示图片的宽和高都是为原来的1/4,所以整张图也就变成原来的1/16 */ public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { if (reqWidth == 0 || reqHeight == 0) { return 1; } // 获取图片原生的宽和高 final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; // 如果原生的宽高大于请求的宽高,那么将原生的宽和高都置为原来的一半 if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // 主要计算逻辑 while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }

    3,把bitmap压缩成指定尺寸的图片

    public static Bitmap compressBmp(Bitmap bitmap, double maxSize) { if (null == bitmap) { return null; } if (bitmap.isRecycled()) { return null; } // 单位:从 Byte 换算成 KB double currentSize = bmp2ByteArray(bitmap, false).length / 1024; // 判断bitmap占用空间是否大于允许最大空间,如果大于则压缩,小于则不压缩 while (currentSize > maxSize) { // 计算bitmap的大小是maxSize的多少倍 double multiple = currentSize / maxSize; // 开始压缩:将宽带和高度压缩掉对应的平方根倍 // 1.保持新的宽度和高度,与bitmap原来的宽高比率一致 // 2.压缩后达到了最大大小对应的新bitmap,显示效果最好 bitmap = compressBmp(bitmap, bitmap.getWidth() / Math.sqrt(multiple), bitmap.getHeight() / Math.sqrt(multiple)); currentSize = bmp2ByteArray(bitmap, false).length / 1024; } return bitmap; } public static Bitmap compressBmp(Bitmap bitmap, double newWidth, double newHeight) { if (null == bitmap) { return null; } if (bitmap.isRecycled()) { return null; } if (newWidth <= 0 || newHeight <= 0) { return null; } // 获取图片的宽和高 float width = bitmap.getWidth(); float height = bitmap.getHeight(); // 创建操作图片的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true); return bmp; } private static byte[] bmp2ByteArray(Bitmap bitmap, boolean needRecycle) { if (null == bitmap) { return null; } if (bitmap.isRecycled()) { return null; } ByteArrayOutputStream output = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bitmap.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { Log.e(TAG, e.toString()); } return result; }

    点击打开链接

    转载请注明原文地址: https://ju.6miu.com/read-41018.html

    最新回复(0)