From 68f2ca2a3536334e4df3cd512fdf5a3ee3e35c6c Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Thu, 19 Sep 2024 15:38:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: keminLuo <541416002@qq.com> --- ohos/src/main/ets/MobileScannerPlugin.ets | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index 704e52c..d4c55e4 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -80,6 +80,8 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab private scanWindow: Array | null = null; // scanWindow,扫描区域 + private imageBuffer: ArrayBuffer | null = null; + publishEvent(event: ESObject) { this.eventSink?.success(event) } @@ -281,7 +283,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab return true; } }); - this.callback(_r, null, this.cameraWidth, this.cameraHeight) + this.callback(_r, this.imageBuffer, this.cameraWidth, this.cameraHeight) } // 返回相机帧的回调 @@ -292,6 +294,18 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab return; } if (frameResult && frameResult.scanCodeRects && frameResult.scanCodeRects.length > 0) { + let sourcePotions: image.SourceOptions = { + sourceDensity: 120, + sourcePixelFormat: image.PixelMapFormat.NV21, + sourceSize: { height: this.cameraHeight, width: this.cameraWidth} + } + let imageResource = image.createImageSource(frameResult.byteBuffer, sourcePotions) + let packer = image.createImagePacker() + let packingOpt: image.PackingOption = { format: "image/jpeg", quality: 100 } + packer.packing(imageResource, packingOpt).then((imgBuffer: ArrayBuffer) => { + this.imageBuffer = imgBuffer; + console.log(TAG, "get imageBuffer success!") + }) if (frameResult.scanCodeRects[0]) { this.scanCodeRect = []; // 码图位置信息转换 @@ -429,16 +443,14 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab result.success(true) } - callback(barcodes: Barcode[], image: ByteBuffer | null, width: number, height: number) { + callback(barcodes: Barcode[], image: ArrayBuffer | null, width: number, height: number) { // 需要先暂停后才能继续识别 customScan.stop().then(() => { - // 连续识别 - this.startScan(this.cameraWidth, this.cameraHeight) if (image) { this.publishEvent({ "name": "barcode", "data": barcodes, - "image": image, + "image": new Uint8Array(image), "width": width, "height": height }) @@ -448,6 +460,8 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab "data": barcodes }) } + // 连续识别 + this.startScan(this.cameraWidth, this.cameraHeight) }) } -- Gitee From 586ce476eb58521a03e379ecba01f32898eae991 Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Thu, 19 Sep 2024 19:50:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- ohos/src/main/ets/MobileScannerPlugin.ets | 46 ++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index d4c55e4..b2b5cde 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -33,7 +33,7 @@ import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; import { display } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { Point } from '@ohos.UiTest'; -import { hasFlashUnit, getCameraManager} from './CameraUtil'; +import { hasFlashUnit, getCameraManager } from './CameraUtil'; import { image } from '@kit.ImageKit'; const TAG: string = "mobile_scanner"; @@ -67,10 +67,8 @@ 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; - private scanWidth: number = 384 // xComponent宽度,默认设置384,单位vp private scanHeight: number = 682 // xComponent高度,默认设置682,单位vp private scanCodeRect: Array = [] // 扫码结果码图位置 @@ -78,7 +76,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab private displayHeight: number = 0 // 屏幕高度,单位vp private displayWidth: number = 0 // 屏幕宽度,单位vp - private scanWindow: Array | null = null; // scanWindow,扫描区域 + private scanWindow: Array | null = null; // scanWindow,扫描区域 private imageBuffer: ArrayBuffer | null = null; @@ -90,7 +88,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"); @@ -180,12 +179,12 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab if (!this.isStart) { this.isStart = true; - let torchable:boolean = hasFlashUnit(this.applicationContext!) + let torchable: boolean = hasFlashUnit(this.applicationContext!) let scanTypes = [scanCore.ScanType.ALL] if (formats) { scanTypes = [] - formats.forEach((format:number) => { + formats.forEach((format: number) => { let type = Barcode.convertToScanType(format) if (type != scanCore.ScanType.FORMAT_UNKNOWN) { scanTypes.push(type) @@ -266,10 +265,10 @@ 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} 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, + { 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 @@ -278,14 +277,12 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab }).filter(barcode => { if (this.scanWindow) { return this.isBarcodeInScanWindow(barcode); - } - else { + } else { return true; } }); this.callback(_r, this.imageBuffer, this.cameraWidth, this.cameraHeight) } - // 返回相机帧的回调 private frameCallback: AsyncCallback = async (error: BusinessError, frameResult: customScan.ScanFrame) => { @@ -297,15 +294,22 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab let sourcePotions: image.SourceOptions = { sourceDensity: 120, sourcePixelFormat: image.PixelMapFormat.NV21, - sourceSize: { height: this.cameraHeight, width: this.cameraWidth} + sourceSize: { height: this.cameraWidth, width: this.cameraHeight } } let imageResource = image.createImageSource(frameResult.byteBuffer, sourcePotions) + let opts: image.InitializationOptions = { + editable: true, + pixelFormat: 3, + size: { height: this.cameraWidth, width: this.cameraHeight } + } + let pixelMap: image.PixelMap = await imageResource.createPixelMap(opts) let packer = image.createImagePacker() let packingOpt: image.PackingOption = { format: "image/jpeg", quality: 100 } - packer.packing(imageResource, packingOpt).then((imgBuffer: ArrayBuffer) => { + packer.packing(pixelMap, packingOpt).then((imgBuffer: ArrayBuffer) => { this.imageBuffer = imgBuffer; console.log(TAG, "get imageBuffer success!") }) + if (frameResult.scanCodeRects[0]) { this.scanCodeRect = []; // 码图位置信息转换 @@ -396,7 +400,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab Log.e(TAG, "analyzeImage:Callback Error:" + JSON.stringify(error)); return } - Log.d(TAG, "analyzeImage:Callback scan result:" + JSON.stringify(scanResult)); + Log.d(TAG, "analyzeImage:Callback scan result:" + JSON.stringify(scanResult)); if (scanResult.length == 0) { result.success(false) return @@ -407,10 +411,10 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab let corners: Array = [] if (cornerPoints != undefined && cornerPoints != null) { corners = [ - {x: this.cameraWidth - cornerPoints[0].y, y: cornerPoints[0].x}, - {x: this.cameraWidth - cornerPoints[1].y, y: cornerPoints[1].x}, - {x: this.cameraWidth - cornerPoints[2].y, y: cornerPoints[2].x}, - {x: this.cameraWidth - cornerPoints[3].y, y: cornerPoints[3].x}, + { x: this.cameraWidth - cornerPoints[0].y, y: cornerPoints[0].x }, + { x: this.cameraWidth - cornerPoints[1].y, y: cornerPoints[1].x }, + { x: this.cameraWidth - cornerPoints[2].y, y: cornerPoints[2].x }, + { x: this.cameraWidth - cornerPoints[3].y, y: cornerPoints[3].x }, ] } return { -- Gitee From 1f7357cbb2aaec0c29bb773119b10dd729a458e1 Mon Sep 17 00:00:00 2001 From: laoguanyao <806103474@qq.com> Date: Fri, 20 Sep 2024 09:08:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=9B=BE=E7=89=87buffer=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: laoguanyao <806103474@qq.com> --- ohos/src/main/ets/MobileScannerPlugin.ets | 30 +++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ohos/src/main/ets/MobileScannerPlugin.ets b/ohos/src/main/ets/MobileScannerPlugin.ets index b2b5cde..69fe744 100644 --- a/ohos/src/main/ets/MobileScannerPlugin.ets +++ b/ohos/src/main/ets/MobileScannerPlugin.ets @@ -78,7 +78,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab private scanWindow: Array | null = null; // scanWindow,扫描区域 - private imageBuffer: ArrayBuffer | null = null; + private imageBuffer: Uint8Array | null = null; publishEvent(event: ESObject) { this.eventSink?.success(event) @@ -209,7 +209,7 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab this.setDisplay() - this.startScan(cameraWidth, cameraHeight) + this.startScan() //customScan.on('lightingFlash', this.torchCallback); if (torch) { @@ -294,21 +294,19 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab let sourcePotions: image.SourceOptions = { sourceDensity: 120, sourcePixelFormat: image.PixelMapFormat.NV21, - sourceSize: { height: this.cameraWidth, width: this.cameraHeight } + sourceSize: { height: 960, width: 1280 } } let imageResource = image.createImageSource(frameResult.byteBuffer, sourcePotions) let opts: image.InitializationOptions = { editable: true, pixelFormat: 3, - size: { height: this.cameraWidth, width: this.cameraHeight } + size: { height: 960, width: 1280 } } let pixelMap: image.PixelMap = await imageResource.createPixelMap(opts) - let packer = image.createImagePacker() + let packer: image.ImagePacker = image.createImagePacker() let packingOpt: image.PackingOption = { format: "image/jpeg", quality: 100 } - packer.packing(pixelMap, packingOpt).then((imgBuffer: ArrayBuffer) => { - this.imageBuffer = imgBuffer; - console.log(TAG, "get imageBuffer success!") - }) + let imgBuffer = await packer.packing(pixelMap, packingOpt); + this.imageBuffer = new Uint8Array(imgBuffer) if (frameResult.scanCodeRects[0]) { this.scanCodeRect = []; @@ -336,10 +334,10 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab } } - startScan(cameraWidth: number, cameraHeight: number) { + startScan() { const viewControl: customScan.ViewControl = { - width: cameraWidth, - height: cameraHeight, + width: 1280, + height: 960, surfaceId: this.surfaceId!, }; @@ -447,14 +445,14 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab result.success(true) } - callback(barcodes: Barcode[], image: ArrayBuffer | null, width: number, height: number) { + callback(barcodes: Barcode[], image: Uint8Array | null, width: number, height: number) { // 需要先暂停后才能继续识别 customScan.stop().then(() => { if (image) { this.publishEvent({ "name": "barcode", "data": barcodes, - "image": new Uint8Array(image), + "image": image, "width": width, "height": height }) @@ -465,7 +463,9 @@ export class MobileScannerPlugin implements FlutterPlugin, MethodCallHandler, Ab }) } // 连续识别 - this.startScan(this.cameraWidth, this.cameraHeight) + setTimeout(() => { + this.startScan() + }, 1000) }) } -- Gitee