Home | History | Annotate | Download | only in transcode
      1 package com.bumptech.glide.load.resource.transcode;
      2 
      3 import android.graphics.Bitmap;
      4 
      5 import com.bumptech.glide.load.engine.Resource;
      6 import com.bumptech.glide.load.resource.bytes.BytesResource;
      7 
      8 import java.io.ByteArrayOutputStream;
      9 
     10 /**
     11  * An {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} that converts
     12  * {@link android.graphics.Bitmap}s into byte arrays using
     13  * {@link android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)}.
     14  */
     15 public class BitmapBytesTranscoder implements ResourceTranscoder<Bitmap, byte[]> {
     16     private final Bitmap.CompressFormat compressFormat;
     17     private final int quality;
     18 
     19     public BitmapBytesTranscoder() {
     20         this(Bitmap.CompressFormat.JPEG, 100);
     21     }
     22 
     23     public BitmapBytesTranscoder(Bitmap.CompressFormat compressFormat, int quality) {
     24         this.compressFormat = compressFormat;
     25         this.quality = quality;
     26     }
     27 
     28     @Override
     29     public Resource<byte[]> transcode(Resource<Bitmap> toTranscode) {
     30         ByteArrayOutputStream os = new ByteArrayOutputStream();
     31         toTranscode.get().compress(compressFormat, quality, os);
     32         toTranscode.recycle();
     33         return new BytesResource(os.toByteArray());
     34     }
     35 
     36     @Override
     37     public String getId() {
     38         return "BitmapBytesTranscoder.com.bumptech.glide.load.resource.transcode";
     39     }
     40 }
     41