Search in sources :

Example 86 with EncodedImage

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;
}
Also used : PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 87 with 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);
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) PooledByteBufferOutputStream(com.facebook.common.memory.PooledByteBufferOutputStream) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 88 with EncodedImage

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();
        }
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 89 with EncodedImage

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;
}
Also used : Cursor(android.database.Cursor) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 90 with EncodedImage

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);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) BitmapFactory(android.graphics.BitmapFactory) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

EncodedImage (com.facebook.imagepipeline.image.EncodedImage)108 Test (org.junit.Test)32 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)20 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)12 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)12 CacheKey (com.facebook.cache.common.CacheKey)11 Rect (android.graphics.Rect)9 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)9 InputStream (java.io.InputStream)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)9 AnimatedDrawableBackend (com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend)8 AnimatedImageCompositor (com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor)8 ImageDecodeOptions (com.facebook.imagepipeline.common.ImageDecodeOptions)8 Before (org.junit.Before)8 Bitmap (android.graphics.Bitmap)7 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)7 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)7 FakeClock (com.facebook.imagepipeline.testing.FakeClock)6 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)6