2010-03-30 5 views

답변

10

실제로 할 매우 간단합니다.

Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)

업데이트를 사용 BitmapRegionDecoder

+3

Thanks Steve,하지만 먼저 sourceBitmap을로드해야합니다. 메모리에 들어가기에는 너무 커서, 다운 샘플링되지 않은 비트 맵을 사용하고 싶습니다. . – Schermvlieger

+0

죄송 합니다만, 귀하의 질문에 그 부분을 제대로 읽지 않았습니다. 다운 샘플링없이이 작업을 수행하는 방법을 잘 모르겠습니다. 사실 아주 비슷한 문제가 내 자신의 그림에 나와 있습니다! –

9

InputStream istream = null; 
try { 
    istream = this.getContentResolver().openInputStream(yourBitmapUri); 
} catch (FileNotFoundException e1) { 
    e1.printStackTrace(); 
} 

BitmapRegionDecoder decoder  = null; 
try { 
    decoder = BitmapRegionDecoder.newInstance(istream, false); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
Bitmap bMap = decoder.decodeRegion(new Rect(istream, x to start from, y to start from, x to end with, y to end with), null);  
imageView.setImageBitmap(bMap); 
+0

oops..sorry .. sdk 버전 2.3 이상에서이 메소드가 의도 한 것 ... – Renjith

0

@RKN

귀하의 방법도 예외가 OutOfMemoryError를 던질 수있는 시도를 사용 - 잘린 비트 맵 VM을 초과하는 경우.

내 방법이 exeption에 너의 보호를 결합 : (L, T, R, B - 이미지의 %)

Bitmap cropBitmap(ContentResolver cr, String file, float l, float t, float r, float b) 
{ 
    try 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     // First decode with inJustDecodeBounds=true to check dimensions 
     BitmapFactory.decodeFile(file, options); 
     int oWidth = options.outWidth; 
     int oHeight = options.outHeight; 

     InputStream istream = cr.openInputStream(Uri.fromFile(new File(file))); 

     BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(istream, false); 
     if (decoder != null) 
     { 
      options = new BitmapFactory.Options(); 

      int startingSize = 1; 
      if ((r - l) * oWidth * (b - t) * oHeight > 2073600) 
       startingSize = (int) ((r - l) * oWidth * (b - t) * oHeight/2073600) + 1; 

      for (options.inSampleSize = startingSize; options.inSampleSize <= 32; options.inSampleSize++) 
      { 
       try 
       { 
        return decoder.decodeRegion(new Rect((int) (l * oWidth), (int) (t * oHeight), (int) (r * oWidth), (int) (b * oHeight)), options);  
       } 
       catch (OutOfMemoryError e) 
       { 
        Continue with for loop if OutOfMemoryError occurs 
       } 
      } 
     } 
     else 
      return null; 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    return null; 
} 

및 최대 사용 가능한 비트 맵 또는 null

+0

입력 이미지가 너무 크기 때문에 처음에'startingSize'> 32 인 경우 어떻게해야합니까? – TWiStErRob

0

사용 RapidDecoder를 반환합니다.

그리고 단순히이

import rapid.decoder.BitmapDecoder; 

Rect bounds = new Rect(left, top, right, bottom); 
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image) 
     .region(bounds).decode(); 

그것은 안드로이드 2.2 이상이 필요 않습니다.

0

당신은 밖으로 완전히 여전히 목표보다 이미지 큰 산출 가능한 최대 inSampleSize를 계산 다음 알고리즘

  • 를 사용하여 비트 맵을로드와 비트 맵의 ​​확장 버전을로드 할 수 있습니다.
  • BitmapFactory.decodeFile (file, options)을 사용하여 이미지를로드하고 inSampleSize를 옵션으로 전달합니다.
  • Bitmap.createScaledBitmap()을 사용하여 원하는 크기로 크기를 조정하십시오.

자세한 내용은 Android: Resize a large bitmap file to scaled output file을 참조하십시오.

관련 문제