美文网首页
[转]Bitmap的优化

[转]Bitmap的优化

作者: 兔斯基第2号 | 来源:发表于2018-07-20 15:14 被阅读0次

    Bitmap的优化套路很简单,粗暴,就是让压缩。
    三种压缩方式:

    1. 对图片质量进行压缩
    2. 对图片尺寸进行压缩
    3. 使用libjpeg.so库进行压缩

    对图片质量进行压缩

    public static Bitmap compressImage(Bitmap bitmap){  
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                //质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
                int options = 100;  
                //循环判断如果压缩后图片是否大于50kb,大于继续压缩  
                while ( baos.toByteArray().length / 1024>50) {  
                    //清空baos  
                    baos.reset();  
                    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);  
                    options -= 10;//每次都减少10  
                }  
                //把压缩后的数据baos存放到ByteArrayInputStream中  
                ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
                //把ByteArrayInputStream数据生成图片  
                Bitmap newBitmap = BitmapFactory.decodeStream(isBm, null, null);  
                return newBitmap;  
            }  
    

    对图片尺寸进行压缩

     /**
         * 按图片尺寸压缩 参数是bitmap
         * @param bitmap
         * @param pixelW
         * @param pixelH
         * @return
         */
        public static Bitmap compressImageFromBitmap(Bitmap bitmap, int pixelW, int pixelH) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            if( os.toByteArray().length / 1024>512) {//判断如果图片大于0.5M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
                os.reset();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
            }
            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            BitmapFactory.decodeStream(is, null, options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = computeSampleSize(options , pixelH > pixelW ? pixelW : pixelH ,pixelW * pixelH );
            is = new ByteArrayInputStream(os.toByteArray());
            Bitmap newBitmap = BitmapFactory.decodeStream(is, null, options);
            return newBitmap;
        }
    
    
        /**
         * 动态计算出图片的inSampleSize
         * @param options
         * @param minSideLength
         * @param maxNumOfPixels
         * @return
         */
        public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
            int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
            int roundedSize;
            if (initialSize <= 8) {
                roundedSize = 1;
                while (roundedSize < initialSize) {
                    roundedSize <<= 1;
                }
            } else {
                roundedSize = (initialSize + 7) / 8 * 8;
            }
            return roundedSize;
        }
    
        private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
            double w = options.outWidth;
            double h = options.outHeight;
            int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
            int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
            if (upperBound < lowerBound) {
                return lowerBound;
            }
            if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
                return 1;
            } else if (minSideLength == -1) {
                return lowerBound;
            } else {
                return upperBound;
            }
        }
    

    使用libjpeg.so库进行压缩

    可以参考这篇Android性能优化系列之Bitmap图片优化:
    https://blog.csdn.net/u012124438/article/details/66087785

    摘自:《Android 性能优化最佳实践》任玉刚公众号

    相关文章

      网友评论

          本文标题:[转]Bitmap的优化

          本文链接:https://www.haomeiwen.com/subject/psqqmftx.html