@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.drawee_view); ImageDecodeOptions imageDecodeOptionsWithCustomDecoder = new ImageDecodeOptionsBuilder() .setCustomImageDecoder(CUSTOM_COLOR_DECODER) .build(); AbstractDraweeController controller = Fresco.newDraweeControllerBuilder() .setImageRequest( ImageRequestBuilder.newBuilderWithResourceId(R.raw.custom_color1) .setImageDecodeOptions(imageDecodeOptionsWithCustomDecoder) .build()) .build(); simpleDraweeView.setController(controller); }
/** * Decodes a GIF into a CloseableImage. * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data) * @param options the options for the decode * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps * @return a {@link CloseableImage} for the GIF image */ public CloseableImage decodeGif( final EncodedImage encodedImage, final ImageDecodeOptions options, final Bitmap.Config bitmapConfig) { if (sGifAnimatedImageDecoder == null) { throw new UnsupportedOperationException("To encode animated gif please add the dependency " + "to the animated-gif module"); } final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef(); Preconditions.checkNotNull(bytesRef); try { final PooledByteBuffer input = bytesRef.get(); AnimatedImage gifImage = sGifAnimatedImageDecoder.decode(input.getNativePtr(), input.size()); return getCloseableImage(options, gifImage, bitmapConfig); } finally { CloseableReference.closeSafely(bytesRef); } }
/** * Decode a WebP into a CloseableImage. * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data) * @param options the options for the decode * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps * @return a {@link CloseableImage} for the WebP image */ public CloseableImage decodeWebP( final EncodedImage encodedImage, final ImageDecodeOptions options, final Bitmap.Config bitmapConfig) { if (sWebpAnimatedImageDecoder == null) { throw new UnsupportedOperationException("To encode animated webp please add the dependency " + "to the animated-webp module"); } final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef(); Preconditions.checkNotNull(bytesRef); try { final PooledByteBuffer input = bytesRef.get(); AnimatedImage webPImage = sWebpAnimatedImageDecoder.decode( input.getNativePtr(), input.size()); return getCloseableImage(options, webPImage, bitmapConfig); } finally { CloseableReference.closeSafely(bytesRef); } }
public BitmapMemoryCacheKey( String sourceString, @Nullable ResizeOptions resizeOptions, RotationOptions rotationOptions, ImageDecodeOptions imageDecodeOptions, @Nullable CacheKey postprocessorCacheKey, @Nullable String postprocessorName, Object callerContext) { mSourceString = Preconditions.checkNotNull(sourceString); mResizeOptions = resizeOptions; mRotationOptions = rotationOptions; mImageDecodeOptions = imageDecodeOptions; mPostprocessorCacheKey = postprocessorCacheKey; mPostprocessorName = postprocessorName; mHash = HashCodeUtil.hashCode( sourceString.hashCode(), (resizeOptions != null) ? resizeOptions.hashCode() : 0, rotationOptions.hashCode(), mImageDecodeOptions, mPostprocessorCacheKey, postprocessorName); mCallerContext = callerContext; mCacheTime = RealtimeSinceBootClock.get().now(); }
@Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { ImageFormat imageFormat = encodedImage.getImageFormat(); if (imageFormat == DefaultImageFormats.JPEG) { return decodeJpeg(encodedImage, length, qualityInfo, options); } else if (imageFormat == DefaultImageFormats.GIF) { return decodeGif(encodedImage, length, qualityInfo, options); } else if (imageFormat == DefaultImageFormats.WEBP_ANIMATED) { return decodeAnimatedWebp(encodedImage, length, qualityInfo, options); } else if (imageFormat == ImageFormat.UNKNOWN) { throw new DecodeException("unknown image format", encodedImage); } return decodeStaticImage(encodedImage, options); }
/** * Decodes image. * * @param encodedImage input image (encoded bytes plus meta data) * @param length if image type supports decoding incomplete image then determines where the image * data should be cut for decoding. * @param qualityInfo quality information for the image * @param options options that cange decode behavior */ @Override public CloseableImage decode( final EncodedImage encodedImage, final int length, final QualityInfo qualityInfo, final ImageDecodeOptions options) { if (options.customImageDecoder != null) { return options.customImageDecoder.decode(encodedImage, length, qualityInfo, options); } ImageFormat imageFormat = encodedImage.getImageFormat(); if (imageFormat == null || imageFormat == ImageFormat.UNKNOWN) { imageFormat = ImageFormatChecker.getImageFormat_WrapIOException( encodedImage.getInputStream()); encodedImage.setImageFormat(imageFormat); } if (mCustomDecoders != null) { ImageDecoder decoder = mCustomDecoders.get(imageFormat); if (decoder != null) { return decoder.decode(encodedImage, length, qualityInfo, options); } } return mDefaultDecoder.decode(encodedImage, length, qualityInfo, options); }
/** * Decodes gif into CloseableImage. * * @param encodedImage input image (encoded bytes plus meta data) * @return a CloseableImage */ public CloseableImage decodeGif( final EncodedImage encodedImage, final int length, final QualityInfo qualityInfo, final ImageDecodeOptions options) { InputStream is = encodedImage.getInputStream(); if (is == null) { return null; } try { if (!options.forceStaticImage && mAnimatedGifDecoder != null) { return mAnimatedGifDecoder.decode(encodedImage, length, qualityInfo, options); } return decodeStaticImage(encodedImage, options); } finally { Closeables.closeQuietly(is); } }
/** * @param encodedImage input image (encoded bytes plus meta data) * @return a CloseableStaticBitmap */ public CloseableStaticBitmap decodeStaticImage( final EncodedImage encodedImage, ImageDecodeOptions options) { CloseableReference<Bitmap> bitmapReference = mPlatformDecoder.decodeFromEncodedImage(encodedImage, options.bitmapConfig, null); try { return new CloseableStaticBitmap( bitmapReference, ImmutableQualityInfo.FULL_QUALITY, encodedImage.getRotationAngle(), encodedImage.getExifOrientation()); } finally { bitmapReference.close(); } }
/** * Decodes a partial jpeg. * * @param encodedImage input image (encoded bytes plus meta data) * @param length amount of currently available data in bytes * @param qualityInfo quality info for the image * @return a CloseableStaticBitmap */ public CloseableStaticBitmap decodeJpeg( final EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { CloseableReference<Bitmap> bitmapReference = mPlatformDecoder.decodeJPEGFromEncodedImage( encodedImage, options.bitmapConfig, null, length); try { return new CloseableStaticBitmap( bitmapReference, qualityInfo, encodedImage.getRotationAngle(), encodedImage.getExifOrientation()); } finally { bitmapReference.close(); } }
@Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { InputStream encodedInputStream = null; try { encodedInputStream = encodedImage.getInputStream(); return new CloseableKeyframesImage( KFImageDeserializer.deserialize(encodedInputStream)); } catch (IOException e) { e.printStackTrace(); return null; } finally { Closeables.closeQuietly(encodedInputStream); } }
@Override public ImageDecoder getGifDecoder(final Bitmap.Config bitmapConfig) { return new ImageDecoder() { @Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { return getAnimatedImageFactory().decodeGif(encodedImage, options, bitmapConfig); } }; }
@Override public ImageDecoder getWebPDecoder(final Bitmap.Config bitmapConfig) { return new ImageDecoder() { @Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { return getAnimatedImageFactory().decodeWebP(encodedImage, options, bitmapConfig); } }; }
private CloseableImage getCloseableImage( ImageDecodeOptions options, AnimatedImage image, Bitmap.Config bitmapConfig) { List<CloseableReference<Bitmap>> decodedFrames = null; CloseableReference<Bitmap> previewBitmap = null; try { final int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0; if (options.forceStaticImage) { return new CloseableStaticBitmap( createPreviewBitmap(image, bitmapConfig, frameForPreview), ImmutableQualityInfo.FULL_QUALITY, 0); } if (options.decodeAllFrames) { decodedFrames = decodeAllFrames(image, bitmapConfig); previewBitmap = CloseableReference.cloneOrNull(decodedFrames.get(frameForPreview)); } if (options.decodePreviewFrame && previewBitmap == null) { previewBitmap = createPreviewBitmap(image, bitmapConfig, frameForPreview); } AnimatedImageResult animatedImageResult = AnimatedImageResult.newBuilder(image) .setPreviewBitmap(previewBitmap) .setFrameForPreview(frameForPreview) .setDecodedFrames(decodedFrames) .build(); return new CloseableAnimatedImage(animatedImageResult); } finally { CloseableReference.closeSafely(previewBitmap); CloseableReference.closeSafely(decodedFrames); } }
@Test public void testCreateDefaults() { WebPImage mockWebPImage = mock(WebPImage.class); // Expect a call to WebPImage.create TrivialPooledByteBuffer byteBuffer = createByteBuffer(); when(mWebPImageMock.decode(byteBuffer.getNativePtr(), byteBuffer.size())) .thenReturn(mockWebPImage); EncodedImage encodedImage = new EncodedImage( CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER)); encodedImage.setImageFormat(ImageFormat.UNKNOWN); CloseableAnimatedImage closeableImage = (CloseableAnimatedImage) mAnimatedImageFactory.decodeWebP( encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG); // Verify we got the right result AnimatedImageResult imageResult = closeableImage.getImageResult(); assertSame(mockWebPImage, imageResult.getImage()); assertNull(imageResult.getPreviewBitmap()); assertFalse(imageResult.hasDecodedFrame(0)); // Should not have interacted with these. verifyZeroInteractions(mMockAnimatedDrawableBackendProvider); verifyZeroInteractions(mMockBitmapFactory); }
@Test public void testCreateDefaults() { GifImage mockGifImage = mock(GifImage.class); // Expect a call to GifImage.create TrivialPooledByteBuffer byteBuffer = createByteBuffer(); when(mGifImageMock.decode(byteBuffer.getNativePtr(), byteBuffer.size())) .thenReturn(mockGifImage); EncodedImage encodedImage = new EncodedImage( CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER)); encodedImage.setImageFormat(ImageFormat.UNKNOWN); CloseableAnimatedImage closeableImage = (CloseableAnimatedImage) mAnimatedImageFactory.decodeGif( encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG); // Verify we got the right result AnimatedImageResult imageResult = closeableImage.getImageResult(); assertSame(mockGifImage, imageResult.getImage()); assertNull(imageResult.getPreviewBitmap()); assertFalse(imageResult.hasDecodedFrame(0)); // Should not have interacted with these. verifyZeroInteractions(mMockAnimatedDrawableBackendProvider); verifyZeroInteractions(mMockBitmapFactory); }
@Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { try { SVG svg = SVG.getFromInputStream(encodedImage.getInputStream()); return new CloseableSvgImage(svg); } catch (SVGParseException e) { e.printStackTrace(); } return null; }
@Override public CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) { try { // Read the file as a string String text = new String(ByteStreams.toByteArray(encodedImage.getInputStream())); // Check if the string matches "<color>#" if (!text.startsWith(COLOR_TAG + "#")) { return null; } // Parse the int value between # and < int startIndex = COLOR_TAG.length() + 1; int endIndex = text.lastIndexOf('<'); int color = Integer.parseInt(text.substring(startIndex, endIndex), 16); // Add the alpha component so that we actually see the color color = ColorUtils.setAlphaComponent(color, 255); // Return the CloseableImage return new CloseableColorImage(color); } catch (IOException e) { e.printStackTrace(); } // Return nothing if an error occurred return null; }
@VisibleForTesting protected DataSource<CloseableReference<CloseableImage>> fetchDecodedImage() { ImagePipelineFactory factory; try { factory = ImagePipelineFactory.getInstance(); } catch (NullPointerException e) { // Image pipeline is not initialized ImagePipelineFactory.initialize(mAttachedView.getContext().getApplicationContext()); factory = ImagePipelineFactory.getInstance(); } ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(getImageUri())) .setImageDecodeOptions(ImageDecodeOptions.newBuilder().setDecodePreviewFrame(true).build()) .build(); return factory.getImagePipeline().fetchDecodedImage(request, null); }
public static ImageDecodeOptions getImageDecodeOptions(){ ImageDecodeOptions decodeOptions = ImageDecodeOptions.newBuilder() // .setBackgroundColor(Color.TRANSPARENT)//图片的背景颜色 // .setDecodeAllFrames(decodeAllFrames)//解码所有帧 // .setDecodePreviewFrame(decodePreviewFrame)//解码预览框 // .setForceOldAnimationCode(forceOldAnimationCode)//使用以前动画 // .setFrom(options)//使用已经存在的图像解码 // .setMinDecodeIntervalMs(intervalMs)//最小解码间隔(分位单位) .setUseLastFrameForPreview(true)//使用最后一帧进行预览 .build(); return decodeOptions; }
public static void loadThumbFromUrl(Context context, ImageView imageView, int resizeWidthDp, int resizeHeightDp, String url, String cookie, String referer, ControllerListener controllerListener) { if (TextUtils.isEmpty(url)) { imageView.setImageURI(null); return; } Uri uri = Uri.parse(url); JsonObject header = new JsonObject(); header.addProperty("Cookie", cookie); header.addProperty("Referer", referer); if (url != null && url.startsWith("http")) { if (HProxy.isEnabled() && HProxy.isAllowPicture()) { HProxy proxy = new HProxy(url); header.addProperty(proxy.getHeaderKey(), proxy.getHeaderValue()); MyOkHttpNetworkFetcher.headers.put(uri, getGson().toJson(header)); } MyOkHttpNetworkFetcher.headers.put(uri, getGson().toJson(header)); } if (imageView instanceof SimpleDraweeView) { ImageDecodeOptions imageDecodeOptions = new ImageDecodeOptionsBuilder() .setForceStaticImage(true) .setDecodePreviewFrame(true) .build(); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setResizeOptions(new ResizeOptions(DensityUtil.dp2px(context, resizeWidthDp), DensityUtil.dp2px(context, resizeHeightDp))) .setImageDecodeOptions(imageDecodeOptions) .setLocalThumbnailPreviewsEnabled(true) .build(); DraweeController controller = Fresco.newDraweeControllerBuilder() .setCallerContext(context) .setTapToRetryEnabled(true) .setAutoPlayAnimations(false) .setOldController(((SimpleDraweeView) imageView).getController()) .setControllerListener(controllerListener) .setImageRequest(request) .build(); ((SimpleDraweeView) imageView).setController(controller); } }
/** * Decodes gif into CloseableImage. * * @param encodedImage input image (encoded bytes plus meta data) * @return a CloseableImage */ public CloseableImage decodeGif( final EncodedImage encodedImage, final int length, final QualityInfo qualityInfo, final ImageDecodeOptions options) { if (!options.forceStaticImage && mAnimatedGifDecoder != null) { return mAnimatedGifDecoder.decode(encodedImage, length, qualityInfo, options); } return decodeStaticImage(encodedImage, options); }
public static ImageDecodeOptions getImageDecodeOptions() { ImageDecodeOptions decodeOptions = ImageDecodeOptions.newBuilder() // .setBackgroundColor(Color.TRANSPARENT)//图片的背景颜色 // .setDecodeAllFrames(decodeAllFrames)//解码所有帧 // .setDecodePreviewFrame(decodePreviewFrame)//解码预览框 // .setForceOldAnimationCode(forceOldAnimationCode)//使用以前动画 // .setFrom(options)//使用已经存在的图像解码 // .setMinDecodeIntervalMs(intervalMs)//最小解码间隔(分位单位) .setUseLastFrameForPreview(true)//使用最后一帧进行预览 .build(); return decodeOptions; }
CloseableImage decode( EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options);