Improve MMS attachment quality

Fixes #1551
This commit is contained in:
Moez Bhatti
2020-01-08 18:59:46 -05:00
parent 72a61f5b44
commit 0f21fc80c2
10 changed files with 114 additions and 215 deletions
@@ -34,6 +34,7 @@ class GlideAppModule : AppGlideModule() {
}
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
// registry.prepend(GifDrawable::class.java, ReEncodingGifResourceEncoder(context, glide.bitmapPool))
}
}
@@ -19,77 +19,35 @@
package com.moez.QKSMS.util
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import java.io.ByteArrayOutputStream
object ImageUtils {
fun compressGif(context: Context, uri: Uri, maxBytes: Int): ByteArray {
val request = GlideApp
fun getScaledGif(context: Context, uri: Uri, maxWidth: Int, maxHeight: Int, quality: Int = 90): ByteArray {
val gif = GlideApp
.with(context)
.asGif()
.load(uri)
.transform(CenterCrop())
.centerInside()
.encodeQuality(quality)
.submit(maxWidth, maxHeight)
.get()
val gif = request.submit().get()
val width = gif.firstFrame.width
val height = gif.firstFrame.height
val stream = ByteArrayOutputStream()
GifEncoder(context, GlideApp.get(context).bitmapPool)
.encodeTransformedToStream(gif, stream)
val unscaledBytes = stream.size().toDouble()
var attempts = 0
var bytes = unscaledBytes
while (maxBytes > 0 && bytes > maxBytes) {
val scale = Math.sqrt(maxBytes / unscaledBytes) * (1 - attempts * 0.1)
val scaledGif = request.submit((width * scale).toInt(), (height * scale).toInt()).get()
stream.reset()
GifEncoder(context, GlideApp.get(context).bitmapPool)
.encodeTransformedToStream(scaledGif, stream)
attempts++
bytes = stream.size().toDouble()
}
return stream.toByteArray()
val outputStream = ByteArrayOutputStream()
GifEncoder(context, GlideApp.get(context).bitmapPool).encodeTransformedToStream(gif, outputStream)
return outputStream.toByteArray()
}
fun compressBitmap(src: Bitmap, maxBytes: Int): ByteArray {
val quality = 90
val height = src.height
val width = src.width
val stream = ByteArrayOutputStream()
src.compress(Bitmap.CompressFormat.JPEG, quality, stream)
val unscaledBytes = stream.size().toDouble()
// Based on the byte size of the bitmap, we'll try to reduce the image's dimensions such
// that it will fit within the max byte size set. If we don't get it right the first time,
// use a slightly heavier compression until we fit within the max size
var attempts = 0
var bytes = unscaledBytes
while (maxBytes > 0 && bytes > maxBytes) {
val scale = Math.sqrt(maxBytes / unscaledBytes) * (1 - attempts * 0.1)
stream.reset()
Bitmap.createScaledBitmap(src, (width * scale).toInt(), (height * scale).toInt(), true)
.compress(Bitmap.CompressFormat.JPEG, quality, stream)
attempts++
bytes = stream.size().toDouble()
}
return stream.toByteArray().also { src.recycle() }
fun getScaledImage(context: Context, uri: Uri, maxWidth: Int, maxHeight: Int, quality: Int = 90): ByteArray {
return GlideApp
.with(context)
.`as`(ByteArray::class.java)
.load(uri)
.centerInside()
.encodeQuality(quality)
.submit(maxWidth, maxHeight)
.get()
}
}