本文整理汇总了Java中android.graphics.BitmapRegionDecoder.recycle方法的典型用法代码示例。如果您正苦于以下问题:Java BitmapRegionDecoder.recycle方法的具体用法?Java BitmapRegionDecoder.recycle怎么用?Java BitmapRegionDecoder.recycle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.BitmapRegionDecoder
的用法示例。
在下文中一共展示了BitmapRegionDecoder.recycle方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateFullImage
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
private void updateFullImage(Path path, Future<BitmapRegionDecoder> future) {
ImageEntry entry = mImageCache.get(path);
if (entry == null || entry.fullImageTask != future) {
BitmapRegionDecoder fullImage = future.get();
if (fullImage != null) fullImage.recycle();
return;
}
entry.fullImageTask = null;
entry.fullImage = future.get();
if (entry.fullImage != null) {
if (path == getPath(mCurrentIndex)) {
updateTileProvider(entry);
mPhotoView.notifyImageChange(0);
}
}
updateImageRequests();
}
示例2: readBitmap
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
private Bitmap readBitmap (final int scalePercentage, final Rect cropRect) throws IOException {
if (100 % scalePercentage != 0) throw new IllegalArgumentException("scalePercentage " + scalePercentage + " is not a int ratio.");
final Options opts = new Options();
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inSampleSize = 100 / scalePercentage;
if (cropRect != null) {
final BitmapRegionDecoder dec = BitmapRegionDecoder.newInstance(openFileDescriptor().getFileDescriptor(), true);
try {
return dec.decodeRegion(cropRect, opts);
}
finally {
dec.recycle();
}
}
return BitmapFactory.decodeFileDescriptor(openFileDescriptor().getFileDescriptor(), null, opts);
}
示例3: getHotseatPalette
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.N)
private Palette getHotseatPalette() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (AndroidVersion.isAtLeastNougat) {
try (ParcelFileDescriptor fd = wallpaperManager
.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
BitmapRegionDecoder decoder = BitmapRegionDecoder
.newInstance(fd.getFileDescriptor(), false);
int height = decoder.getHeight();
Rect decodeRegion = new Rect(0, (int) (height * (1f - HOTSEAT_FRACTION)),
decoder.getWidth(), height);
Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
decoder.recycle();
if (bitmap != null) {
return Palette.from(bitmap).clearFilters().generate();
}
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}
Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
return Palette.from(wallpaper)
.setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)),
wallpaper.getWidth(), wallpaper.getHeight())
.clearFilters()
.generate();
}
示例4: getStatusBarPalette
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.N)
private Palette getStatusBarPalette() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
int statusBarHeight = getResources()
.getDimensionPixelSize(R.dimen.status_bar_height);
if (AndroidVersion.isAtLeastNougat) {
try (ParcelFileDescriptor fd = wallpaperManager
.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
BitmapRegionDecoder decoder = BitmapRegionDecoder
.newInstance(fd.getFileDescriptor(), false);
Rect decodeRegion = new Rect(0, 0,
decoder.getWidth(), statusBarHeight);
Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
decoder.recycle();
if (bitmap != null) {
return Palette.from(bitmap).clearFilters().generate();
}
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}
Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
return Palette.from(wallpaper)
.setRegion(0, 0, wallpaper.getWidth(), statusBarHeight)
.clearFilters()
.generate();
}
示例5: recycle
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
/**
* While there are decoders in the map, wait until each is available before acquiring,
* recycling and removing it. After this is called, any call to {@link #acquire()} will
* block forever, so this call should happen within a write lock, and all calls to
* {@link #acquire()} should be made within a read lock so they cannot end up blocking on
* the semaphore when it has no permits.
*/
private synchronized void recycle() {
while (!decoders.isEmpty()) {
BitmapRegionDecoder decoder = acquire();
decoder.recycle();
decoders.remove(decoder);
}
}
示例6: readBitmapInternal
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
private Bitmap readBitmapInternal(BitmapFactory.Options options, boolean mayUseRegionDecoder,
boolean mayUseWebViewDecoder) {
ImageData imageData = getImageData();
if (imageData.type == ImageType.NOT_IMAGE) {
return null;
}
if (imageData.type != ImageType.IMAGE_SVG) {
Bitmap bitmap = readBitmapSimple(options);
if (bitmap != null) {
return bitmap;
}
if (mayUseRegionDecoder && isRegionDecoderSupported()) {
InputStream input = null;
BitmapRegionDecoder decoder = null;
try {
input = openInputStream();
decoder = BitmapRegionDecoder.newInstance(input, false);
return decoder.decodeRegion(new Rect(0, 0, decoder.getWidth(), decoder.getHeight()), options);
} catch (IOException e) {
Log.persistent().stack(e);
} finally {
IOUtils.close(input);
if (decoder != null) {
decoder.recycle();
}
}
}
}
if (mayUseWebViewDecoder) {
return WebViewBitmapDecoder.loadBitmap(this, options);
}
return null;
}
示例7: decodeSquare
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
private WeakReference<Bitmap> decodeSquare(WeakReference<byte[]> data, BitmapFactory.Options options) {
int size = Math.min(options.outWidth, options.outHeight);
//We need to crop square image from the center of the image
int indent = (Math.max(options.outWidth, options.outHeight) - size) / 2;
Rect rect;
if (options.outHeight >= options.outWidth) {
rect = new Rect(0, indent, size, size + indent);
} else {
rect = new Rect(indent, 0, size + indent, size);
}
options.inJustDecodeBounds = false;
options.inPurgeable = true;
options.inInputShareable = true;
WeakReference<Bitmap> result;
try {
BitmapRegionDecoder regionDecoder = BitmapRegionDecoder.newInstance(data.get(), 0, data.get().length, true);
result = new WeakReference<>(regionDecoder.decodeRegion(rect, options));
regionDecoder.recycle();
} catch (IOException ex) {
Log.e(CropToSquareImageTask.class, "exception creating BitmapRegionDecoder", ex);
throw new RuntimeException("Error creating BitmapRegionDecoder");
}
return result;
}
示例8: doInBackground
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
@Override
protected Bitmap doInBackground (final Void... params) {
LOG.i("Generating preview: s=%s q=%s.", this.dlg.getScale(), this.dlg.getQuality());
try {
final Bitmap scaled = this.srcMetadata.getBitmap(this.dlg.getScale().getPercentage());
final ByteArrayOutputStream compOut = new ByteArrayOutputStream(512 * 1024);
if (scaled.compress(Bitmap.CompressFormat.JPEG, this.dlg.getQuality().getPercentage(), compOut)) {
this.summary
.append(this.srcMetadata.getWidth()).append(" x ").append(this.srcMetadata.getHeight())
.append(" (").append(IoHelper.readableFileSize(this.srcMetadata.getSize())).append(")")
.append(" --> ").append(scaled.getWidth()).append(" x ").append(scaled.getHeight())
.append(" (").append(IoHelper.readableFileSize(compOut.size())).append(")");
final BitmapRegionDecoder dec = BitmapRegionDecoder.newInstance(compOut.toBufferedInputStream(), true);
try {
final int srcW = dec.getWidth();
final int srcH = dec.getHeight();
final int tgtW = this.dlg.getRootView().getWidth(); // FIXME Workaround for ImageView width issue. Fix properly with something like FixedWidthImageView.
final int tgtH = this.imgPreview.getHeight();
final int left = srcW > tgtW ? (srcW - tgtW) / 2 : 0;
final int top = srcH > tgtH ? (srcH - tgtH) / 2 : 0;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
return dec.decodeRegion(new Rect(left, top, left + tgtW, top + tgtH), options);
}
finally {
dec.recycle();
}
}
this.summary.append("Failed to compress image."); //ES
return null;
}
// XXX Many cases of OutOfMemoryError have been reported, particularly on low end hardware.
// Try not to upset the user too much by not dying completely if possible.
catch (final Throwable e) {
LOG.e("Failed to generate preview image.", e);
this.summary.append(e.toString());
return null;
}
}
示例9: decodeStaticImageFromStream
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
protected CloseableReference<Bitmap> decodeStaticImageFromStream(
InputStream inputStream, BitmapFactory.Options options, @Nullable Rect regionToDecode) {
Preconditions.checkNotNull(inputStream);
int targetWidth = options.outWidth;
int targetHeight = options.outHeight;
if (regionToDecode != null) {
targetWidth = regionToDecode.width();
targetHeight = regionToDecode.height();
}
int sizeInBytes =
BitmapUtil.getSizeInByteForBitmap(targetWidth, targetHeight, options.inPreferredConfig);
final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes);
if (bitmapToReuse == null) {
throw new NullPointerException("BitmapPool.get returned null");
}
options.inBitmap = bitmapToReuse;
Bitmap decodedBitmap = null;
ByteBuffer byteBuffer = mDecodeBuffers.acquire();
if (byteBuffer == null) {
byteBuffer = ByteBuffer.allocate(DECODE_BUFFER_SIZE);
}
try {
options.inTempStorage = byteBuffer.array();
if (regionToDecode != null) {
BitmapRegionDecoder bitmapRegionDecoder = null;
try {
bitmapToReuse.reconfigure(targetWidth, targetHeight, options.inPreferredConfig);
bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, true);
decodedBitmap = bitmapRegionDecoder.decodeRegion(regionToDecode, options);
} catch (IOException e) {
FLog.e(TAG, "Could not decode region %s, decoding full bitmap instead.", regionToDecode);
} finally {
if (bitmapRegionDecoder != null) {
bitmapRegionDecoder.recycle();
}
}
}
if (decodedBitmap == null) {
decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
}
} catch (RuntimeException re) {
mBitmapPool.release(bitmapToReuse);
throw re;
} finally {
mDecodeBuffers.release(byteBuffer);
}
if (bitmapToReuse != decodedBitmap) {
mBitmapPool.release(bitmapToReuse);
decodedBitmap.recycle();
throw new IllegalStateException();
}
return CloseableReference.of(decodedBitmap, mBitmapPool);
}
示例10: onSave
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
@Override
public void onSave(final WallpaperPickerActivity a) {
boolean finishActivityWhenDone = true;
BitmapCropTask.OnBitmapCroppedHandler h = new BitmapCropTask.OnBitmapCroppedHandler() {
@Override
public void onBitmapCropped(byte[] imageBytes, Rect hint) {
Bitmap thumb = null;
Point thumbSize = getDefaultThumbnailSize(a.getResources());
if (imageBytes != null) {
// rotation is set to 0 since imageBytes has already been correctly rotated
thumb = createThumbnail(
thumbSize, null, null, imageBytes, null, 0, 0, true);
a.getSavedImages().writeImage(thumb, imageBytes);
} else {
try {
// Generate thumb
Point size = getDefaultThumbnailSize(a.getResources());
Rect finalCropped = new Rect();
Utils.getMaxCropRect(hint.width(), hint.height(), size.x, size.y, false)
.roundOut(finalCropped);
finalCropped.offset(hint.left, hint.top);
InputStream in = a.getContentResolver().openInputStream(mUri);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(in, true);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = finalCropped.width() / size.x;
thumb = decoder.decodeRegion(finalCropped, options);
decoder.recycle();
Utils.closeSilently(in);
if (thumb != null) {
thumb = Bitmap.createScaledBitmap(thumb, size.x, size.y, true);
}
} catch (IOException e) { }
PointF center = a.mCropView.getCenter();
Float[] extras = new Float[] {
a.mCropView.getScale(),
center.x,
center.y
};
a.getSavedImages().writeImage(thumb, mUri, extras);
}
}
};
boolean shouldFadeOutOnFinish = a.getWallpaperParallaxOffset() == 0f;
a.cropImageAndSetWallpaper(mUri, h, finishActivityWhenDone, shouldFadeOutOnFinish);
}
示例11: createClippedBitmap
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
private Bitmap createClippedBitmap() {
if (mSampleSize <= 1) {
return mClipImageView.clip();
}
// 获取缩放位移后的矩阵值
final float[] matrixValues = mClipImageView.getClipMatrixValues();
final float scale = matrixValues[Matrix.MSCALE_X];
final float transX = matrixValues[Matrix.MTRANS_X];
final float transY = matrixValues[Matrix.MTRANS_Y];
// 获取在显示的图片中裁剪的位置
final Rect border = mClipImageView.getClipBorder();
final float cropX = ((-transX + border.left) / scale) * mSampleSize;
final float cropY = ((-transY + border.top) / scale) * mSampleSize;
final float cropWidth = (border.width() / scale) * mSampleSize;
final float cropHeight = (border.height() / scale) * mSampleSize;
// 获取在旋转之前的裁剪位置
final RectF srcRect = new RectF(cropX, cropY, cropX + cropWidth, cropY + cropHeight);
final Rect clipRect = getRealRect(srcRect);
final BitmapFactory.Options ops = new BitmapFactory.Options();
final Matrix outputMatrix = new Matrix();
outputMatrix.setRotate(mDegree);
// 如果裁剪之后的图片宽高仍然太大,则进行缩小
if (mMaxWidth > 0 && cropWidth > mMaxWidth) {
ops.inSampleSize = findBestSample((int) cropWidth, mMaxWidth);
final float outputScale = mMaxWidth / (cropWidth / ops.inSampleSize);
outputMatrix.postScale(outputScale, outputScale);
}
// 裁剪
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(mInput, false);
final Bitmap source = decoder.decodeRegion(clipRect, ops);
recycleImageViewBitmap();
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), outputMatrix, false);
} catch (Exception e) {
return mClipImageView.clip();
} finally {
if (decoder != null && !decoder.isRecycled()) {
decoder.recycle();
}
}
}
示例12: cropImage
import android.graphics.BitmapRegionDecoder; //导入方法依赖的package包/类
public static Bitmap cropImage(InputStream is, int frameWidth, int frameHeight, int flag) {
if(is == null || frameWidth < 0 || frameHeight < 0 || (frameWidth >= frameHeight) ) {
throw new IllegalStateException();
}
long startTimeMs = System.currentTimeMillis();
Bitmap croppedImage = null;
is.mark(0);
BitmapFactory.Options boundOption = new BitmapFactory.Options();
boundOption.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, boundOption);
Log.i(TAG, "original width:" + boundOption.outWidth + ", height:" + boundOption.outHeight);
float frameAspectRatio = (float)frameWidth / frameHeight;
float imageAspectRatio = (float)boundOption.outWidth / boundOption.outHeight;
float scaleRate = 0f;
Rect cropRegion = null;
int cropWidth = boundOption.outWidth, cropHeight = boundOption.outHeight;
Log.i(TAG, "frameAspectRatio:" + frameAspectRatio + ", imageAspectRatio:" + imageAspectRatio);
if(frameAspectRatio >= imageAspectRatio) {
cropHeight = (int)(boundOption.outWidth / frameAspectRatio);
cropRegion = new Rect(0, (boundOption.outHeight - cropHeight) / 2, boundOption.outWidth, (boundOption.outHeight + cropHeight) / 2);
} else {
cropWidth = (int)(boundOption.outHeight * frameAspectRatio);
cropRegion = new Rect((boundOption.outWidth - cropWidth) / 2, 0, (boundOption.outWidth + cropWidth) / 2, boundOption.outHeight);
}
scaleRate = (float)cropWidth / frameWidth;
Log.i(TAG, "cropWidth:" + cropWidth + ", cropHeight:" + cropHeight + ", cropRegion:" + cropRegion + ", scaleRate:" + scaleRate);
BitmapRegionDecoder brd = null;
try {
is.reset();
BitmapFactory.Options imageOption = new BitmapFactory.Options();
imageOption.inJustDecodeBounds = false;
imageOption.inPreferQualityOverSpeed = true;
imageOption.inSampleSize = (int)(scaleRate + 0.5);
Log.i(TAG, "imageOption.inSampleSize:" + imageOption.inSampleSize);
brd = BitmapRegionDecoder.newInstance(is, false);
croppedImage = brd.decodeRegion(cropRegion, imageOption);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} finally {
if(brd != null) {
brd.recycle();
}
}
if(croppedImage != null) {
Log.i(TAG, "croppedImage width:" + croppedImage.getWidth() + ", height:" + croppedImage.getHeight());
}
long stopTimeMs = System.currentTimeMillis();
Log.i(TAG, "cost {" + (stopTimeMs - startTimeMs) + "ms}");
return croppedImage;
}