Search in sources :

Example 26 with ResizeOptions

use of com.facebook.imagepipeline.common.ResizeOptions 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 27 with ResizeOptions

use of com.facebook.imagepipeline.common.ResizeOptions in project fresco by facebook.

the class ResizeAndRotateProducer method getSoftwareNumerator.

private static int getSoftwareNumerator(ImageRequest imageRequest, EncodedImage encodedImage, boolean resizingEnabled) {
    if (!resizingEnabled) {
        return JpegTranscoder.SCALE_DENOMINATOR;
    }
    final ResizeOptions resizeOptions = imageRequest.getResizeOptions();
    if (resizeOptions == null) {
        return JpegTranscoder.SCALE_DENOMINATOR;
    }
    final int rotationAngle = getRotationAngle(imageRequest.getRotationOptions(), encodedImage);
    final boolean swapDimensions = rotationAngle == 90 || rotationAngle == 270;
    final int widthAfterRotation = swapDimensions ? encodedImage.getHeight() : encodedImage.getWidth();
    final int heightAfterRotation = swapDimensions ? encodedImage.getWidth() : encodedImage.getHeight();
    float ratio = determineResizeRatio(resizeOptions, widthAfterRotation, heightAfterRotation);
    int numerator = roundNumerator(ratio, resizeOptions.roundUpFraction);
    if (numerator > MAX_JPEG_SCALE_NUMERATOR) {
        return MAX_JPEG_SCALE_NUMERATOR;
    }
    return (numerator < 1) ? 1 : numerator;
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions)

Example 28 with ResizeOptions

use of com.facebook.imagepipeline.common.ResizeOptions in project fresco by facebook.

the class MediaVariationsFragment method loadMainImage.

private void loadMainImage() {
    // Request a non-existent image to force fallback to the variations
    Uri uri = Uri.parse(String.format(URI_TEMPLATE, "full"));
    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).setMediaVariations(getMediaVariationsForMode(mMode)).setResizeOptions(new ResizeOptions(mMainImageDraweeView.getWidth(), mMainImageDraweeView.getHeight())).build();
    Log.i(TAG, request.getMediaVariations() == null ? "null" : request.getMediaVariations().toString());
    setDraweeControllerForRequest(request);
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) Uri(android.net.Uri)

Example 29 with ResizeOptions

use of com.facebook.imagepipeline.common.ResizeOptions in project fresco by facebook.

the class MediaVariationsFragment method loadThumb.

private void loadThumb(SimpleDraweeView draweeView, Size size, ImageRequest.RequestLevel requestLevel) {
    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(size.uri).setMediaVariations(MediaVariations.newBuilderForMediaId(MEDIA_ID).setForceRequestForSpecifiedUri(true).build()).setLowestPermittedRequestLevel(requestLevel).setResizeOptions(new ResizeOptions(draweeView.getWidth(), draweeView.getHeight())).build();
    DraweeController controller = Fresco.newDraweeControllerBuilder().setImageRequest(request).setOldController(draweeView.getController()).build();
    draweeView.setController(controller);
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) DraweeController(com.facebook.drawee.interfaces.DraweeController) ImageRequest(com.facebook.imagepipeline.request.ImageRequest)

Example 30 with ResizeOptions

use of com.facebook.imagepipeline.common.ResizeOptions in project fresco by facebook.

the class MediaVariationsFallbackProducerTest method testSendsSmallestLargerFinalImageToConsumerWhenLargerVariantsFromIndexFound.

@Test
public void testSendsSmallestLargerFinalImageToConsumerWhenLargerVariantsFromIndexFound() {
    when(mImageRequest.getMediaVariations()).thenReturn(mMediaVariationsWithVariants);
    whenCacheContains(mDefaultBufferedDiskCache, CACHE_KEY_S, CACHE_KEY_M, CACHE_KEY_L);
    whenIndexDbContains(URI_M, SIZE_M, CacheChoice.DEFAULT);
    when(mImageRequest.getResizeOptions()).thenReturn(new ResizeOptions(SIZE_M - 10, SIZE_M - 10));
    mMediaVariationsFallbackProducer.produceResults(mConsumer, mProducerContext);
    verify(mConsumer).onNewResult(mImageM, true);
    verify(mConsumer).onProgressUpdate(1L);
    verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
    verifySuccessSentToListener(FOUND, USED_AS_LAST, MediaVariations.SOURCE_IMAGE_REQUEST, VARIANTS_COUNT);
    verifyZeroInteractions(mInputProducer, mSmallImageBufferedDiskCache, mMediaIdExtractor);
}
Also used : ResizeOptions(com.facebook.imagepipeline.common.ResizeOptions) Test(org.junit.Test)

Aggregations

ResizeOptions (com.facebook.imagepipeline.common.ResizeOptions)59 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)27 Uri (android.net.Uri)12 Test (org.junit.Test)11 DraweeController (com.facebook.drawee.interfaces.DraweeController)10 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)7 PipelineDraweeController (com.facebook.drawee.backends.pipeline.PipelineDraweeController)5 File (java.io.File)5 PipelineDraweeControllerBuilder (com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder)4 Drawable (android.graphics.drawable.Drawable)3 CloseableReference (com.facebook.common.references.CloseableReference)3 GenericDraweeHierarchy (com.facebook.drawee.generic.GenericDraweeHierarchy)3 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)3 FileSystemReference (im.actor.runtime.files.FileSystemReference)3 Context (android.content.Context)2 Canvas (android.graphics.Canvas)2 Point (android.graphics.Point)2 Animatable (android.graphics.drawable.Animatable)2 View (android.view.View)2 Spinner (android.widget.Spinner)2