压缩图片
来源:互联网 发布:python正则表达式知乎 编辑:程序博客网 时间:2024/05/16 19:28
一个人的端午太过无聊了,记一下最近遇到的一个bug。话说在用WebView加载一张本地图片的时候,直接加载速度比较慢,于是我打算将图片压缩一下质量再转为输入流给WebView,结果耗时更长了,代码如下:
long time = System.currentTimeMillis(); Log.e(TAG, "shouldInterceptRequest: "); Bitmap bmp = BitmapFactory.decodeFile(imgPath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG,80,baos); Log.e(TAG, "shouldInterceptRequest: "+(System.currentTimeMillis()-time));
05-29 16:54:33.590 4769-4832/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 05-29 16:55:43.740 4769-4832/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 70151
后来我一直不明白,为什么我压缩的结果是如此慢?直到看了人家的压缩以后才发现,我加载同样一张不到6M图片的图片,耗时60秒——70秒,而压缩以后耗时只有10%,并且,APP使使用的内存差别也很大,我的压缩达结果占用内存达到100M以上,而别人压缩以后加载内存占用不到30M,那么别人是怎么压缩的呢?
Bitmap bitmap = compressBitmap(srcPath, rqsW, rqsH); long time = System.currentTimeMillis(); Log.e("StreamActivity", "shouldInterceptRequest: ");// Bitmap bitmap = BitmapFactory.decodeFile(srcPath); int degree = getDegress(srcPath); try { if (degree != 0) bitmap = rotateBitmap(bitmap, degree); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos); Log.e("StreamActivity", "shouldInterceptRequest: "+(System.currentTimeMillis()-time));* 压缩指定路径的图片,并得到图片对象 * * @param path bitmap source path * @return Bitmap {@link android.graphics.Bitmap} */ public final static Bitmap compressBitmap(String path, int rqsW, int rqsH) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = caculateInSampleSize(options, rqsW, rqsH); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); }/** * get the orientation of the bitmap {@link android.media.ExifInterface} * 获取bitmap的方向信息 * * @param path * @return */ public final static int getDegress(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; }/** * rotate the bitmap * * @param bitmap * @param degress * @return */ public static Bitmap rotateBitmap(Bitmap bitmap, int degress) { if (bitmap != null) { Matrix m = new Matrix(); m.postRotate(degress); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); return bitmap; } return bitmap; }
别人的结果:
05-29 17:01:46.350 10313-11784/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 05-29 17:01:46.410 10313-11784/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 5505-29 17:01:55.190 10313-11784/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 05-29 17:01:55.250 10313-11784/com.wxxiaomi.ming.webviewcachemodule E/StreamActivity: shouldInterceptRequest: 55
so,图片压缩的最高境界是先压缩像素后压缩质量,否则将图片文件转为输入流的时候就非常耗时且占用内存很厉害!
over!