Performance Optimization 11_Bitmap compression

Android Performance Optimization Summary

1 compression quality

  • Principle: By algorithm pull out (assimilation) pixel similar picture in the vicinity of a number of these points to reduce the quality of presentation purpose file size.
  • Note: It can only be achieved in fact impact on the file, because the size of the bitmap in memory is calculated in terms of pixels, which is the width * height, for quality compression, and does not change the actual pixel image
  • Usage scenarios: save the picture after compression to local or upload pictures to the server. According to the actual demand.
  • Implementation code
 public void qualitCompress(View v) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        //压缩图片
        compressImageToFile(bitmap, new File(sdFile, "qualityCompress.jpeg"));
    }


private void compressImageToFile(Bitmap bitmap, File file) {
        //0 ~ 100
        int quality = 50;
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality,bao );
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bao.toByteArray());
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2 compression size

  • Principle : By reducing the value of the unit pixel size, reduced pixel on the true meaning of being
  • Usage scenarios : cache thumbnails when (picture processing)
  • Implementation code
public static void compressBitmapToFileBySize(Bitmap bmp, File file) {
        //压缩尺寸倍数,值越大,图片的尺寸就越小
        int ratio = 4;

        Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(result);
        RectF rect = new RectF(0,0,bmp.getWidth()/ratio,bmp.getHeight()/ratio);
        canvas.drawBitmap(bmp,null ,rect ,null );

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        result.compress(Bitmap.CompressFormat.JPEG,100 ,bao );

        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bao.toByteArray());
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3 sampling rate reduction (usually)

  • Principle : set the image of the sample rate, reduce the image pixels
public static void compressBitmap(String filePath, File file){
        // 数值越高,图片像素越低
        int inSampleSize = 8;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
//	        options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
        //采样率
        options.inSampleSize = inSampleSize;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 把压缩后的数据存放到baos中
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);
        try {
            if(file.exists())
            {
                file.delete();
            }
            else {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3 Demo

BitmapActivity

Published 229 original articles · won praise 72 · Views 150,000 +

Guess you like

Origin blog.csdn.net/baopengjian/article/details/104186997