diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index 376fb64d0f323e3038793ca93942e3ee03597f9d..d65f448be826ce2c021f453466a42a3d01aef5fa 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -26,9 +26,10 @@ import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; import { AbilityAware, AbilityPluginBinding, ByteBuffer } from '@ohos/flutter_ohos/index'; import { UIAbility } from '@kit.AbilityKit'; import { cameraPermission, checkPermissions, requestPermissions } from './CameraPermissions'; -import { customScan, scanBarcode, scanCore } from '@kit.ScanKit'; +import { customScan, detectBarcode, scanBarcode, scanCore } from '@kit.ScanKit'; import EventChannel, { EventSink } from '@ohos/flutter_ohos/src/main/ets/plugin/common/EventChannel'; import { Barcode, BarcodeFormat, BarcodeType } from './Barcode'; +import { fileUri } from '@kit.CoreFileKit'; const TAG: string = "mobile_scanner"; @@ -61,7 +62,6 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab private surfaceId: string | null = null; private binding: FlutterPluginBinding | null = null; private isStart = false; - private cameraWidth: number = 0; private cameraHeight: number = 0; @@ -73,7 +73,8 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab this.binding = binding; this.applicationContext = binding.getApplicationContext(); - this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), "dev.steenbakker.mobile_scanner/scanner/method"); + this.methodChannel = + new MethodChannel(binding.getBinaryMessenger(), "dev.steenbakker.mobile_scanner/scanner/method"); this.methodChannel.setMethodCallHandler(this); this.eventChannel = new EventChannel(binding.getBinaryMessenger(), "dev.steenbakker.mobile_scanner/scanner/event"); @@ -246,7 +247,41 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab } analyzeImage(call: MethodCall, result: MethodResult) { - result.notImplemented() + const path: string = call.args + let inputImage: detectBarcode.InputImage + + if (path.startsWith('file://')) { + inputImage = { + uri: path + } + } else { + inputImage = { + uri: fileUri.getUriFromPath(path) + } + } + + const options: scanBarcode.ScanOptions = { + enableMultiMode: true, + } + + detectBarcode.decode(inputImage, options).then((r) => { + const _r: Barcode[] = r.map(item => { + return { + displayValue: item.originalValue, + rawValue: item.originalValue, + format: BarcodeFormat.unknown, + type: BarcodeType.unknown, + } as Barcode; + }); + this.analyzerCallback(_r); + result.success(_r.length > 0); + }).catch((e: Error) => { + result.error( + "MobileScanner", + `Failed to analyze image, ${e.message}`, + null + ) + }) } setScale(call: MethodCall, result: MethodResult) {