Bitmap compress

public static byte[] getByteArray(final String filePath, int maxLen) {   
            InputStream inStream = new FileInputStream(new File(filePath));
            Bitmap bmp = BitmapFactory.decodeStream(inStream);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
           UtilsLog.d(TAG, "bmpToByteArray a len:" + output.toByteArray().length);
           int options = 100;
           while (output.toByteArray().length > maxkb && options != 10) {
               output.reset(); 
               bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
               UtilsLog.d(TAG, "bmpToByteArray b options:" +options+" len:"+ output.toByteArray().length);
               options -= 10;
            }
            UtilsLog.d(TAG, "bmpToByteArray c len:" + output.toByteArray().length);
            return output.toByteArray();     
    }

decodeStream对于大图片非常耗时,


image.png

最终压缩长度为最初的0.029 即不到3%

你可能感兴趣的:(Bitmap compress)