Ответ 1
На самом деле это очень просто. Используйте
Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)
Обновление: используйте BitmapRegionDecoder
Я хотел бы загрузить обрезанную версию растрового изображения в объект Bitmap, не загружая также исходное растровое изображение.
Возможно ли это без написания пользовательских подпрограмм загрузки для обработки необработанных данных?
Спасибо, Шандор
На самом деле это очень просто. Используйте
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);
@RKN
Ваш метод также может вызывать исключение OutOfMemoryError - если обрезанное растровое изображение превышает VM.
Мой метод объединяет ваши и защиту от этого исключения: (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
Используйте RapidDecoder.
И просто сделайте это
import rapid.decoder.BitmapDecoder;
Rect bounds = new Rect(left, top, right, bottom);
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
.region(bounds).decode();
Для этого требуется Android 2.2 или выше.
Вы можете загрузить масштабированную версию растрового изображения без полной загрузки растрового изображения, используя следующий алгоритм
Проверьте следующее сообщение Android: измените размер большого файла растрового изображения на масштабируемый выходной файл для получения более подробной информации.