1
1
package dev.steenbakker.mobile_scanner
2
2
3
3
import android.app.Activity
4
+ import android.content.Context
5
+ import android.hardware.camera2.CameraCharacteristics
6
+ import android.hardware.camera2.CameraManager
4
7
import android.net.Uri
5
8
import android.os.Handler
6
9
import android.os.Looper
@@ -18,6 +21,7 @@ import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
18
21
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
19
22
import io.flutter.view.TextureRegistry
20
23
import java.io.File
24
+ import com.google.mlkit.vision.barcode.ZoomSuggestionOptions
21
25
22
26
class MobileScannerHandler (
23
27
private val activity : Activity ,
@@ -138,13 +142,14 @@ class MobileScannerHandler(
138
142
val timeout: Int = call.argument<Int >(" timeout" ) ? : 250
139
143
val cameraResolutionValues: List <Int >? = call.argument<List <Int >>(" cameraResolution" )
140
144
val useNewCameraSelector: Boolean = call.argument<Boolean >(" useNewCameraSelector" ) ? : false
145
+ val enableAutoZoom: Boolean = call.argument<Boolean >(" autoZoom" ) ? : false
141
146
val cameraResolution: Size ? = if (cameraResolutionValues != null ) {
142
147
Size (cameraResolutionValues[0 ], cameraResolutionValues[1 ])
143
148
} else {
144
149
null
145
150
}
146
151
147
- val barcodeScannerOptions: BarcodeScannerOptions ? = buildBarcodeScannerOptions(formats)
152
+ val barcodeScannerOptions: BarcodeScannerOptions ? = buildBarcodeScannerOptions(formats, enableAutoZoom )
148
153
149
154
val position =
150
155
if (facing == 0 ) CameraSelector .DEFAULT_FRONT_CAMERA else CameraSelector .DEFAULT_BACK_CAMERA
@@ -230,7 +235,7 @@ class MobileScannerHandler(
230
235
231
236
mobileScanner!! .analyzeImage(
232
237
Uri .fromFile(File (filePath)),
233
- buildBarcodeScannerOptions(formats),
238
+ buildBarcodeScannerOptions(formats, false ),
234
239
analyzeImageSuccessCallback,
235
240
analyzeImageErrorCallback)
236
241
}
@@ -253,6 +258,14 @@ class MobileScannerHandler(
253
258
}
254
259
}
255
260
261
+ private fun setZoomRatio (scale : Float ) : Boolean {
262
+ try {
263
+ mobileScanner!! .setZoomRatio(scale.toDouble())
264
+ return true
265
+ } catch (e: ZoomWhenStopped ) { }
266
+ return false
267
+ }
268
+
256
269
private fun resetScale (result : MethodChannel .Result ) {
257
270
try {
258
271
mobileScanner!! .resetScale()
@@ -269,25 +282,54 @@ class MobileScannerHandler(
269
282
result.success(null )
270
283
}
271
284
272
- private fun buildBarcodeScannerOptions (formats : List <Int >? ): BarcodeScannerOptions ? {
285
+ private fun buildBarcodeScannerOptions (formats : List <Int >? , enableAutoZoom : Boolean ): BarcodeScannerOptions ? {
286
+ val builder : BarcodeScannerOptions .Builder ?
273
287
if (formats == null ) {
274
- return null
288
+ builder = BarcodeScannerOptions .Builder ()
289
+ } else {
290
+ val formatsList: MutableList <Int > = mutableListOf ()
291
+
292
+ for (formatValue in formats) {
293
+ formatsList.add(BarcodeFormats .fromRawValue(formatValue).intValue)
294
+ }
295
+
296
+ if (formatsList.size == 1 ) {
297
+ builder = BarcodeScannerOptions .Builder ().setBarcodeFormats(formatsList.first())
298
+ } else {
299
+ builder = BarcodeScannerOptions .Builder ().setBarcodeFormats(
300
+ formatsList.first(),
301
+ * formatsList.subList(1 , formatsList.size).toIntArray()
302
+ )
303
+ }
275
304
}
276
305
277
- val formatsList: MutableList <Int > = mutableListOf ()
278
-
279
- for (formatValue in formats) {
280
- formatsList.add(BarcodeFormats .fromRawValue(formatValue).intValue)
306
+ if (enableAutoZoom) {
307
+ builder.setZoomSuggestionOptions(
308
+ ZoomSuggestionOptions .Builder {
309
+ setZoomRatio(it)
310
+ }.setMaxSupportedZoomRatio(getMaxZoomRatio())
311
+ .build())
281
312
}
282
313
283
- if (formatsList.size == 1 ) {
284
- return BarcodeScannerOptions .Builder ().setBarcodeFormats(formatsList.first())
285
- .build()
286
- }
314
+ return builder.build()
315
+ }
316
+
317
+ private fun getMaxZoomRatio (): Float {
318
+ val cameraManager = activity.getSystemService(Context .CAMERA_SERVICE ) as CameraManager
319
+ var maxZoom = 1.0F
320
+
321
+ try {
322
+ for (cameraId in cameraManager.cameraIdList) {
323
+ val characteristics = cameraManager.getCameraCharacteristics(cameraId)
287
324
288
- return BarcodeScannerOptions .Builder ().setBarcodeFormats(
289
- formatsList.first(),
290
- * formatsList.subList(1 , formatsList.size).toIntArray()
291
- ).build()
325
+ val maxZoomRatio = characteristics.get(CameraCharacteristics .SCALER_AVAILABLE_MAX_DIGITAL_ZOOM )
326
+ if (maxZoomRatio != null && maxZoomRatio > maxZoom) {
327
+ maxZoom = maxZoomRatio
328
+ }
329
+ }
330
+ } catch (e: Exception ) {
331
+ e.printStackTrace()
332
+ }
333
+ return maxZoom
292
334
}
293
335
}
0 commit comments