下面列出了怎么用com.bumptech.glide.load.resource.bitmap.DownsampleStrategy的API类实例代码及写法,或者点击链接到github查看源代码。
private void prepareSvg(@NonNull Resource<SVG> toTranscode, @Nullable Options options) {
if (!(toTranscode instanceof SvgResource)) {
return;
}
DownsampleStrategy strategy =
options == null ? null : options.get(DownsampleStrategy.OPTION);
if (strategy != null) {
float scaleFactor = strategy.getScaleFactor(
Math.round(toTranscode.get().getDocumentWidth()),
Math.round(toTranscode.get().getDocumentHeight()),
((SvgResource) toTranscode).getWidth(),
((SvgResource) toTranscode).getHeight()
);
SvgUtils.scaleDocumentSize(toTranscode.get(), scaleFactor);
}
}
@WorkerThread
public static <T> Bitmap createScaledBitmap(Context context, T model, int maxWidth, int maxHeight)
throws BitmapDecodingException
{
try {
return GlideApp.with(context.getApplicationContext())
.asBitmap()
.load(model)
.downsample(DownsampleStrategy.AT_MOST)
.submit(maxWidth, maxHeight)
.get();
} catch (InterruptedException | ExecutionException e) {
throw new BitmapDecodingException(e);
}
}
@WorkerThread
private <T> byte[] createScaledBytes(Context context, T model, MediaConstraints constraints)
throws BitmapDecodingException {
try {
int quality = MAX_COMPRESSION_QUALITY;
int attempts = 0;
byte[] bytes;
Bitmap scaledBitmap = GlideApp.with(context.getApplicationContext())
.asBitmap()
.load(model)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.downsample(DownsampleStrategy.AT_MOST)
.submit(constraints.getImageMaxWidth(context),
constraints.getImageMaxWidth(context))
.get();
if (scaledBitmap == null) {
throw new BitmapDecodingException("Unable to decode image");
}
try {
do {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
bytes = baos.toByteArray();
Log.w(TAG, "iteration with quality " + quality + " size " + (bytes.length / 1024) + "kb");
if (quality == MIN_COMPRESSION_QUALITY) break;
int nextQuality = (int) Math.floor(quality * Math.sqrt((double) constraints.getImageMaxSize(context) / bytes.length));
if (quality - nextQuality < MIN_COMPRESSION_QUALITY_DECREASE) {
nextQuality = quality - MIN_COMPRESSION_QUALITY_DECREASE;
}
quality = Math.max(nextQuality, MIN_COMPRESSION_QUALITY);
}
while (bytes.length > constraints.getImageMaxSize(context) && attempts++ < MAX_COMPRESSION_ATTEMPTS);
if (bytes.length > constraints.getImageMaxSize(context)) {
throw new BitmapDecodingException("Unable to scale image below: " + bytes.length);
}
Log.w(TAG, "createScaledBytes(" + model.toString() + ") -> quality " + Math.min(quality, MAX_COMPRESSION_QUALITY) + ", " + attempts + " attempt(s)");
return bytes;
} finally {
scaledBitmap.recycle();
}
} catch (InterruptedException | ExecutionException e) {
throw new BitmapDecodingException(e);
}
}
@WorkerThread
public static <T> ScaleResult createScaledBytes(Context context, T model, int maxImageWidth, int maxImageHeight, int maxImageSize)
throws BitmapDecodingException
{
try {
int quality = MAX_COMPRESSION_QUALITY;
int attempts = 0;
byte[] bytes;
Bitmap scaledBitmap = GlideApp.with(context.getApplicationContext())
.asBitmap()
.load(model)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.downsample(DownsampleStrategy.AT_MOST)
.submit(maxImageWidth, maxImageHeight)
.get();
if (scaledBitmap == null) {
throw new BitmapDecodingException("Unable to decode image");
}
Log.i(TAG, "Initial scaled bitmap has size of " + scaledBitmap.getByteCount() + " bytes.");
try {
do {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitmap.compress(CompressFormat.JPEG, quality, baos);
bytes = baos.toByteArray();
Log.w(TAG, "iteration with quality " + quality + " size " + (bytes.length / 1024) + "kb");
if (quality == MIN_COMPRESSION_QUALITY) break;
int nextQuality = (int)Math.floor(quality * Math.sqrt((double)maxImageSize / bytes.length));
if (quality - nextQuality < MIN_COMPRESSION_QUALITY_DECREASE) {
nextQuality = quality - MIN_COMPRESSION_QUALITY_DECREASE;
}
quality = Math.max(nextQuality, MIN_COMPRESSION_QUALITY);
}
while (bytes.length > maxImageSize && attempts++ < MAX_COMPRESSION_ATTEMPTS);
if (bytes.length > maxImageSize) {
throw new BitmapDecodingException("Unable to scale image below: " + bytes.length);
}
if (bytes.length <= 0) {
throw new BitmapDecodingException("Decoding failed. Bitmap has a length of " + bytes.length + " bytes.");
}
Log.w(TAG, "createScaledBytes(" + model.toString() + ") -> quality " + Math.min(quality, MAX_COMPRESSION_QUALITY) + ", " + attempts + " attempt(s)");
return new ScaleResult(bytes, scaledBitmap.getWidth(), scaledBitmap.getHeight());
} finally {
if (scaledBitmap != null) scaledBitmap.recycle();
}
} catch (InterruptedException | ExecutionException e) {
throw new BitmapDecodingException(e);
}
}
public static boolean compress(Uri uri, File file, FragmentActivity fragmentActivity, Context context) {
if (uri == null || file == null) {
return false;
}
byte[] bytes;
Bitmap scaledBitMap;
try {
RequestOptions req = new RequestOptions();
req.downsample(DownsampleStrategy.AT_MOST);
Drawable drawable = null;
if (fragmentActivity != null) {
drawable = Glide.with(fragmentActivity).load(uri).apply(req).submit(600, 600).get();
} else if (context != null) {
drawable = Glide.with(context).load(uri).apply(req).submit(600, 600).get();
}
if (drawable == null) {
return false;
}
scaledBitMap = ((BitmapDrawable) drawable).getBitmap();
if (scaledBitMap != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
scaledBitMap.compress(Bitmap.CompressFormat.JPEG, MAX_COMPRESSION_QUALITY, baos);
bytes = baos.toByteArray();
File sizeOfFile = new File(file.getAbsolutePath());
String path = sizeOfFile.getAbsolutePath();
if (sizeOfFile.exists()) {
boolean deleted = sizeOfFile.delete();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
bos.write(bytes);
bos.flush();
bos.close();
return true;
}
} catch (Exception e) {
Log.i(TAG, "Got error in compression :" + e.getMessage());
}
return false;
}