2013-04-02 4 views
26

내 비트 맵 이미지 크기를 최대 640px로 줄이고 싶습니다. 예를 들어, 크기가 1200 x 1200 픽셀 인 비트 맵 이미지가 있습니다. 어떻게하면 640 픽셀로 줄일 수 있습니까? 이 방법비트 맵의 ​​크기를 Android의 특정 픽셀로 줄입니다.

/** getResizedBitmap method is used to Resized the Image according to custom width and height 
    * @param image 
    * @param newHeight (new desired height) 
    * @param newWidth (new desired Width) 
    * @return image (new resized image) 
    * */ 
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, 
      matrix, false); 
    return resizedBitmap; 
} 

답변

72

widthheight는 사용

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) { 
    return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true); 
} 

같은 비트 맵 비율을 유지하지만, 최대 변의 길이, 사용을 줄이려면 :

public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float) width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 

     return Bitmap.createScaledBitmap(image, width, height, true); 
} 
+7

이 스 니펫을 보내 주셔서 감사합니다. 어렵습니다. "if (bitmapRatio> 1)"가 0이 아닌지 확인해야합니다. 높이가 더 크더라도 음수 비율은 없습니다. – ClemM

+0

어디서나이 솔루션을 볼 수 있습니다. 하지만 내 비트 맵을 왜 자르지? 난 단지 상단 부분을 떠난 그것은뿐만 아니라 화면의 오른쪽으로 이동됩니다 ( – Sermilion

11

사용 :

Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter); 

가 필터를 통과 = 거짓이 고르지, 픽셀 화 된 이미지가 발생합니다.

전달 필터를 true로 설정하면 더 부드러운 가장자리가 나타납니다. 당신은 비트 맵을 통과하면

10

또는 당신이 이런 식으로 작업을 수행 할 수 있습니다

+0

방법 이 방법을 사용할 수 있습니까? 이 방법은 존재하지 않습니다! – coolcool1994

+3

당신 downvote 했습니까? 그것이 존재하지 않는다는 것이 무엇을 의미합니까? Bitmap.createScaledBitmap이 (가) api lvl 이후에 존재합니다. –

관련 문제