2017-02-17 5 views
3

안드로이드 비트 맵 스케일링과 샘플링 사이에 혼란이 있습니다. 스케일링을위한 두 개의 코드와 샘플링을위한 또 다른 코드가있을 수 있습니다.이 두 코드의 작업을 식별하고 그 차이점은 무엇입니까?
비트 맵 스케일링과 샘플링의 차이점은 무엇입니까?

는 스케일링 :

public static Bitmap getScaleBitmap(Bitmap bitmap, int newWidth, int newHeight) { 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 
    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 
} 

샘플링 :
여기

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.id.myimage, 100, 100)); 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

코드를 모두 크기 조정, 이미지의 내가 좋은 간단 어느 식별 할 수있는 방법을 다른 방법을 수행합니다.

답변

0

스케일링 : 먼저 메모리의 전체 비트 맵을 디코딩 한 다음 크기를 조정합니다.

샘플링 : 전체 비트 맵을 메모리에로드하지 않고 필요한 크기 조정 비트 맵을 얻습니다.

0

첫 번째 코드는 비트 맵을 사용하여 더 작은 새로운 비트 맵을 만듭니다. 더 큰 비트 맵에는 memmory를 사용합니다.

두 번째 코드는 리소스를 사용합니다. inJustDecodeBounds memmory에 전체 비트 맵을로드하지 않도록합니다. 그런 다음 크기 조정 방법을 계산 한 다음 inJustDecodeBounds을 memmory 축소 이미지 버전으로 잘못로드하면 다시 계산하십시오. 따라서 디코딩 된 이미지에만 memmory를 사용하십시오.

Oficial docs