Skip to content

Commit 6856368

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Reduce the screenshots size (facebook#54800)
Summary: # Changelog: [Internal] From my observations, currently every base64 string for encoded screenshot takes on average ~100Kb. This is too much. These changes reduce the size to ~6Kb, at a cost of quality. We can't accomodate such big strings, because there could be hundreds, if not thousands of frames for a ~10-20s trace. Reviewed By: huntie Differential Revision: D88489936
1 parent c800503 commit 6856368

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,21 @@ internal class FrameTimingsObserver(
9292
if (copyResult == PixelCopy.SUCCESS) {
9393
CoroutineScope(Dispatchers.Default).launch {
9494
try {
95-
val scaleFactor = 0.5f
95+
val scaleFactor = 0.25f
9696
val scaledWidth = (width * scaleFactor).toInt()
9797
val scaledHeight = (height * scaleFactor).toInt()
9898
val scaledBitmap =
9999
Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
100100

101101
val outputStream = ByteArrayOutputStream()
102-
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream)
103-
val jpegBytes = outputStream.toByteArray()
104-
val jpegBase64 = Base64.encodeToString(jpegBytes, Base64.NO_WRAP)
105-
continuation.resume(jpegBase64)
102+
val compressFormat =
103+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
104+
Bitmap.CompressFormat.WEBP_LOSSY
105+
else Bitmap.CompressFormat.WEBP
106+
scaledBitmap.compress(compressFormat, 0, outputStream)
107+
val bytes = outputStream.toByteArray()
108+
val base64 = Base64.encodeToString(bytes, Base64.NO_WRAP)
109+
continuation.resume(base64)
106110

107111
scaledBitmap.recycle()
108112
} catch (e: Exception) {

0 commit comments

Comments
 (0)