2013-12-15 2 views
3

Bitmap 클래스에 압축 메소드가 있음을 알기 전에이 메소드를 작성했습니다.Android 비트 맵 압축

/** 
* Calcuate how much to compress the image 
* @param options 
* @param reqWidth 
* @param reqHeight 
* @return 
*/ 
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) { 

    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; // default to not zoom image 

    if (height > reqHeight || width > reqWidth) { 
      final int heightRatio = Math.round((float) height/ (float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 
     return inSampleSize; 
} 

/** 
* resize image to 480x800 
* @param filePath 
* @return 
*/ 
public static Bitmap getSmallBitmap(String filePath) { 

    File file = new File(filePath); 
    long originalSize = file.length(); 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, options); 

    // Calculate inSampleSize based on a preset ratio 
    options.inSampleSize = calculateInSampleSize(options, 480, 800); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    Bitmap compressedImage = BitmapFactory.decodeFile(filePath, options); 


    return compressedImage; 
} 

궁금 해서요, 내가이 일을 계속 사용하거나, 하나의 내장 사용하도록 전환해야한다는 Compress 방법에 내장 된 비교? 차이점은 무엇입니까? 당신은 당신이 SampleSize을 이미지의 품질의 느슨한 많이 사용하지 않을 것이기 때문에 이미지를 크기를 조정하는 위의 코드에서 뭐하는

+0

이것을 확인하십시오. http://stackoverflow.com/questions/31156501/what-is-the-difference-between-compressing-image-and-resizing-image-android –

답변

2

내 방법 Loading Large Bitmap 지침

와 일치한다 메모리
  • 작은 비트 맵
  • 압축() 메소드가 작은 것과 큰 비트 맵 변환 디스크

  • 큰 파일
      :

      • 메모리의 큰 비트 맵
      • 작은 디스크 (IOStream)에 비트 맵 (및 다른 형식) 나는 다른 크기의 ImageViews에 파일에서 비트 맵을로드하는 데 필요한 경우

      내가 당신의 방법을 사용합니다.

  • +0

    음, 실제로 방금 테스트를 해봤는데 내 코드가지도를 줄이지 않는 것 같습니다 –

    +0

    이미 비트 맵이 충분히 작을 수 있습니까? 또한 calculateInSampleSize()에 반복 루프가 누락되었습니다. Google 코드를 복사하여 붙여 넣기 만하면됩니다. – Y2i

    2

    Basically

    .

    compress(Bitmap.CompressFormat format, int quality, OutputStream stream)

    변경할 때 사용하는 imageFormat 당신이 Bitmap.CompressFormat JPEG
    Bitmap.CompressFormat PNGBitmap.CompressFormat WEBP 또는 quality 매개 변수 0 - 100를 사용하여 이미지의 quality을 줄일 수 있습니다.