use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class LocalExifThumbnailProducer method buildEncodedImage.
private EncodedImage buildEncodedImage(PooledByteBuffer imageBytes, ExifInterface exifInterface) {
Pair<Integer, Integer> dimensions = BitmapUtil.decodeDimensions(new PooledByteBufferInputStream(imageBytes));
int rotationAngle = this.getRotationAngle(exifInterface);
int width = dimensions != null ? dimensions.first : EncodedImage.UNKNOWN_WIDTH;
int height = dimensions != null ? dimensions.second : EncodedImage.UNKNOWN_HEIGHT;
EncodedImage encodedImage;
CloseableReference<PooledByteBuffer> closeableByteBuffer = CloseableReference.of(imageBytes);
try {
encodedImage = new EncodedImage(closeableByteBuffer);
} finally {
CloseableReference.closeSafely(closeableByteBuffer);
}
encodedImage.setImageFormat(DefaultImageFormats.JPEG);
encodedImage.setRotationAngle(rotationAngle);
encodedImage.setWidth(width);
encodedImage.setHeight(height);
return encodedImage;
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class WebpTranscodeProducer method transcodeLastResult.
private void transcodeLastResult(final EncodedImage originalResult, final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
Preconditions.checkNotNull(originalResult);
final EncodedImage encodedImageCopy = EncodedImage.cloneOrNull(originalResult);
final StatefulProducerRunnable<EncodedImage> runnable = new StatefulProducerRunnable<EncodedImage>(consumer, producerContext.getProducerListener(), producerContext, PRODUCER_NAME) {
@Override
protected EncodedImage getResult() throws Exception {
PooledByteBufferOutputStream outputStream = mPooledByteBufferFactory.newOutputStream();
try {
// NULLSAFE_FIXME[Parameter Not Nullable]
doTranscode(encodedImageCopy, outputStream);
CloseableReference<PooledByteBuffer> ref = CloseableReference.of(outputStream.toByteBuffer());
try {
EncodedImage encodedImage = new EncodedImage(ref);
// NULLSAFE_FIXME[Parameter Not Nullable]
encodedImage.copyMetaDataFrom(encodedImageCopy);
return encodedImage;
} finally {
CloseableReference.closeSafely(ref);
}
} finally {
outputStream.close();
}
}
@Override
protected void disposeResult(EncodedImage result) {
EncodedImage.closeSafely(result);
}
@Override
protected void onSuccess(EncodedImage result) {
EncodedImage.closeSafely(encodedImageCopy);
super.onSuccess(result);
}
@Override
protected void onFailure(Exception e) {
EncodedImage.closeSafely(encodedImageCopy);
super.onFailure(e);
}
@Override
protected void onCancellation() {
EncodedImage.closeSafely(encodedImageCopy);
super.onCancellation();
}
};
mExecutor.execute(runnable);
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class EncodedMemoryCacheProducer method produceResults.
@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
try {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("EncodedMemoryCacheProducer#produceResults");
}
final ProducerListener2 listener = producerContext.getProducerListener();
listener.onProducerStart(producerContext, PRODUCER_NAME);
final ImageRequest imageRequest = producerContext.getImageRequest();
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, producerContext.getCallerContext());
final boolean isEncodedCacheEnabledForRead = producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.ENCODED_READ);
CloseableReference<PooledByteBuffer> cachedReference = isEncodedCacheEnabledForRead ? mMemoryCache.get(cacheKey) : null;
try {
if (cachedReference != null) {
EncodedImage cachedEncodedImage = new EncodedImage(cachedReference);
try {
listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, true);
producerContext.putOriginExtra("memory_encoded");
consumer.onProgressUpdate(1f);
consumer.onNewResult(cachedEncodedImage, Consumer.IS_LAST);
return;
} finally {
EncodedImage.closeSafely(cachedEncodedImage);
}
}
if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE.getValue()) {
listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
listener.onUltimateProducerReached(producerContext, PRODUCER_NAME, false);
producerContext.putOriginExtra("memory_encoded", "nil-result");
consumer.onNewResult(null, Consumer.IS_LAST);
return;
}
Consumer consumerOfInputProducer = new EncodedMemoryCacheConsumer(consumer, mMemoryCache, cacheKey, producerContext.getImageRequest().isCacheEnabled(ImageRequest.CachesLocationsMasks.ENCODED_WRITE), producerContext.getImagePipelineConfig().getExperiments().isEncodedCacheEnabled());
listener.onProducerFinishWithSuccess(producerContext, PRODUCER_NAME, listener.requiresExtraMap(producerContext, PRODUCER_NAME) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
mInputProducer.produceResults(consumerOfInputProducer, producerContext);
} finally {
CloseableReference.closeSafely(cachedReference);
}
} finally {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
}
}
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class LocalContentUriThumbnailFetchProducer method getCameraImage.
@Nullable
private EncodedImage getCameraImage(Uri uri, @Nullable ResizeOptions resizeOptions) throws IOException {
if (resizeOptions == null) {
return null;
}
@Nullable Cursor cursor = mContentResolver.query(uri, PROJECTION, null, null, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
final int imageIdColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
final EncodedImage thumbnail = this.getThumbnail(resizeOptions, cursor.getLong(imageIdColumnIndex));
if (thumbnail != null) {
final String pathname = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
thumbnail.setRotationAngle(getRotationAngle(pathname));
return thumbnail;
}
}
} finally {
cursor.close();
}
return null;
}
use of com.facebook.imagepipeline.image.EncodedImage in project fresco by facebook.
the class HoneycombBitmapCreator method createNakedBitmap.
@Override
public Bitmap createNakedBitmap(int width, int height, Bitmap.Config bitmapConfig) {
CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate((short) width, (short) height);
EncodedImage encodedImage = null;
CloseableReference<byte[]> encodedBytesArrayRef = null;
try {
encodedImage = new EncodedImage(jpgRef);
encodedImage.setImageFormat(DefaultImageFormats.JPEG);
BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
int length = jpgRef.get().size();
final PooledByteBuffer pooledByteBuffer = jpgRef.get();
encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
byte[] encodedBytesArray = encodedBytesArrayRef.get();
pooledByteBuffer.read(0, encodedBytesArray, 0, length);
Bitmap bitmap = Preconditions.checkNotNull(BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options));
bitmap.setHasAlpha(true);
bitmap.eraseColor(Color.TRANSPARENT);
return bitmap;
} finally {
CloseableReference.closeSafely(encodedBytesArrayRef);
EncodedImage.closeSafely(encodedImage);
CloseableReference.closeSafely(jpgRef);
}
}
Aggregations