Skip to content

Commit

Permalink
Merge pull request #35 from dream-approaching/sig
Browse files Browse the repository at this point in the history
fix: 修复无法连续扫码的问题
  • Loading branch information
dream-approaching authored Aug 14, 2024
2 parents 5a4e0d1 + 17126af commit 7b96035
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
30 changes: 18 additions & 12 deletions package/harmony/vision_camera/src/main/ets/VisionCameraView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -443,24 +445,28 @@ export struct VisionCameraView {

async scanStart(isFirst?: boolean) {
clearTimeout(this.timer)
let scanResult: ScanResult | undefined = undefined;
let scanResult: ScanResult | Array<scanBarcode.ScanResult> | 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<scanBarcode.ScanResult>) => {
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();
}
}

}

Expand Down
35 changes: 15 additions & 20 deletions package/harmony/vision_camera/src/main/ets/service/ScanSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class ScanSession {
/**
* 启动相机进行扫码
*/
async scanStart(surfaceId: string, SurfaceRect, isFirst?: boolean): Promise<ScanResult> {
async scanStart(surfaceId: string, SurfaceRect, isFirst: boolean, isAction: boolean, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): Promise<void> {
Logger.info(TAG, `ScanStart:surfaceId: ${surfaceId}`)
Logger.info(TAG, `ScanStart:SurfaceRect: ${JSON.stringify(SurfaceRect)}`)
Logger.info(TAG, `ScanStart:isFirst: ${isFirst}`)
Expand All @@ -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<scanBarcode.ScanResult>) => {
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();
}
}

Expand Down

0 comments on commit 7b96035

Please sign in to comment.