1package com.bumptech.glide.load.resource.bitmap; 2 3import android.content.Context; 4import android.graphics.Bitmap; 5 6import com.bumptech.glide.Glide; 7import com.bumptech.glide.load.DecodeFormat; 8import com.bumptech.glide.load.ResourceDecoder; 9import com.bumptech.glide.load.engine.Resource; 10import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 11 12import java.io.InputStream; 13 14/** 15 * An {@link com.bumptech.glide.load.ResourceDecoder} that uses an 16 * {@link com.bumptech.glide.load.resource.bitmap.Downsampler} to decode an {@link android.graphics.Bitmap} from an 17 * {@link java.io.InputStream}. 18 */ 19public class StreamBitmapDecoder implements ResourceDecoder<InputStream, Bitmap> { 20 private static final String ID = "StreamBitmapDecoder.com.bumptech.glide.load.resource.bitmap"; 21 private final Downsampler downsampler; 22 private BitmapPool bitmapPool; 23 private DecodeFormat decodeFormat; 24 private String id; 25 26 public StreamBitmapDecoder(Context context) { 27 this(Glide.get(context).getBitmapPool()); 28 } 29 30 public StreamBitmapDecoder(BitmapPool bitmapPool) { 31 this(bitmapPool, DecodeFormat.DEFAULT); 32 } 33 34 public StreamBitmapDecoder(Context context, DecodeFormat decodeFormat) { 35 this(Glide.get(context).getBitmapPool(), decodeFormat); 36 } 37 38 public StreamBitmapDecoder(BitmapPool bitmapPool, DecodeFormat decodeFormat) { 39 this(Downsampler.AT_LEAST, bitmapPool, decodeFormat); 40 } 41 42 public StreamBitmapDecoder(Downsampler downsampler, BitmapPool bitmapPool, DecodeFormat decodeFormat) { 43 this.downsampler = downsampler; 44 this.bitmapPool = bitmapPool; 45 this.decodeFormat = decodeFormat; 46 } 47 48 @Override 49 public Resource<Bitmap> decode(InputStream source, int width, int height) { 50 Bitmap bitmap = downsampler.decode(source, bitmapPool, width, height, decodeFormat); 51 return BitmapResource.obtain(bitmap, bitmapPool); 52 } 53 54 @Override 55 public String getId() { 56 if (id == null) { 57 id = new StringBuilder() 58 .append(ID) 59 .append(downsampler.getId()) 60 .append(decodeFormat.name()) 61 .toString(); 62 } 63 return id; 64 } 65} 66