压缩图片

一个人的端午太过无聊了,记一下最近遇到的一个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: 55
05-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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值