1package com.bumptech.glide; 2 3import android.graphics.Bitmap; 4import android.os.ParcelFileDescriptor; 5 6import com.bumptech.glide.load.model.ImageVideoModelLoader; 7import com.bumptech.glide.load.model.ImageVideoWrapper; 8import com.bumptech.glide.load.model.ModelLoader; 9import com.bumptech.glide.load.resource.transcode.BitmapBytesTranscoder; 10import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; 11import com.bumptech.glide.provider.DataLoadProvider; 12import com.bumptech.glide.provider.FixedLoadProvider; 13 14import java.io.InputStream; 15 16/** 17 * A class for creating a load request that either loads an {@link Bitmap} directly or that adds an 18 * {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} to transcode the {@link Bitmap} into another 19 * resource type. 20 * 21 * @paramThe type of model to load the {@link Bitmap} or transcoded class from. 22 */ 23public class BitmapTypeRequest<ModelType> extends BitmapRequestBuilder<ModelType, Bitmap> { 24 private final ModelLoader<ModelType, InputStream> streamModelLoader; 25 private final ModelLoader<ModelType, ParcelFileDescriptor> fileDescriptorModelLoader; 26 private final Glide glide; 27 private final RequestManager.OptionsApplier optionsApplier; 28 29 private static <A, R> FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R> buildProvider(Glide glide, 30 ModelLoader<A, InputStream> streamModelLoader, 31 ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, 32 Class<R> transcodedClass, ResourceTranscoder<Bitmap, R> transcoder) { 33 if (streamModelLoader == null && fileDescriptorModelLoader == null) { 34 return null; 35 } 36 37 if (transcoder == null) { 38 transcoder = glide.buildTranscoder(Bitmap.class, transcodedClass); 39 } 40 DataLoadProvider<ImageVideoWrapper, Bitmap> loadProvider = glide.buildDataProvider(ImageVideoWrapper.class, 41 Bitmap.class); 42 ImageVideoModelLoader<A> modelLoader = new ImageVideoModelLoader<A>(streamModelLoader, 43 fileDescriptorModelLoader); 44 45 return new FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R>(modelLoader, transcoder, loadProvider); 46 } 47 48 BitmapTypeRequest(GenericRequestBuilder<ModelType, ?, ?, ?> other, 49 ModelLoader<ModelType, InputStream> streamModelLoader, 50 ModelLoader<ModelType, ParcelFileDescriptor> fileDescriptorModelLoader, 51 RequestManager.OptionsApplier optionsApplier) { 52 super(buildProvider(other.glide, streamModelLoader, fileDescriptorModelLoader, Bitmap.class, null), 53 Bitmap.class, other); 54 this.streamModelLoader = streamModelLoader; 55 this.fileDescriptorModelLoader = fileDescriptorModelLoader; 56 this.glide = other.glide; 57 this.optionsApplier = optionsApplier; 58 } 59 60 /** 61 * Sets a transcoder to transcode the decoded and transformed {@link Bitmap} into another resource type. 62 * 63 * @param transcoder The transoder to use. 64 * @param transcodeClass The {@link Class} of the resource the {@link Bitmap} will be transcoded to. 65 * @param <R> The type of the resource the {@link Bitmap} will be transcoded to. 66 * @return This request builder. 67 */ 68 public <R> BitmapRequestBuilder<ModelType, R> transcode(ResourceTranscoder<Bitmap, R> transcoder, 69 Class<R> transcodeClass) { 70 return optionsApplier.apply(new BitmapRequestBuilder<ModelType, R>( 71 buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, transcodeClass, transcoder), 72 transcodeClass, this)); 73 } 74 75 /** 76 * Transcodes the decoded and transformed {@link Bitmap} to bytes by compressing it as a JPEG to a byte array. 77 * array. 78 * 79 * @see #toBytes(android.graphics.Bitmap.CompressFormat, int) 80 * 81 * @return This request builder. 82 */ 83 public BitmapRequestBuilder<ModelType, byte[]> toBytes() { 84 return transcode(new BitmapBytesTranscoder(), byte[].class); 85 } 86 87 /** 88 * Transcodes the decoded and transformed {@link android.graphics.Bitmap} to bytes by compressing it using the 89 * given format and quality to a byte array. 90 * 91 * @see android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) 92 * @see #toBytes() 93 * 94 * @param compressFormat The {@link android.graphics.Bitmap.CompressFormat} to use to compress the {@link Bitmap}. 95 * @param quality The quality level from 0-100 to use to compress the {@link Bitmap}. 96 * @return This request builder. 97 */ 98 public BitmapRequestBuilder<ModelType, byte[]> toBytes(Bitmap.CompressFormat compressFormat, int quality) { 99 return transcode(new BitmapBytesTranscoder(compressFormat, quality), byte[].class); 100 } 101} 102