com.bumptech.glide.load.resource.bitmap.DownsampleStrategy Java Examples
The following examples show how to use
com.bumptech.glide.load.resource.bitmap.DownsampleStrategy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SvgBitmapDrawableTranscoder.java From SvgGlidePlugins with Apache License 2.0 | 6 votes |
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); } }
Example #2
Source File: BitmapUtil.java From deltachat-android with GNU General Public License v3.0 | 5 votes |
@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); } }
Example #3
Source File: MediaConstraints.java From bcm-android with GNU General Public License v3.0 | 4 votes |
@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); } }
Example #4
Source File: BitmapUtil.java From deltachat-android with GNU General Public License v3.0 | 4 votes |
@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); } }
Example #5
Source File: AlBitmapUtils.java From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License | 4 votes |
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; }