Merge pull request #589 from Naveen3Singh/improve_compressor

Improve image compression
This commit is contained in:
Tibor Kaputa 2023-03-04 20:34:26 +01:00 committed by GitHub
commit 758d538e64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 92 deletions

View File

@ -33,6 +33,7 @@ import android.view.inputmethod.EditorInfo
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
@ -1100,6 +1101,23 @@ class ThreadActivity : SimpleActivity() {
return
}
val mimeType = contentResolver.getType(uri)
if (mimeType == null) {
toast(R.string.unknown_error_occurred)
return
}
val isImage = mimeType.isImageMimeType()
val isGif = mimeType.isGifMimeType()
if (isGif || !isImage) {
// is it assumed that images will always be compressed below the max MMS size limit
val fileSize = getFileSizeFromUri(uri)
val mmsFileSizeLimit = config.mmsFileSizeLimit
if (mmsFileSizeLimit != FILE_SIZE_NONE && fileSize > mmsFileSizeLimit) {
toast(R.string.attachment_sized_exceeds_max_limit, length = Toast.LENGTH_LONG)
return
}
}
var adapter = getAttachmentsAdapter()
if (adapter == null) {
adapter = AttachmentsAdapter(
@ -1115,17 +1133,12 @@ class ThreadActivity : SimpleActivity() {
}
thread_attachments_recyclerview.beVisible()
val mimeType = contentResolver.getType(uri)
if (mimeType == null) {
toast(R.string.unknown_error_occurred)
return
}
val attachment = AttachmentSelection(
id = id,
uri = uri,
mimetype = mimeType,
filename = getFilenameFromUri(uri),
isPending = mimeType.isImageMimeType() && !mimeType.isGifMimeType()
isPending = isImage && !isGif
)
adapter.addAttachment(attachment)
checkSendMessageAvailability()
@ -1228,8 +1241,9 @@ class ThreadActivity : SimpleActivity() {
sendMessageCompat(text, addresses, subscriptionId, attachments)
ensureBackgroundThread {
val messageIds = messages.map { it.id }
val message = getMessages(threadId, getImageResolutions = true, limit = 1).firstOrNull { it.id !in messageIds }
if (message != null) {
val messages = getMessages(threadId, getImageResolutions = true, limit = maxOf(1, attachments.size))
.filter { it.id !in messageIds }
for (message in messages) {
insertOrUpdateMessage(message)
}
}

View File

@ -260,8 +260,9 @@ class ThreadAdapter(
holder.viewClicked(message)
}
thread_mesage_attachments_holder.removeAllViews()
if (message.attachment?.attachments?.isNotEmpty() == true) {
thread_mesage_attachments_holder.beVisible()
thread_mesage_attachments_holder.removeAllViews()
for (attachment in message.attachment.attachments) {
val mimetype = attachment.mimetype
when {
@ -272,6 +273,8 @@ class ThreadAdapter(
thread_message_play_outline.beVisibleIf(mimetype.startsWith("video/"))
}
} else {
thread_mesage_attachments_holder.beGone()
}
}
}

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.smsmessenger.extensions.getFileSizeFromUri
import com.simplemobiletools.smsmessenger.extensions.isImageMimeType
import java.io.File
import java.io.FileOutputStream
import kotlin.math.roundToInt
/**
* Compress image to a given size based on
@ -28,7 +29,11 @@ class ImageCompressor(private val context: Context) {
}
}
fun compressImage(uri: Uri, compressSize: Long, callback: (compressedFileUri: Uri?) -> Unit) {
private val minQuality = 30
private val minResolution = 56
private val scaleStepFactor = 0.6f // increase for more accurate file size at the cost increased computation
fun compressImage(uri: Uri, compressSize: Long, lossy: Boolean = compressSize < FILE_SIZE_1_MB, callback: (compressedFileUri: Uri?) -> Unit) {
ensureBackgroundThread {
try {
val fileSize = context.getFileSizeFromUri(uri)
@ -36,28 +41,49 @@ class ImageCompressor(private val context: Context) {
val mimeType = contentResolver.getType(uri)!!
if (mimeType.isImageMimeType()) {
val byteArray = contentResolver.openInputStream(uri)?.readBytes()!!
var destinationFile = File(outputDirectory, System.currentTimeMillis().toString().plus(mimeType.getExtensionFromMimeType()))
destinationFile.writeBytes(byteArray)
val sizeConstraint = SizeConstraint(compressSize)
val bitmap = loadBitmap(destinationFile)
var imageFile = File(outputDirectory, System.currentTimeMillis().toString().plus(mimeType.getExtensionFromMimeType()))
imageFile.writeBytes(byteArray)
val bitmap = loadBitmap(imageFile)
val format = if (lossy) {
Bitmap.CompressFormat.JPEG
} else {
imageFile.path.getCompressionFormat()
}
// if image weight > * 2 targeted size: cut down resolution by 2
if (fileSize > 2 * compressSize) {
val resConstraint = ResolutionConstraint(bitmap.width / 2, bitmap.height / 2)
while (resConstraint.isSatisfied(destinationFile).not()) {
destinationFile = resConstraint.satisfy(destinationFile)
// This quality approximation mostly works for smaller images but will fail with larger images.
val compressionRatio = compressSize / fileSize.toDouble()
val quality = maxOf((compressionRatio * 100).roundToInt(), minQuality)
imageFile = overWrite(imageFile, bitmap, format = format, quality = quality)
// Even the highest quality images start to look ugly if we use 10 as the minimum quality,
// so we better save some image quality and change resolution instead. This is time consuming
// and mostly needed for very large images. Since there's no reliable way to predict the
// required resolution, we'll just iterate and find the best result.
if (imageFile.length() > compressSize) {
var scaledWidth = bitmap.width
var scaledHeight = bitmap.height
while (imageFile.length() > compressSize) {
scaledWidth = (scaledWidth * scaleStepFactor).roundToInt()
scaledHeight = (scaledHeight * scaleStepFactor).roundToInt()
if (scaledHeight < minResolution && scaledWidth < minResolution) {
break
}
imageFile = decodeSampledBitmapFromFile(imageFile, scaledWidth, scaledHeight).run {
determineImageRotation(imageFile, bitmap = this).run {
overWrite(imageFile, bitmap = this, format = format, quality = quality)
}
}
}
}
// do compression
while (sizeConstraint.isSatisfied(destinationFile).not()) {
destinationFile = sizeConstraint.satisfy(destinationFile)
}
callback.invoke(context.getMyFileUri(destinationFile))
callback.invoke(context.getMyFileUri(imageFile))
} else {
callback.invoke(null)
}
} else {
//no need to compress since the file is less than the compress size
// no need to compress since the file is less than the compress size
callback.invoke(uri)
}
} catch (e: Exception) {
@ -107,77 +133,36 @@ class ImageCompressor(private val context: Context) {
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
private inner class SizeConstraint(
private val maxFileSize: Long,
private val stepSize: Int = 10,
private val maxIteration: Int = 10,
private val minQuality: Int = 10
) {
private var iteration: Int = 0
private fun decodeSampledBitmapFromFile(imageFile: File, reqWidth: Int, reqHeight: Int): Bitmap {
return BitmapFactory.Options().run {
inJustDecodeBounds = true
BitmapFactory.decodeFile(imageFile.absolutePath, this)
fun isSatisfied(imageFile: File): Boolean {
// If size requirement is not met and maxIteration is reached
if (iteration >= maxIteration && imageFile.length() >= maxFileSize) {
throw Exception("Unable to compress image to targeted size")
}
return imageFile.length() <= maxFileSize
}
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
fun satisfy(imageFile: File): File {
iteration++
val quality = (100 - iteration * stepSize).takeIf { it >= minQuality } ?: minQuality
return overWrite(imageFile, loadBitmap(imageFile), quality = quality)
inJustDecodeBounds = false
BitmapFactory.decodeFile(imageFile.absolutePath, this)
}
}
private inner class ResolutionConstraint(private val width: Int, private val height: Int) {
private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
// Raw height and width of image
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
private fun decodeSampledBitmapFromFile(imageFile: File, reqWidth: Int, reqHeight: Int): Bitmap {
return BitmapFactory.Options().run {
inJustDecodeBounds = true
BitmapFactory.decodeFile(imageFile.absolutePath, this)
if (height > reqHeight || width > reqWidth) {
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
val halfHeight: Int = height / 2
val halfWidth: Int = width / 2
inJustDecodeBounds = false
BitmapFactory.decodeFile(imageFile.absolutePath, this)
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
inSampleSize *= 2
}
}
private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
// Raw height and width of image
val (height: Int, width: Int) = options.run { outHeight to outWidth }
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val halfHeight: Int = height / 2
val halfWidth: Int = width / 2
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
inSampleSize *= 2
}
}
return inSampleSize
}
fun isSatisfied(imageFile: File): Boolean {
return BitmapFactory.Options().run {
inJustDecodeBounds = true
BitmapFactory.decodeFile(imageFile.absolutePath, this)
calculateInSampleSize(this, width, height) <= 1
}
}
fun satisfy(imageFile: File): File {
return decodeSampledBitmapFromFile(imageFile, width, height).run {
determineImageRotation(imageFile, this).run {
overWrite(imageFile, this)
}
}
}
return inSampleSize
}
}

View File

@ -40,7 +40,16 @@ fun Context.sendMessageCompat(text: String, addresses: List<String>, subId: Int?
val isMms = attachments.isNotEmpty() || isLongMmsMessage(text, settings) || addresses.size > 1 && settings.group
if (isMms) {
messagingUtils.sendMmsMessage(text, addresses, attachments, settings)
// we send all MMS attachments separately to reduces the chances of hitting provider MMS limit.
if (attachments.size > 1) {
for (i in 0 until attachments.lastIndex) {
val attachment = attachments[i]
messagingUtils.sendMmsMessage("", addresses, listOf(attachment), settings)
}
}
val lastAttachment = attachments[attachments.lastIndex]
messagingUtils.sendMmsMessage(text, addresses, listOf(lastAttachment), settings)
} else {
try {
messagingUtils.sendSmsMessage(text, addresses.toSet(), settings.subscriptionId, settings.deliveryReports)

View File

@ -42,9 +42,7 @@ class MmsSentReceiver : SendStatusReceiver() {
}
override fun updateAppDatabase(context: Context, intent: Intent, receiverResultCode: Int) {
if (resultCode == Activity.RESULT_OK) {
refreshMessages()
}
refreshMessages()
}
companion object {

View File

@ -5,5 +5,4 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:paddingBottom="@dimen/medium_margin"
app:shapeAppearanceOverlay="@style/roundedImageView" />

View File

@ -5,7 +5,7 @@
android:id="@+id/thread_message_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginTop="@dimen/small_margin"
android:foreground="@drawable/selector"
android:paddingStart="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
@ -23,6 +23,7 @@
android:id="@+id/thread_mesage_attachments_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/tiny_margin"
android:divider="@drawable/linear_layout_vertical_divider"
android:orientation="vertical"
android:showDividers="middle" />
@ -44,6 +45,7 @@
android:layout_height="wrap_content"
android:layout_below="@+id/thread_mesage_attachments_holder"
android:layout_alignParentEnd="true"
android:layout_marginVertical="@dimen/tiny_margin"
android:autoLink="email|web"
android:background="@drawable/item_sent_background"
android:padding="@dimen/normal_margin"