원본 비트 맵을로드하지 않고 비트 맵 이미지로 자른 버전의 비트 맵 이미지를로드하고 싶습니다.안드로이드에서 비트 맵 파일의 일부만로드하는 중
원시 데이터를 처리하기 위해 사용자 정의로드 루틴을 작성하지 않고도이 작업을 수행 할 수 있습니까?
감사합니다, 산도르
원본 비트 맵을로드하지 않고 비트 맵 이미지로 자른 버전의 비트 맵 이미지를로드하고 싶습니다.안드로이드에서 비트 맵 파일의 일부만로드하는 중
원시 데이터를 처리하기 위해 사용자 정의로드 루틴을 작성하지 않고도이 작업을 수행 할 수 있습니까?
감사합니다, 산도르
실제로 할 매우 간단합니다.
Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)
업데이트를 사용 BitmapRegionDecoder
이
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);
oops..sorry .. sdk 버전 2.3 이상에서이 메소드가 의도 한 것 ... – Renjith
@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
입력 이미지가 너무 크기 때문에 처음에'startingSize'> 32 인 경우 어떻게해야합니까? – TWiStErRob
사용 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 이상이 필요 않습니다.
당신은 밖으로 완전히 여전히 목표보다 이미지 큰 산출 가능한 최대 inSampleSize를 계산 다음 알고리즘
자세한 내용은 Android: Resize a large bitmap file to scaled output file을 참조하십시오.
Thanks Steve,하지만 먼저 sourceBitmap을로드해야합니다. 메모리에 들어가기에는 너무 커서, 다운 샘플링되지 않은 비트 맵을 사용하고 싶습니다. . – Schermvlieger
죄송 합니다만, 귀하의 질문에 그 부분을 제대로 읽지 않았습니다. 다운 샘플링없이이 작업을 수행하는 방법을 잘 모르겠습니다. 사실 아주 비슷한 문제가 내 자신의 그림에 나와 있습니다! –