Showing
1 changed file
with
35 additions
and
1 deletions
| @@ -22,6 +22,10 @@ import dev.steenbakker.mobile_scanner.utils.YuvToRgbConverter | @@ -22,6 +22,10 @@ import dev.steenbakker.mobile_scanner.utils.YuvToRgbConverter | ||
| 22 | import io.flutter.view.TextureRegistry | 22 | import io.flutter.view.TextureRegistry |
| 23 | import java.io.ByteArrayOutputStream | 23 | import java.io.ByteArrayOutputStream |
| 24 | import kotlin.math.roundToInt | 24 | import kotlin.math.roundToInt |
| 25 | +import android.util.Size | ||
| 26 | +import android.hardware.display.DisplayManager | ||
| 27 | +import android.view.WindowManager | ||
| 28 | +import android.content.Context | ||
| 25 | 29 | ||
| 26 | 30 | ||
| 27 | class MobileScanner( | 31 | class MobileScanner( |
| @@ -166,6 +170,24 @@ class MobileScanner( | @@ -166,6 +170,24 @@ class MobileScanner( | ||
| 166 | return scaledScanWindow.contains(barcodeBoundingBox) | 170 | return scaledScanWindow.contains(barcodeBoundingBox) |
| 167 | } | 171 | } |
| 168 | 172 | ||
| 173 | + // Return the best resolution for the actual device orientation. | ||
| 174 | + // By default camera set its resolution to width 480 and height 640 which is too low for ML KIT. | ||
| 175 | + // If we return an higher resolution than device can handle, camera package take the most relavant one available. | ||
| 176 | + // Resolution set must take care of device orientation to preserve aspect ratio. | ||
| 177 | + private fun getResolution(windowManager: WindowManager): Size { | ||
| 178 | + val rotation = windowManager.defaultDisplay.rotation | ||
| 179 | + val widthMaxRes = 480 * 4; | ||
| 180 | + val heightMaxRes = 640 * 4; | ||
| 181 | + | ||
| 182 | + val targetResolution = if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { | ||
| 183 | + Size(widthMaxRes, heightMaxRes) // Portrait mode | ||
| 184 | + } else { | ||
| 185 | + Size(heightMaxRes, widthMaxRes) // Landscape mode | ||
| 186 | + } | ||
| 187 | + return targetResolution | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + | ||
| 169 | /** | 191 | /** |
| 170 | * Start barcode scanning by initializing the camera and barcode scanner. | 192 | * Start barcode scanning by initializing the camera and barcode scanner. |
| 171 | */ | 193 | */ |
| @@ -229,7 +251,19 @@ class MobileScanner( | @@ -229,7 +251,19 @@ class MobileScanner( | ||
| 229 | // Build the analyzer to be passed on to MLKit | 251 | // Build the analyzer to be passed on to MLKit |
| 230 | val analysisBuilder = ImageAnalysis.Builder() | 252 | val analysisBuilder = ImageAnalysis.Builder() |
| 231 | .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) | 253 | .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) |
| 232 | -// analysisBuilder.setTargetResolution(Size(1440, 1920)) | 254 | + val displayManager = activity.applicationContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager |
| 255 | + val windowManager = activity.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager | ||
| 256 | + // Set initial resolution | ||
| 257 | + analysisBuilder.setTargetResolution(getResolution(windowManager)) | ||
| 258 | + // Listen future orientation | ||
| 259 | + displayManager.registerDisplayListener(object : DisplayManager.DisplayListener { | ||
| 260 | + override fun onDisplayAdded(displayId: Int) {} | ||
| 261 | + override fun onDisplayRemoved(displayId: Int) {} | ||
| 262 | + override fun onDisplayChanged(displayId: Int) { | ||
| 263 | + analysisBuilder.setTargetResolution(getResolution(windowManager)) | ||
| 264 | + } | ||
| 265 | + }, null) | ||
| 266 | + | ||
| 233 | val analysis = analysisBuilder.build().apply { setAnalyzer(executor, captureOutput) } | 267 | val analysis = analysisBuilder.build().apply { setAnalyzer(executor, captureOutput) } |
| 234 | 268 | ||
| 235 | camera = cameraProvider!!.bindToLifecycle( | 269 | camera = cameraProvider!!.bindToLifecycle( |
-
Please register or login to post a comment