com.bumptech.glide.load.resource.transcode.BitmapBytesTranscoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glide Show documentation
Show all versions of glide Show documentation
A fast and efficient image loading library for Android focused on smooth scrolling.
package com.bumptech.glide.load.resource.transcode;
import android.graphics.Bitmap;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bytes.BytesResource;
import java.io.ByteArrayOutputStream;
/**
* An {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} that converts {@link
* android.graphics.Bitmap}s into byte arrays using {@link android.graphics.Bitmap#compress
* (android.graphics.Bitmap.CompressFormat,
* int, java.io.OutputStream)}.
*/
public class BitmapBytesTranscoder implements ResourceTranscoder {
private final Bitmap.CompressFormat compressFormat;
private final int quality;
public BitmapBytesTranscoder() {
this(Bitmap.CompressFormat.JPEG, 100);
}
public BitmapBytesTranscoder(Bitmap.CompressFormat compressFormat, int quality) {
this.compressFormat = compressFormat;
this.quality = quality;
}
@Override
public Resource transcode(Resource toTranscode, Options options) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
toTranscode.get().compress(compressFormat, quality, os);
toTranscode.recycle();
return new BytesResource(os.toByteArray());
}
}