Search in sources :

Example 1 with CloseableBitmap

use of com.facebook.imagepipeline.image.CloseableBitmap in project DevRing by LJYcoder.

the class FrescoManager method getBitmap.

@Override
public void getBitmap(Context context, String url, final ImageListener<Bitmap> imageListener) {
    // 参考自https://github.com/hpdx/fresco-helper/blob/master/fresco-helper/src/main/java/com/facebook/fresco/helper/ImageLoader.java
    Uri uri = Uri.parse(url);
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
    ImageRequest imageRequest = builder.build();
    DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
    dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() {

        @Override
        public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
            if (!dataSource.isFinished()) {
                return;
            }
            CloseableReference<CloseableImage> imageReference = dataSource.getResult();
            if (imageReference != null) {
                final CloseableReference<CloseableImage> closeableReference = imageReference.clone();
                try {
                    CloseableImage closeableImage = closeableReference.get();
                    // 动图处理
                    if (closeableImage instanceof CloseableAnimatedImage) {
                        AnimatedImageResult animatedImageResult = ((CloseableAnimatedImage) closeableImage).getImageResult();
                        if (animatedImageResult != null && animatedImageResult.getImage() != null) {
                            int imageWidth = animatedImageResult.getImage().getWidth();
                            int imageHeight = animatedImageResult.getImage().getHeight();
                            Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
                            Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, bitmapConfig);
                            animatedImageResult.getImage().getFrame(0).renderFrame(imageWidth, imageHeight, bitmap);
                            if (imageListener != null) {
                                imageListener.onSuccess(bitmap);
                            }
                        }
                    } else // 非动图处理
                    if (closeableImage instanceof CloseableBitmap) {
                        CloseableBitmap closeableBitmap = (CloseableBitmap) closeableImage;
                        Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
                        if (bitmap != null && !bitmap.isRecycled()) {
                            // https://github.com/facebook/fresco/issues/648
                            final Bitmap tempBitmap = bitmap.copy(bitmap.getConfig(), false);
                            if (imageListener != null) {
                                imageListener.onSuccess(tempBitmap);
                            }
                        }
                    }
                } finally {
                    imageReference.close();
                    closeableReference.close();
                }
            }
        }

        @Override
        public void onFailureImpl(DataSource dataSource) {
            Throwable throwable = dataSource.getFailureCause();
            if (imageListener != null) {
                imageListener.onFail(throwable);
            }
        }
    }, UiThreadImmediateExecutorService.getInstance());
}
Also used : CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) DiskCacheConfig(com.facebook.cache.disk.DiskCacheConfig) ImageConfig(com.ljy.devring.image.support.ImageConfig) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) SimpleProgressiveJpegConfig(com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig) CloseableReference(com.facebook.common.references.CloseableReference) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Uri(android.net.Uri) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) DataSource(com.facebook.datasource.DataSource) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Example 2 with CloseableBitmap

use of com.facebook.imagepipeline.image.CloseableBitmap in project fresco by facebook.

the class BaseFrescoStethoPlugin method storeEntries.

private void storeEntries(List<CountingMemoryCacheInspector.DumpInfoEntry<CacheKey, CloseableImage>> entries, int i, PrintStream writer, File directory) throws IOException {
    String filename;
    for (CountingMemoryCacheInspector.DumpInfoEntry<CacheKey, CloseableImage> entry : entries) {
        CloseableImage closeableImage = entry.value.get();
        if (closeableImage instanceof CloseableBitmap) {
            CloseableBitmap closeableBitmap = (CloseableBitmap) closeableImage;
            filename = "tmp" + i + ".png";
            writer.println(formatStrLocaleSafe("Storing image %d as %s. Key: %s", i, filename, entry.key));
            storeImage(closeableBitmap.getUnderlyingBitmap(), new File(directory, filename), Bitmap.CompressFormat.PNG, 100);
        } else {
            writer.println(formatStrLocaleSafe("Image %d has unrecognized type %s. Key: %s", i, closeableImage, entry.key));
        }
        i++;
    }
}
Also used : CloseableImage(com.facebook.imagepipeline.image.CloseableImage) File(java.io.File) CacheKey(com.facebook.cache.common.CacheKey) BitmapMemoryCacheKey(com.facebook.imagepipeline.cache.BitmapMemoryCacheKey) CountingMemoryCacheInspector(com.facebook.imagepipeline.cache.CountingMemoryCacheInspector) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap)

Example 3 with CloseableBitmap

use of com.facebook.imagepipeline.image.CloseableBitmap in project fresco by facebook.

the class BaseBitmapDataSubscriber method onNewResultImpl.

@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
    if (!dataSource.isFinished()) {
        return;
    }
    CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
    Bitmap bitmap = null;
    if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
        bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
    }
    try {
        onNewResultImpl(bitmap);
    } finally {
        CloseableReference.closeSafely(closeableImageRef);
    }
}
Also used : Bitmap(android.graphics.Bitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap)

Example 4 with CloseableBitmap

use of com.facebook.imagepipeline.image.CloseableBitmap in project SherlockAdapter by EvilBT.

the class FrescoUtil method save.

public static Boolean save(@NonNull String url, @NonNull File outputFile) throws IOException {
    ImagePipelineFactory factory = Fresco.getImagePipelineFactory();
    ImagePipeline pipeline = factory.getImagePipeline();
    boolean isInCache = pipeline.isInDiskCacheSync(Uri.parse(url));
    if (isInCache) {
        BinaryResource resource = factory.getMainFileCache().getResource(new SimpleCacheKey(url));
        if (resource instanceof FileBinaryResource) {
            FileBinaryResource fileResource = (FileBinaryResource) resource;
            FileChannel input = new FileInputStream(fileResource.getFile()).getChannel();
            FileChannel output = new FileOutputStream(outputFile).getChannel();
            output.transferFrom(input, 0, input.size());
            input.close();
            output.close();
            return true;
        }
    }
    boolean isMemoryCache = pipeline.isInBitmapMemoryCache(Uri.parse(url));
    if (!isMemoryCache) {
        return false;
    }
    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
    DataSource<CloseableReference<CloseableImage>> dataSource = pipeline.fetchImageFromBitmapCache(request, null);
    if (!dataSource.isFinished()) {
        return false;
    }
    CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
    Bitmap bitmap = null;
    if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
        bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
    }
    if (bitmap == null) {
        return false;
    }
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    outputStream.flush();
    outputStream.close();
    return true;
}
Also used : ImagePipelineFactory(com.facebook.imagepipeline.core.ImagePipelineFactory) FileChannel(java.nio.channels.FileChannel) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileOutputStream(java.io.FileOutputStream) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Aggregations

CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)4 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)4 Bitmap (android.graphics.Bitmap)3 CloseableReference (com.facebook.common.references.CloseableReference)2 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)2 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)2 Uri (android.net.Uri)1 BinaryResource (com.facebook.binaryresource.BinaryResource)1 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)1 CacheKey (com.facebook.cache.common.CacheKey)1 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)1 DiskCacheConfig (com.facebook.cache.disk.DiskCacheConfig)1 DataSource (com.facebook.datasource.DataSource)1 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)1 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)1 CountingMemoryCacheInspector (com.facebook.imagepipeline.cache.CountingMemoryCacheInspector)1 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)1 ImagePipelineFactory (com.facebook.imagepipeline.core.ImagePipelineFactory)1 SimpleProgressiveJpegConfig (com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig)1 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)1