Search in sources :

Example 16 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class EncodedImageTest method testParseMetaData_JPEG.

@Test
public void testParseMetaData_JPEG() throws IOException {
    PooledByteBuffer buf = new TrivialPooledByteBuffer(ByteStreams.toByteArray(EncodedImageTest.class.getResourceAsStream("images/image.jpg")));
    EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
    encodedImage.parseMetaData();
    assertSame(DefaultImageFormats.JPEG, encodedImage.getImageFormat());
    assertEquals(550, encodedImage.getWidth());
    assertEquals(468, encodedImage.getHeight());
    assertEquals(0, encodedImage.getRotationAngle());
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)

Example 17 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class EncodedImageTest method testParseMetaData_PNG.

@Test
public void testParseMetaData_PNG() throws IOException {
    PooledByteBuffer buf = new TrivialPooledByteBuffer(ByteStreams.toByteArray(EncodedImageTest.class.getResourceAsStream("images/image.png")));
    EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
    encodedImage.parseMetaData();
    assertSame(DefaultImageFormats.PNG, encodedImage.getImageFormat());
    assertEquals(800, encodedImage.getWidth());
    assertEquals(600, encodedImage.getHeight());
    assertEquals(0, encodedImage.getRotationAngle());
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)

Example 18 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project DevRing by LJYcoder.

the class FrescoManager method downLoadImage.

@Override
public void downLoadImage(Context context, String url, final File saveFile, final ImageListener<File> 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<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(imageRequest, context);
    dataSource.subscribe(new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {

        @Override
        public void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
            if (!dataSource.isFinished()) {
                return;
            }
            CloseableReference<PooledByteBuffer> imageReference = dataSource.getResult();
            if (imageReference != null) {
                final CloseableReference<PooledByteBuffer> closeableReference = imageReference.clone();
                try {
                    PooledByteBuffer pooledByteBuffer = closeableReference.get();
                    InputStream inputStream = new PooledByteBufferInputStream(pooledByteBuffer);
                    OutputStream outputStream = new FileOutputStream(saveFile);
                    if (FileUtil.saveFile(inputStream, outputStream) && imageListener != null) {
                        imageListener.onSuccess(saveFile);
                    }
                } catch (Exception e) {
                    if (imageListener != null) {
                        imageListener.onFail(e);
                    }
                    e.printStackTrace();
                } finally {
                    imageReference.close();
                    closeableReference.close();
                }
            }
        }

        @Override
        public void onProgressUpdate(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
            int progress = (int) (dataSource.getProgress() * 100);
            RingLog.d("fresco下载图片进度:" + progress);
        }

        @Override
        public void onFailureImpl(DataSource dataSource) {
            Throwable throwable = dataSource.getFailureCause();
            if (imageListener != null) {
                imageListener.onFail(throwable);
            }
        }
    }, Executors.newSingleThreadExecutor());
}
Also used : PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CloseableReference(com.facebook.common.references.CloseableReference) Uri(android.net.Uri) DataSource(com.facebook.datasource.DataSource) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) FileOutputStream(java.io.FileOutputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder)

Example 19 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer 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 20 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class LocalExifThumbnailProducer method buildEncodedImage.

private EncodedImage buildEncodedImage(PooledByteBuffer imageBytes, ExifInterface exifInterface) {
    Pair<Integer, Integer> dimensions = BitmapUtil.decodeDimensions(new PooledByteBufferInputStream(imageBytes));
    int rotationAngle = getRotationAngle(exifInterface);
    int width = dimensions != null ? dimensions.first : EncodedImage.UNKNOWN_WIDTH;
    int height = dimensions != null ? dimensions.second : EncodedImage.UNKNOWN_HEIGHT;
    EncodedImage encodedImage;
    CloseableReference<PooledByteBuffer> closeableByteBuffer = CloseableReference.of(imageBytes);
    try {
        encodedImage = new EncodedImage(closeableByteBuffer);
    } finally {
        CloseableReference.closeSafely(closeableByteBuffer);
    }
    encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    encodedImage.setRotationAngle(rotationAngle);
    encodedImage.setWidth(width);
    encodedImage.setHeight(height);
    return encodedImage;
}
Also used : PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)34 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)14 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)7 InputStream (java.io.InputStream)7 Bitmap (android.graphics.Bitmap)6 BitmapFactory (android.graphics.BitmapFactory)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 TargetApi (android.annotation.TargetApi)2 Uri (android.net.Uri)2 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)2 AnimatedImage (com.facebook.imagepipeline.animated.base.AnimatedImage)2 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)2 BitmapCounter (com.facebook.imagepipeline.memory.BitmapCounter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 Rect (android.graphics.Rect)1 ExifInterface (android.media.ExifInterface)1 BinaryResource (com.facebook.binaryresource.BinaryResource)1 CacheKey (com.facebook.cache.common.CacheKey)1 PooledByteBufferOutputStream (com.facebook.common.memory.PooledByteBufferOutputStream)1