Search in sources :

Example 41 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class LocalExifThumbnailProducer method produceResults.

@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ProducerListener listener = producerContext.getListener();
    final String requestId = producerContext.getId();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<EncodedImage>(consumer, listener, PRODUCER_NAME, requestId) {

        @Override
        protected EncodedImage getResult() throws Exception {
            final Uri sourceUri = imageRequest.getSourceUri();
            final ExifInterface exifInterface = getExifInterface(sourceUri);
            if (exifInterface == null || !exifInterface.hasThumbnail()) {
                return null;
            }
            byte[] bytes = exifInterface.getThumbnail();
            PooledByteBuffer pooledByteBuffer = mPooledByteBufferFactory.newByteBuffer(bytes);
            return buildEncodedImage(pooledByteBuffer, exifInterface);
        }

        @Override
        protected void disposeResult(EncodedImage result) {
            EncodedImage.closeSafely(result);
        }

        @Override
        protected Map<String, String> getExtraMapOnSuccess(final EncodedImage result) {
            return ImmutableMap.of(CREATED_THUMBNAIL, Boolean.toString(result != null));
        }
    };
    producerContext.addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            cancellableProducerRunnable.cancel();
        }
    });
    mExecutor.execute(cancellableProducerRunnable);
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ExifInterface(android.media.ExifInterface) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) Uri(android.net.Uri) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 42 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class LocalVideoThumbnailProducer method produceResults.

@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
    final ProducerListener listener = producerContext.getListener();
    final String requestId = producerContext.getId();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final StatefulProducerRunnable cancellableProducerRunnable = new StatefulProducerRunnable<CloseableReference<CloseableImage>>(consumer, listener, PRODUCER_NAME, requestId) {

        @Override
        protected CloseableReference<CloseableImage> getResult() throws Exception {
            Bitmap thumbnailBitmap = ThumbnailUtils.createVideoThumbnail(imageRequest.getSourceFile().getPath(), calculateKind(imageRequest));
            if (thumbnailBitmap == null) {
                return null;
            }
            return CloseableReference.<CloseableImage>of(new CloseableStaticBitmap(thumbnailBitmap, SimpleBitmapReleaser.getInstance(), ImmutableQualityInfo.FULL_QUALITY, 0));
        }

        @Override
        protected Map<String, String> getExtraMapOnSuccess(final CloseableReference<CloseableImage> result) {
            return ImmutableMap.of(CREATED_THUMBNAIL, String.valueOf(result != null));
        }

        @Override
        protected void disposeResult(CloseableReference<CloseableImage> result) {
            CloseableReference.closeSafely(result);
        }
    };
    producerContext.addCallbacks(new BaseProducerContextCallbacks() {

        @Override
        public void onCancellationRequested() {
            cancellableProducerRunnable.cancel();
        }
    });
    mExecutor.execute(cancellableProducerRunnable);
}
Also used : Bitmap(android.graphics.Bitmap) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap)

Example 43 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class MediaVariationsFallbackProducer method produceResults.

public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final ResizeOptions resizeOptions = imageRequest.getResizeOptions();
    final MediaVariations mediaVariations = imageRequest.getMediaVariations();
    if (!imageRequest.isDiskCacheEnabled() || resizeOptions == null || resizeOptions.height <= 0 || resizeOptions.width <= 0) {
        startInputProducerWithExistingConsumer(consumer, producerContext);
        return;
    }
    final String mediaId;
    @MediaVariations.Source final String source;
    if (mediaVariations == null) {
        if (mMediaIdExtractor == null) {
            mediaId = null;
            source = null;
        } else {
            mediaId = mMediaIdExtractor.getMediaIdFrom(imageRequest.getSourceUri());
            source = MediaVariations.SOURCE_ID_EXTRACTOR;
        }
    } else {
        mediaId = mediaVariations.getMediaId();
        source = MediaVariations.SOURCE_INDEX_DB;
    }
    if (mediaVariations == null && mediaId == null) {
        startInputProducerWithExistingConsumer(consumer, producerContext);
        return;
    }
    producerContext.getListener().onProducerStart(producerContext.getId(), PRODUCER_NAME);
    final AtomicBoolean isCancelled = new AtomicBoolean(false);
    if (mediaVariations != null && mediaVariations.getVariantsCount() > 0) {
        chooseFromVariants(consumer, producerContext, imageRequest, mediaVariations, resizeOptions, isCancelled);
    } else {
        MediaVariations.Builder mediaVariationsBuilder = MediaVariations.newBuilderForMediaId(mediaId).setForceRequestForSpecifiedUri(mediaVariations != null && mediaVariations.shouldForceRequestForSpecifiedUri()).setSource(source);
        Task<MediaVariations> indexedMediaVariationsTask = mMediaVariationsIndex.getCachedVariants(mediaId, mediaVariationsBuilder);
        indexedMediaVariationsTask.continueWith(new Continuation<MediaVariations, Object>() {

            @Override
            public Object then(Task<MediaVariations> task) throws Exception {
                if (task.isCancelled() || task.isFaulted()) {
                    return task;
                } else {
                    try {
                        if (task.getResult() == null) {
                            startInputProducerWithWrappedConsumer(consumer, producerContext, mediaId);
                            return null;
                        } else {
                            return chooseFromVariants(consumer, producerContext, imageRequest, task.getResult(), resizeOptions, isCancelled);
                        }
                    } catch (Exception e) {
                        return null;
                    }
                }
            }
        });
    }
    subscribeTaskForRequestCancellation(isCancelled, producerContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CancellationException(java.util.concurrent.CancellationException) MediaVariations(com.facebook.imagepipeline.request.MediaVariations)

Example 44 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class MediaVariationsFallbackProducer method attemptCacheReadForVariant.

private Task attemptCacheReadForVariant(final Consumer<EncodedImage> consumer, final ProducerContext producerContext, ImageRequest imageRequest, MediaVariations mediaVariations, List<MediaVariations.Variant> sortedVariants, int index, AtomicBoolean isCancelled) {
    final MediaVariations.Variant variant = sortedVariants.get(index);
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, variant.getUri(), producerContext.getCallerContext());
    final ImageRequest.CacheChoice cacheChoice;
    if (variant.getCacheChoice() == null) {
        cacheChoice = imageRequest.getCacheChoice();
    } else {
        cacheChoice = variant.getCacheChoice();
    }
    final BufferedDiskCache preferredCache = cacheChoice == ImageRequest.CacheChoice.SMALL ? mSmallImageBufferedDiskCache : mDefaultBufferedDiskCache;
    Task<EncodedImage> readTask = preferredCache.get(cacheKey, isCancelled);
    Continuation<EncodedImage, Void> continuation = onFinishDiskReads(consumer, producerContext, imageRequest, mediaVariations, sortedVariants, index, isCancelled);
    return readTask.continueWith(continuation);
}
Also used : BufferedDiskCache(com.facebook.imagepipeline.cache.BufferedDiskCache) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey) MediaVariations(com.facebook.imagepipeline.request.MediaVariations)

Example 45 with ImageRequest

use of com.facebook.imagepipeline.request.ImageRequest in project fresco by facebook.

the class PostprocessedBitmapMemoryCacheProducer method produceResults.

@Override
public void produceResults(final Consumer<CloseableReference<CloseableImage>> consumer, final ProducerContext producerContext) {
    final ProducerListener listener = producerContext.getListener();
    final String requestId = producerContext.getId();
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final Object callerContext = producerContext.getCallerContext();
    // If there's no postprocessor or the postprocessor doesn't require caching, forward results.
    final Postprocessor postprocessor = imageRequest.getPostprocessor();
    if (postprocessor == null || postprocessor.getPostprocessorCacheKey() == null) {
        mInputProducer.produceResults(consumer, producerContext);
        return;
    }
    listener.onProducerStart(requestId, getProducerName());
    final CacheKey cacheKey = mCacheKeyFactory.getPostprocessedBitmapCacheKey(imageRequest, callerContext);
    CloseableReference<CloseableImage> cachedReference = mMemoryCache.get(cacheKey);
    if (cachedReference != null) {
        listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "true") : null);
        consumer.onProgressUpdate(1.0f);
        consumer.onNewResult(cachedReference, true);
        cachedReference.close();
    } else {
        final boolean isRepeatedProcessor = postprocessor instanceof RepeatedPostprocessor;
        Consumer<CloseableReference<CloseableImage>> cachedConsumer = new CachedPostprocessorConsumer(consumer, cacheKey, isRepeatedProcessor, mMemoryCache);
        listener.onProducerFinishWithSuccess(requestId, getProducerName(), listener.requiresExtraMap(requestId) ? ImmutableMap.of(VALUE_FOUND, "false") : null);
        mInputProducer.produceResults(cachedConsumer, producerContext);
    }
}
Also used : CloseableReference(com.facebook.common.references.CloseableReference) Postprocessor(com.facebook.imagepipeline.request.Postprocessor) RepeatedPostprocessor(com.facebook.imagepipeline.request.RepeatedPostprocessor) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) RepeatedPostprocessor(com.facebook.imagepipeline.request.RepeatedPostprocessor) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CacheKey(com.facebook.cache.common.CacheKey)

Aggregations

ImageRequest (com.facebook.imagepipeline.request.ImageRequest)71 DraweeController (com.facebook.drawee.interfaces.DraweeController)22 ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)19 CloseableReference (com.facebook.common.references.CloseableReference)17 Uri (android.net.Uri)13 Bitmap (android.graphics.Bitmap)11 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)11 File (java.io.File)10 CacheKey (com.facebook.cache.common.CacheKey)9 DataSource (com.facebook.datasource.DataSource)8 BaseBitmapDataSubscriber (com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber)8 PipelineDraweeController (com.facebook.drawee.backends.pipeline.PipelineDraweeController)7 Animatable (android.graphics.drawable.Animatable)6 BaseControllerListener (com.facebook.drawee.controller.BaseControllerListener)6 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)6 ImageInfo (com.facebook.imagepipeline.image.ImageInfo)6 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)5 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)5 Nullable (android.support.annotation.Nullable)4 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)4