diff --git a/package/harmony/vision_camera/src/main/ets/VisionCameraView.ets b/package/harmony/vision_camera/src/main/ets/VisionCameraView.ets index 5eba25417d..7bb889264c 100644 --- a/package/harmony/vision_camera/src/main/ets/VisionCameraView.ets +++ b/package/harmony/vision_camera/src/main/ets/VisionCameraView.ets @@ -44,6 +44,8 @@ import Utils from './utils/Utils'; import { VisionCameraViewSpec } from './types/VisionCameraViewSpec'; import { RecordVideoOptions } from './types/VideoFile'; import { Permissions } from '@kit.AbilityKit'; +import { scanBarcode } from '@kit.ScanKit' +import { BusinessError } from '@kit.BasicServicesKit' const previewViewType: Record<'surface-view' | 'texture-view', 'surface' | 'texture'> = { 'surface-view': 'surface', @@ -443,24 +445,28 @@ export struct VisionCameraView { async scanStart(isFirst?: boolean) { clearTimeout(this.timer) - let scanResult: ScanResult | undefined = undefined; + let scanResult: ScanResult | Array | undefined = undefined; + const isActive = this.descriptorWrapper['rawProps'].isActive; try { - scanResult = await this.scanSession.scanStart(this.surfaceId, - { width: this.localDisplayWidth, height: this.localDisplayHeight }, isFirst); + this.scanSession.scanStart(this.surfaceId, + { width: this.localDisplayWidth, height: this.localDisplayHeight }, isFirst, isActive, + (error: BusinessError, result: Array) => { + if (error) { + Logger.error(TAG, `start failed, code: ${error.code}, message: ${error.message} `) + return; + } + scanResult = result; + if (scanResult) { + const callbackResult = this.scanSession.showScanResult(result); + this.ctx.rnInstance.emitDeviceEvent('onCodeScanned', callbackResult); + } + this.scanSession.rescan(isActive); + }); } catch (error) { if (error.code === '1000500001') { this.onError(new CameraRuntimeError('unknown/unknown', 'codeScanner preview size not support.')) } } - if (scanResult) { - this.ctx.rnInstance.emitDeviceEvent('onCodeScanned', scanResult); - Logger.info(TAG, `get scan result: ${JSON.stringify(scanResult)}`) - - const isActive = this.descriptorWrapper['rawProps'].isActive; - if (isActive) { - this.scanStart(); - } - } } diff --git a/package/harmony/vision_camera/src/main/ets/service/ScanSession.ts b/package/harmony/vision_camera/src/main/ets/service/ScanSession.ts index efd9676c2e..fade6917ee 100644 --- a/package/harmony/vision_camera/src/main/ets/service/ScanSession.ts +++ b/package/harmony/vision_camera/src/main/ets/service/ScanSession.ts @@ -68,7 +68,7 @@ export default class ScanSession { /** * 启动相机进行扫码 */ - async scanStart(surfaceId: string, SurfaceRect, isFirst?: boolean): Promise { + async scanStart(surfaceId: string, SurfaceRect, isFirst: boolean, isAction: boolean, callback: AsyncCallback>): Promise { Logger.info(TAG, `ScanStart:surfaceId: ${surfaceId}`) Logger.info(TAG, `ScanStart:SurfaceRect: ${JSON.stringify(SurfaceRect)}`) Logger.info(TAG, `ScanStart:isFirst: ${isFirst}`) @@ -82,25 +82,20 @@ export default class ScanSession { } this.ScanFrame = SurfaceRect; - return new Promise((resolve, reject) => { - let viewControl: customScan.ViewControl = { - width: SurfaceRect.width, - height: SurfaceRect.height, - surfaceId: surfaceId - }; - Logger.info(TAG, `start viewControl, info: ${JSON.stringify(viewControl)}`); - customScan.start(viewControl, (error: BusinessError, result: Array) => { - if (error) { - Logger.error(TAG, `start failed, code: ${error.code}, message: ${error.message}`); - reject(error); - return; - } - const scanResult = this.showScanResult(result); - Logger.info(TAG, `scanResult11: ${JSON.stringify(scanResult)}`); - this.setEndStatus(true) - resolve(scanResult) - }, this.frameCallback); - }) + let viewControl: customScan.ViewControl = { + width: SurfaceRect.width, + height: SurfaceRect.height, + surfaceId: surfaceId + }; + Logger.info(TAG, `start viewControl, info: ${JSON.stringify(viewControl)}`); + customScan.start(viewControl, callback); + } + } + + async rescan(isAction: boolean) { + this.setEndStatus(true); + if (isAction) { + customScan.rescan(); } }