diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index d4100236bd220d1ab1516b64d5cf9564b39e8f89..0ecba601619ec346a4f879a6cc184c6885218882 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -76,6 +76,8 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab private displayHeight: number = 0 // 屏幕高度,单位vp private displayWidth: number = 0 // 屏幕宽度,单位vp + private scanWindow: Array | null = null; // scanWindow,扫描区域 + publishEvent(event: ESObject) { this.eventSink?.success(event) } @@ -249,12 +251,22 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab format: Barcode.convertScanType(item.scanType), type: BarcodeType.unknown, corners: [ - {x: this.scanCodeRect[i]?.left, y: this.scanCodeRect[i]?.top}, - {x: this.scanCodeRect[i]?.right, y: this.scanCodeRect[i]?.top}, - {x: this.scanCodeRect[i]?.left, y: this.scanCodeRect[i]?.bottom}, - {x: this.scanCodeRect[i]?.right, y: this.scanCodeRect[i]?.bottom}, - ] + {x: this.scanCodeRect[i]?.left, y: this.scanCodeRect[i]?.top} as Point, + {x: this.scanCodeRect[i]?.right, y: this.scanCodeRect[i]?.top} as Point, + {x: this.scanCodeRect[i]?.right, y: this.scanCodeRect[i]?.bottom} as Point, + {x: this.scanCodeRect[i]?.left, y: this.scanCodeRect[i]?.bottom} as Point, + ].map(i => ({ + x: i.x / this.scanWidth * this.cameraWidth, + y: i.y / this.scanHeight * this.cameraHeight + }) as Point) } as Barcode; + }).filter(barcode => { + if (this.scanWindow) { + return this.isBarcodeInScanWindow(barcode); + } + else { + return true; + } }); this.callback(_r, null, this.cameraWidth, this.cameraHeight) } @@ -400,7 +412,8 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab } updateScanWindow(call: MethodCall, result: MethodResult) { - result.notImplemented() + this.scanWindow = call.argument("rect") + result.success(true) } callback(barcodes: Barcode[], image: ByteBuffer | null, width: number, height: number) { @@ -425,6 +438,15 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab }) } + private isBarcodeInScanWindow(barcode: Barcode): boolean { + const corners = barcode.corners; + const top = this.scanWindow![1] * this.cameraHeight; + const bottom = this.scanWindow![3] * this.cameraHeight; + const left = this.scanWindow![0] * this.cameraWidth; + const right = this.scanWindow![2] * this.cameraWidth; + return corners.reduce((r, item) => r && item.x > left && item.x < right && item.y > top && item.y < bottom, true) + } + errorCallback(error: string) { this.publishEvent({ "name": "error",