Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix image scaling on android in production #3103

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 0 additions & 31 deletions android/src/main/java/com/rnmapbox/rnmbx/utils/BitmapUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*

Expand Down Expand Up @@ -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<CloseableImage>? = 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<CloseableImage>? = 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
Expand All @@ -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"
}
Expand Down
Loading