From cc4374d30a10523a5000205caf21455f587acd01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Thu, 12 Oct 2023 21:55:37 +0200 Subject: [PATCH] fix: fix image scaling on android in production --- .../rnmbx/components/images/RNMBXImages.kt | 4 +- .../com/rnmapbox/rnmbx/utils/BitmapUtils.kt | 31 ------ .../rnmbx/utils/DownloadMapImageTask.kt | 95 ++++++------------- 3 files changed, 30 insertions(+), 100 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/images/RNMBXImages.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/images/RNMBXImages.kt index 4040e7c7f..4845c9645 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/images/RNMBXImages.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/images/RNMBXImages.kt @@ -228,8 +228,8 @@ class RNMBXImages(context: Context, private val mManager: RNMBXImagesManager) : var maxY = info.stretchY.maxOfOrNull { max(it.first, it.second) } ?: 0.0f var maxX = info.stretchX.maxOfOrNull { max(it.first, it.second) } ?: 0.0f if (info.content != null) { - maxX = max(max(info.content.left,info.content.right), maxX) - maxY = max(max(info.content.top,info.content.bottom), maxY) + maxX = max(max(info.content.left, info.content.right), maxX) + maxY = max(max(info.content.top, info.content.bottom), maxY) } return emptyImage(ceil(maxX).toInt()+1, ceil(maxY).toInt()+1) } else { diff --git a/android/src/main/java/com/rnmapbox/rnmbx/utils/BitmapUtils.kt b/android/src/main/java/com/rnmapbox/rnmbx/utils/BitmapUtils.kt index 9e2cbcf38..f0d660ee9 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/utils/BitmapUtils.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/utils/BitmapUtils.kt @@ -33,10 +33,6 @@ object BitmapUtils { } } - fun getBitmapFromURL(url: String?): Bitmap? { - return getBitmapFromURL(url, null) - } - fun toImage(bitmap: Bitmap): Image { if (bitmap.config != Bitmap.Config.ARGB_8888) { throw RuntimeException("Only ARGB_8888 bitmap config is supported!") @@ -71,33 +67,6 @@ object BitmapUtils { } } - fun getBitmapFromURL(url: String?, options: BitmapFactory.Options?): Bitmap? { - var bitmap = getImage(url) - if (bitmap != null) { - return bitmap - } - try { - val bitmapStream = URL(url).openStream() - bitmap = BitmapFactory.decodeStream(bitmapStream, null, options) - bitmapStream.close() - addImage(url, bitmap) - } catch (e: Exception) { - Log.w(LOG_TAG, e.localizedMessage) - bitmap = - Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8) // Returns a transparent bitmap - } - return bitmap - } - - fun getBitmapFromResource( - context: Context, - resourceName: String?, - options: BitmapFactory.Options? - ): Bitmap { - val resources = context.resources - val resID = resources.getIdentifier(resourceName, "drawable", context.packageName) - return BitmapFactory.decodeResource(resources, resID, options) - } @Throws(IOException::class) fun createImgTempFile(context: Context, image: Image): String? { diff --git a/android/src/main/java/com/rnmapbox/rnmbx/utils/DownloadMapImageTask.kt b/android/src/main/java/com/rnmapbox/rnmbx/utils/DownloadMapImageTask.kt index 633dccca7..118023942 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/utils/DownloadMapImageTask.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/utils/DownloadMapImageTask.kt @@ -4,11 +4,9 @@ import android.content.Context import android.graphics.Bitmap import com.mapbox.maps.Style import com.mapbox.maps.MapboxMap -import com.rnmapbox.rnmbx.utils.DownloadMapImageTask.OnAllImagesLoaded import android.os.AsyncTask import com.rnmapbox.rnmbx.utils.ImageEntry import android.util.DisplayMetrics -import com.rnmapbox.rnmbx.utils.DownloadMapImageTask import android.graphics.BitmapFactory import android.net.Uri import android.util.Log @@ -22,11 +20,8 @@ import com.facebook.imagepipeline.image.CloseableStaticBitmap import com.facebook.imagepipeline.request.ImageRequestBuilder import com.facebook.react.views.imagehelper.ImageSource import com.rnmapbox.rnmbx.components.images.ImageInfo -import com.rnmapbox.rnmbx.components.images.addBitmapImage import java.io.File import java.lang.ref.WeakReference -import java.util.AbstractMap -import java.util.ArrayList import java.util.HashMap import com.rnmapbox.rnmbx.v11compat.image.* @@ -54,64 +49,40 @@ class DownloadMapImageTask(context: Context, map: MapboxMap, callback: OnAllImag if (uri.startsWith("/")) { uri = Uri.fromFile(File(uri)).toString() } - if (uri.startsWith("http://") || uri.startsWith("https://") || - uri.startsWith("file://") || uri.startsWith("asset://") || uri.startsWith("data:") - ) { - val source = ImageSource(context, uri) - val request = ImageRequestBuilder.newBuilderWithSource(source.uri) - .setRotationOptions(RotationOptions.autoRotate()) - .build() - val dataSource = - Fresco.getImagePipeline().fetchDecodedImage(request, mCallerContext) - var result: CloseableReference? = null - try { - result = DataSources.waitForFinalResult(dataSource) - if (result != null) { - val image = result.get() - if (image is CloseableStaticBitmap) { - val bitmap = - image.underlyingBitmap // Copy the bitmap to make sure it doesn't get recycled when we release - // the fresco reference. - .copy(Bitmap.Config.ARGB_8888, true) - bitmap.density = - (DisplayMetrics.DENSITY_DEFAULT.toDouble() * imageEntry.getScaleOr( - 1.0 - )).toInt() - images.add( - DownloadedImage(name=key, bitmap=bitmap, info=imageEntry.info) - ) - } else { - FLog.e(LOG_TAG, "Failed to load bitmap from: $uri") - } + val source = ImageSource(context, uri) + val request = ImageRequestBuilder.newBuilderWithSource(source.uri) + .setRotationOptions(RotationOptions.autoRotate()) + .build() + val dataSource = + Fresco.getImagePipeline().fetchDecodedImage(request, mCallerContext) + var result: CloseableReference? = null + try { + result = DataSources.waitForFinalResult(dataSource) + if (result != null) { + val image = result.get() + if (image is CloseableStaticBitmap) { + val bitmap = + image.underlyingBitmap // Copy the bitmap to make sure it doesn't get recycled when we release + // the fresco reference. + .copy(Bitmap.Config.ARGB_8888, true) + Log.e("RNMBXImageManager", "downloadImage: $key $uri $image ${image.width}x${image.height}") + bitmap.density = DisplayMetrics.DENSITY_DEFAULT + images.add( + DownloadedImage(name=key, bitmap=bitmap, info=imageEntry.info) + ) } else { FLog.e(LOG_TAG, "Failed to load bitmap from: $uri") } - } catch (e: Throwable) { - Log.w(LOG_TAG, e.localizedMessage) - } finally { - dataSource.close() - if (result != null) { - CloseableReference.closeSafely(result) - } - } - } else { - // local asset required from JS require('image.png') or import icon from 'image.png' while in release mode - val bitmap = BitmapUtils.getBitmapFromResource( - context, - uri, - getBitmapOptions(metrics, imageEntry.info.scale) - ) - if (bitmap != null) { - images.add( - DownloadedImage( - name=key, - bitmap=bitmap, - info=imageEntry.info - ) - ) } else { FLog.e(LOG_TAG, "Failed to load bitmap from: $uri") } + } catch (e: Throwable) { + Log.w(LOG_TAG, e.localizedMessage) + } finally { + dataSource.close() + if (result != null) { + CloseableReference.closeSafely(result) + } } } return images @@ -134,16 +105,6 @@ class DownloadMapImageTask(context: Context, map: MapboxMap, callback: OnAllImag mCallback?.onAllImagesLoaded() } - private fun getBitmapOptions(metrics: DisplayMetrics, scale: Double?): BitmapFactory.Options { - val options = BitmapFactory.Options() - options.inScreenDensity = metrics.densityDpi - options.inTargetDensity = metrics.densityDpi - if (scale != null) { - options.inDensity = (DisplayMetrics.DENSITY_DEFAULT.toDouble() * scale).toInt() - } - return options - } - companion object { const val LOG_TAG = "DownloadMapImageTask" }