Refactor bitmap and make less copies

This commit is contained in:
Matthieu 2021-09-18 13:11:10 +02:00
parent ad0729686b
commit 73fc84ad75
3 changed files with 20 additions and 31 deletions

View File

@ -18,6 +18,7 @@ import com.zomato.photofilters.FilterPack
import com.zomato.photofilters.imageprocessors.Filter
import com.zomato.photofilters.utils.ThumbnailItem
import com.zomato.photofilters.utils.ThumbnailsManager
import org.pixeldroid.app.utils.bitmapFromUri
class FilterListFragment : Fragment() {
@ -56,19 +57,7 @@ class FilterListFragment : Fragment() {
private fun displayImage(bitmap: Bitmap?) {
val r = Runnable {
val tbImage: Bitmap = (if (bitmap == null) {
// TODO: Check that there is no crash for OpenGL reasons on newer versions of Android
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Honor EXIF orientation if API >= 28
ImageDecoder.decodeBitmap(ImageDecoder
.createSource(requireActivity().contentResolver, PhotoEditActivity.imageUri!!))
.copy(Bitmap.Config.ARGB_8888,true)
} else {
// Ignore EXIF orientation otherwise
MediaStore.Images.Media.getBitmap(
requireActivity().contentResolver,
PhotoEditActivity.imageUri
)
}
bitmapFromUri(requireActivity().contentResolver, PhotoEditActivity.imageUri)
} else {
Bitmap.createScaledBitmap(bitmap, 100, 100, false)
})

View File

@ -5,13 +5,10 @@ import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.graphics.Point
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.view.Menu
import android.view.MenuItem
import android.view.View.GONE
@ -30,6 +27,7 @@ import com.zomato.photofilters.imageprocessors.Filter
import com.zomato.photofilters.imageprocessors.subfilters.BrightnessSubFilter
import com.zomato.photofilters.imageprocessors.subfilters.ContrastSubFilter
import com.zomato.photofilters.imageprocessors.subfilters.SaturationSubfilter
import org.pixeldroid.app.utils.bitmapFromUri
import java.io.File
import java.io.IOException
import java.io.OutputStream
@ -116,18 +114,8 @@ class PhotoEditActivity : BaseActivity() {
binding.tabs.setupWithViewPager(binding.viewPager)
}
private fun loadImage() {
// TODO: Check that there is no crash for OpenGL reasons on newer versions of Android
originalImage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Honor EXIF orientation if API >= 28
ImageDecoder
.decodeBitmap(ImageDecoder.createSource(contentResolver, imageUri!!))
.copy(BITMAP_CONFIG,true)
} else {
// Ignore EXIF orientation otherwise
MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
}
originalImage = bitmapFromUri(contentResolver, imageUri)
compressedImage = resizeImage(originalImage!!)
compressedOriginalImage = compressedImage!!.copy(BITMAP_CONFIG, true)

View File

@ -1,13 +1,13 @@
package org.pixeldroid.app.utils
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.*
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.DisplayMetrics
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDelegate
@ -63,6 +63,18 @@ fun normalizeDomain(domain: String): String {
.trim(Char::isWhitespace)
}
fun bitmapFromUri(contentResolver: ContentResolver, uri: Uri?): Bitmap =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder
.decodeBitmap(
ImageDecoder.createSource(contentResolver, uri!!)
)
{ decoder, _, _ -> decoder.isMutableRequired = true }
} else {
//FIXME EXIF orientation is ignored by getBitmap, respect it manually?
MediaStore.Images.Media.getBitmap(contentResolver, uri)
}
fun BaseActivity.openUrl(url: String): Boolean{
val intent = CustomTabsIntent.Builder().build()