use of android.graphics.BitmapRegionDecoder in project robolectric by robolectric.
the class ShadowBitmapRegionDecoderTest method testDecodeRegionReturnsExpectedConfig.
@Test
public void testDecodeRegionReturnsExpectedConfig() throws IOException {
BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(getImageInputStream(), false);
BitmapFactory.Options options = new BitmapFactory.Options();
assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()).isEqualTo(Bitmap.Config.ARGB_8888);
options.inPreferredConfig = null;
assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()).isEqualTo(Bitmap.Config.ARGB_8888);
options.inPreferredConfig = Bitmap.Config.RGB_565;
assertThat(bitmapRegionDecoder.decodeRegion(new Rect(0, 0, 1, 1), options).getConfig()).isEqualTo(Bitmap.Config.RGB_565);
}
use of android.graphics.BitmapRegionDecoder in project robolectric by robolectric.
the class ShadowBitmapRegionDecoderTest method testDecodeRegionReturnsExpectedSize.
@Test
public void testDecodeRegionReturnsExpectedSize() throws IOException {
BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(getImageInputStream(), false);
Bitmap bitmap = bitmapRegionDecoder.decodeRegion(new Rect(10, 20, 110, 220), new BitmapFactory.Options());
assertThat(bitmap.getWidth()).isEqualTo(100);
assertThat(bitmap.getHeight()).isEqualTo(200);
}
use of android.graphics.BitmapRegionDecoder in project SimpleCropView by IsseiAoki.
the class CropImageView method decodeRegion.
// Cropping ////////////////////////////////////////////////////////////////////////////////////
private Bitmap decodeRegion() {
Bitmap cropped = null;
InputStream is = null;
try {
is = getContext().getContentResolver().openInputStream(mSourceUri);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
final int originalImageWidth = decoder.getWidth();
final int originalImageHeight = decoder.getHeight();
Rect cropRect = calcCropRect(originalImageWidth, originalImageHeight);
if (mAngle != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(-mAngle);
RectF rotated = new RectF();
matrix.mapRect(rotated, new RectF(cropRect));
rotated.offset(rotated.left < 0 ? originalImageWidth : 0, rotated.top < 0 ? originalImageHeight : 0);
cropRect = new Rect((int) rotated.left, (int) rotated.top, (int) rotated.right, (int) rotated.bottom);
}
cropped = decoder.decodeRegion(cropRect, new BitmapFactory.Options());
if (mAngle != 0) {
Bitmap rotated = getRotatedBitmap(cropped);
if (cropped != getBitmap() && cropped != rotated) {
cropped.recycle();
}
cropped = rotated;
}
} catch (IOException e) {
Logger.e("An error occurred while cropping the image: " + e.getMessage(), e);
} catch (OutOfMemoryError e) {
Logger.e("OOM Error: " + e.getMessage(), e);
} catch (Exception e) {
Logger.e("An unexpected error has occurred: " + e.getMessage(), e);
} finally {
Utils.closeQuietly(is);
}
return cropped;
}
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;
}
use of android.graphics.BitmapRegionDecoder in project android_frameworks_base by AOSPA.
the class WallpaperManager method getBuiltInDrawable.
/**
* Returns a drawable for the built-in static wallpaper of the specified type. Based on the
* parameters, the drawable can be cropped and scaled.
*
* @param outWidth The width of the returned drawable
* @param outWidth The height of the returned drawable
* @param scaleToFit If true, scale the wallpaper down rather than just cropping it
* @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
* 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
* @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
* 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
* @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
* IllegalArgumentException if an invalid wallpaper is requested.
* @return A Drawable presenting the built-in default wallpaper image of the given type,
* or {@code null} if no default image of that type is defined on this device.
*/
public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException());
}
if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
}
Resources resources = mContext.getResources();
horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
InputStream wpStream = openDefaultWallpaper(mContext, which);
if (wpStream == null) {
if (DEBUG) {
Log.w(TAG, "default wallpaper stream " + which + " is null");
}
return null;
} else {
InputStream is = new BufferedInputStream(wpStream);
if (outWidth <= 0 || outHeight <= 0) {
Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
return new BitmapDrawable(resources, fullSize);
} else {
int inWidth;
int inHeight;
// Just measure this time through...
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
if (options.outWidth != 0 && options.outHeight != 0) {
inWidth = options.outWidth;
inHeight = options.outHeight;
} else {
Log.e(TAG, "default wallpaper dimensions are 0");
return null;
}
}
// Reopen the stream to do the full decode. We know at this point
// that openDefaultWallpaper() will return non-null.
is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
RectF cropRectF;
outWidth = Math.min(inWidth, outWidth);
outHeight = Math.min(inHeight, outHeight);
if (scaleToFit) {
cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight, horizontalAlignment, verticalAlignment);
} else {
float left = (inWidth - outWidth) * horizontalAlignment;
float right = left + outWidth;
float top = (inHeight - outHeight) * verticalAlignment;
float bottom = top + outHeight;
cropRectF = new RectF(left, top, right, bottom);
}
Rect roundedTrueCrop = new Rect();
cropRectF.roundOut(roundedTrueCrop);
if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
Log.w(TAG, "crop has bad values for full size image");
return null;
}
// See how much we're reducing the size of the image
int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth, roundedTrueCrop.height() / outHeight);
// Attempt to open a region decoder
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(is, true);
} catch (IOException e) {
Log.w(TAG, "cannot open region decoder for default wallpaper");
}
Bitmap crop = null;
if (decoder != null) {
// Do region decoding to get crop bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
crop = decoder.decodeRegion(roundedTrueCrop, options);
decoder.recycle();
}
if (crop == null) {
// BitmapRegionDecoder has failed, try to crop in-memory. We know at
// this point that openDefaultWallpaper() will return non-null.
is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Bitmap fullSize = null;
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
fullSize = BitmapFactory.decodeStream(is, null, options);
if (fullSize != null) {
crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left, roundedTrueCrop.top, roundedTrueCrop.width(), roundedTrueCrop.height());
}
}
if (crop == null) {
Log.w(TAG, "cannot decode default wallpaper");
return null;
}
// Scale down if necessary
if (outWidth > 0 && outHeight > 0 && (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
Matrix m = new Matrix();
RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
RectF returnRect = new RectF(0, 0, outWidth, outHeight);
m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(), (int) returnRect.height(), Bitmap.Config.ARGB_8888);
if (tmp != null) {
Canvas c = new Canvas(tmp);
Paint p = new Paint();
p.setFilterBitmap(true);
c.drawBitmap(crop, m, p);
crop = tmp;
}
}
return new BitmapDrawable(resources, crop);
}
}
}
Aggregations