Skip to content

Commit 5f3413e

Browse files
Merge pull request #1299 from yujune/fix-android-jank-ui
fix: fix android jank ui when returnImage is true
2 parents 00715cc + 9ef0d36 commit 5f3413e

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.0.4
2+
Bugs fixed:
3+
* [Android] Fixed UI jank when `returnImage` is true.
4+
15
## 6.0.3
26
New features:
37
* Adds pause function to pause the camera but keep textures in place.

android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ dependencies {
7979

8080
implementation 'androidx.camera:camera-lifecycle:1.3.4'
8181
implementation 'androidx.camera:camera-camera2:1.3.4'
82+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
8283

8384
testImplementation 'org.jetbrains.kotlin:kotlin-test'
84-
testImplementation 'org.mockito:mockito-core:5.12.0'
85+
testImplementation 'org.mockito:mockito-core:5.12.0'
8586
}

android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import dev.steenbakker.mobile_scanner.objects.DetectionSpeed
3535
import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters
3636
import dev.steenbakker.mobile_scanner.utils.YuvToRgbConverter
3737
import io.flutter.view.TextureRegistry
38+
import kotlinx.coroutines.CoroutineScope
39+
import kotlinx.coroutines.Dispatchers
40+
import kotlinx.coroutines.coroutineScope
41+
import kotlinx.coroutines.launch
3842
import java.io.ByteArrayOutputStream
3943
import kotlin.math.roundToInt
4044

@@ -97,6 +101,7 @@ class MobileScanner(
97101

98102
if (newScannedBarcodes == lastScanned) {
99103
// New scanned is duplicate, returning
104+
imageProxy.close()
100105
return@addOnSuccessListener
101106
}
102107
if (newScannedBarcodes.isNotEmpty()) {
@@ -118,10 +123,12 @@ class MobileScanner(
118123
}
119124

120125
if (barcodeMap.isEmpty()) {
126+
imageProxy.close()
121127
return@addOnSuccessListener
122128
}
123129

124130
if (!returnImage) {
131+
imageProxy.close()
125132
mobileScannerCallback(
126133
barcodeMap,
127134
null,
@@ -130,31 +137,36 @@ class MobileScanner(
130137
return@addOnSuccessListener
131138
}
132139

133-
val bitmap = Bitmap.createBitmap(mediaImage.width, mediaImage.height, Bitmap.Config.ARGB_8888)
134-
val imageFormat = YuvToRgbConverter(activity.applicationContext)
140+
CoroutineScope(Dispatchers.IO).launch {
141+
val bitmap = Bitmap.createBitmap(mediaImage.width, mediaImage.height, Bitmap.Config.ARGB_8888)
142+
val imageFormat = YuvToRgbConverter(activity.applicationContext)
135143

136-
imageFormat.yuvToRgb(mediaImage, bitmap)
144+
imageFormat.yuvToRgb(mediaImage, bitmap)
137145

138-
val bmResult = rotateBitmap(bitmap, camera?.cameraInfo?.sensorRotationDegrees?.toFloat() ?: 90f)
146+
val bmResult = rotateBitmap(bitmap, camera?.cameraInfo?.sensorRotationDegrees?.toFloat() ?: 90f)
139147

140-
val stream = ByteArrayOutputStream()
141-
bmResult.compress(Bitmap.CompressFormat.PNG, 100, stream)
142-
val byteArray = stream.toByteArray()
143-
val bmWidth = bmResult.width
144-
val bmHeight = bmResult.height
145-
bmResult.recycle()
148+
val stream = ByteArrayOutputStream()
149+
bmResult.compress(Bitmap.CompressFormat.PNG, 100, stream)
150+
val byteArray = stream.toByteArray()
151+
val bmWidth = bmResult.width
152+
val bmHeight = bmResult.height
153+
154+
bmResult.recycle()
155+
imageProxy.close()
156+
157+
mobileScannerCallback(
158+
barcodeMap,
159+
byteArray,
160+
bmWidth,
161+
bmHeight
162+
)
163+
}
146164

147-
mobileScannerCallback(
148-
barcodeMap,
149-
byteArray,
150-
bmWidth,
151-
bmHeight
152-
)
153165
}.addOnFailureListener { e ->
154166
mobileScannerErrorCallback(
155167
e.localizedMessage ?: e.toString()
156168
)
157-
}.addOnCompleteListener { imageProxy.close() }
169+
}
158170
}
159171

160172
if (detectionSpeed == DetectionSpeed.NORMAL) {

0 commit comments

Comments
 (0)