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