성능 최적화 11_Bitmap 압축

안드로이드 성능 최적화 요약

1 압축 품질

  • 원리 : 이 점의 수의 근방 (동화) 픽셀과 유사한 포토 밖으로 당겨 알고리즘으로 제시 목적 파일 크기의 품질을 감소시킨다.
  • 주 : 메모리에 비트 맵의 크기는 픽셀의 관점에서 계산되기 때문에에만 품질 압축 폭 * 높이이며, 실제의 화소 이미지를 변경하지 않는, 파일에 실제로 영향을 달성 할 수있다
  • 사용 시나리오 : 서버에 로컬 또는 업로드 할 사진을 압축 한 후 사진 저장. 실제 요구에 따라.
  • 구현 코드
 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 압축 크기

  • 원리 : 존재의 참된 의미를 감소 화소 단위 픽셀 크기의 값을 감소시켜
  • 사용 시나리오 : 캐시 축소판 때 (영상 처리)
  • 구현 코드
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) 샘플링 레이트 감소 (보통)

  • 원리 : 샘플 레이트의 이미지 세트의 이미지 픽셀을 줄이기
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 데모

BitmapActivity

게시 된 229 개 원래 기사 · 원 찬양 72 ·은 15 만 + 조회수

추천

출처blog.csdn.net/baopengjian/article/details/104186997