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);
}
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;
}
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);
}
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);
}
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);
}
Aggregations