use of android.graphics.BitmapRegionDecoder in project android-crop by jdamcd.
the class CropImageActivity method decodeRegionCrop.
private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
// Release memory now
clearImageView();
InputStream is = null;
Bitmap croppedImage = null;
try {
is = getContentResolver().openInputStream(sourceUri);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
final int width = decoder.getWidth();
final int height = decoder.getHeight();
if (exifRotation != 0) {
// Adjust crop area to account for image rotation
Matrix matrix = new Matrix();
matrix.setRotate(-exifRotation);
RectF adjusted = new RectF();
matrix.mapRect(adjusted, new RectF(rect));
// Adjust to account for origin at 0,0
adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right, (int) adjusted.bottom);
}
try {
croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
Matrix matrix = new Matrix();
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
}
} catch (IllegalArgumentException e) {
// Rethrow with some extra information
throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + "," + height + "," + exifRotation + ")", e);
}
} catch (IOException e) {
Log.e("Error cropping image: " + e.getMessage(), e);
setResultException(e);
} catch (OutOfMemoryError e) {
Log.e("OOM cropping image: " + e.getMessage(), e);
setResultException(e);
} finally {
CropUtil.closeSilently(is);
}
return croppedImage;
}
use of android.graphics.BitmapRegionDecoder in project ion by koush.
the class LoadDeepZoom method onCompleted.
@Override
public void onCompleted(Exception e, final Response<File> response) {
if (e == null)
e = response.getException();
if (e != null) {
report(e, null);
return;
}
final File tempFile = response.getResult();
if (ion.bitmapsPending.tag(key) != this) {
// Log.d("IonBitmapLoader", "Bitmap load cancelled (no longer needed)");
return;
}
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
FileInputStream fin = null;
try {
File file;
// file cache will be null if the file is on the local file system already
if (fileCache != null) {
fileCache.commitTempFiles(key, tempFile);
file = fileCache.getFile(key);
} else {
// local file system, use the "temp" file as the source.
file = tempFile;
}
BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
final Point size = new Point(options.outWidth, options.outHeight);
if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
fin = fileCache.get(key);
GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(fin)));
GifFrame frame = gifDecoder.nextFrame();
BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
info.gifDecoder = gifDecoder;
report(null, info);
return;
}
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(file.toString(), false);
Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, size.x, size.y), options);
if (bitmap == null)
throw new Exception("unable to load decoder");
BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
info.decoder = decoder;
info.decoderFile = file;
info.servedFrom = response.getServedFrom();
report(null, info);
} catch (Exception e) {
report(e, null);
} finally {
StreamUtility.closeQuietly(fin);
}
}
});
}
use of android.graphics.BitmapRegionDecoder in project GalleryFinal by pengjianbo.
the class CropImageActivity method decodeRegionCrop.
private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
// Release memory now
clearImageView();
InputStream is = null;
Bitmap croppedImage = null;
try {
is = getContentResolver().openInputStream(sourceUri);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
final int width = decoder.getWidth();
final int height = decoder.getHeight();
if (exifRotation != 0) {
// Adjust crop area to account for image rotation
Matrix matrix = new Matrix();
matrix.setRotate(-exifRotation);
RectF adjusted = new RectF();
matrix.mapRect(adjusted, new RectF(rect));
// Adjust to account for origin at 0,0
adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right, (int) adjusted.bottom);
}
try {
croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
Matrix matrix = new Matrix();
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
}
} catch (IllegalArgumentException e) {
// Rethrow with some extra information
throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + "," + height + "," + exifRotation + ")", e);
}
} catch (IOException e) {
ILogger.e(e);
setCropSaveException(e);
} catch (OutOfMemoryError e) {
ILogger.e(e);
setCropSaveException(e);
} finally {
CropUtil.closeSilently(is);
}
return croppedImage;
}
use of android.graphics.BitmapRegionDecoder in project weiciyuan by qii.
the class ImageUtility method getMiddlePictureInTimeLine.
public static Bitmap getMiddlePictureInTimeLine(String url, int reqWidth, int reqHeight, FileDownloaderHttpHelper.DownloadListener downloadListener) throws WeiboException {
try {
String filePath = FileManager.getFilePathFromUrl(url, FileLocationMethod.picture_bmiddle);
File file = new File(filePath);
if (!file.exists() && !SettingUtility.isEnablePic()) {
return null;
}
if (!file.exists()) {
getBitmapFromNetWork(url, filePath, downloadListener);
}
if (isGif(filePath)) {
return getMiddlePictureInTimeLineGif(filePath, reqWidth, reqHeight);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
int height = options.outHeight;
int width = options.outWidth;
int cutHeight = 0;
int cutWidth = 0;
if (height >= reqHeight && width >= reqWidth) {
cutHeight = reqHeight;
cutWidth = reqWidth;
} else if (height < reqHeight && width >= reqWidth) {
cutHeight = height;
cutWidth = (reqWidth * cutHeight) / reqHeight;
} else if (height >= reqHeight && width < reqWidth) {
cutWidth = width;
cutHeight = (reqHeight * cutWidth) / reqWidth;
} else if (height < reqHeight && width < reqWidth) {
float betweenWidth = ((float) reqWidth - (float) width) / (float) width;
float betweenHeight = ((float) reqHeight - (float) height) / (float) height;
if (betweenWidth > betweenHeight) {
cutWidth = width;
cutHeight = (reqHeight * cutWidth) / reqWidth;
} else {
cutHeight = height;
cutWidth = (reqWidth * cutHeight) / reqHeight;
}
}
if (cutWidth > 0 && cutHeight > 0) {
int startX = 0;
if (cutWidth < width) {
startX = (width - cutWidth) / 2;
}
try {
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(filePath, false);
if (decoder != null) {
Bitmap bitmap = decoder.decodeRegion(new Rect(startX, 0, startX + cutWidth, cutHeight), null);
if (bitmap.getHeight() < reqHeight && bitmap.getWidth() < reqWidth) {
Bitmap scale = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
if (scale != bitmap) {
bitmap.recycle();
bitmap = scale;
}
}
if (bitmap != null) {
Bitmap roundedCornerBitmap = ImageEditUtility.getRoundedCornerBitmap(bitmap);
if (roundedCornerBitmap != bitmap) {
bitmap.recycle();
bitmap = roundedCornerBitmap;
}
return bitmap;
}
}
} catch (IOException ignored) {
}
}
return null;
} catch (OutOfMemoryError ignored) {
ignored.printStackTrace();
return null;
}
}
use of android.graphics.BitmapRegionDecoder in project AisenWeiBo by wangdan.
the class TimelineBitmapCompress method compress.
@Override
public Bitmap compress(byte[] bitmapBytes, File file, String url, ImageConfig config, int origW, int origH) throws Exception {
Logger.v("ATimeline", "压缩小图片");
Bitmap bitmap = null;
int maxWidth = config.getMaxWidth() == 0 ? SystemUtils.getScreenWidth(GlobalContext.getInstance()) : config.getMaxWidth();
int maxHeight = config.getMaxHeight() == 0 ? SystemUtils.getScreenHeight(GlobalContext.getInstance()) : config.getMaxHeight();
// 如果高度比宽度在2倍以上,取高度的一部分
if (origH * 1.0f / origW > 2) {
int reqHeight = maxHeight;
// 截取局部图片
BitmapRegionDecoder bitmapDecoder = BitmapRegionDecoder.newInstance(bitmapBytes, 0, bitmapBytes.length, true);
Rect rect = new Rect(0, 0, origW, reqHeight);
bitmap = bitmapDecoder.decodeRegion(rect, null).copy(Config.ARGB_8888, true);
} else {
bitmap = BitmapDecoder.decodeSampledBitmapFromByte(bitmapBytes, maxWidth, maxHeight);
}
Logger.d(TAG, String.format("bitmap width = %d, height = %d", bitmap.getWidth(), bitmap.getHeight()));
return bitmap;
}
Aggregations