往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)
自定义界面如何连续扫码(customScan.rescan)
问题现象
自定义界面扫码扫到码值后,如何连续扫码?
解决措施
customScan.rescan可以重新触发一次扫码,必须在customScan.start(viewControl, callback)方法Callback接口回调中有效,Promise方式无效。
示例:
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan, scanBarcode } from '@kit.ScanKit';
@Entry
@Component
struct Index {
private callback: AsyncCallback<Array<scanBarcode.ScanResult>> =
async (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
if (error) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get ScanResult by callback. Code: ${error.code}, message: ${error.message}`);
return;
}
hilog.info(0x0001, '[Scan Sample]',
`Succeeded in getting ScanResult by callback, result is ${JSON.stringify(result)}`);
try {
// 重新触发扫码:不需要重启相机并重新触发一次扫码,可以在start接口的Callback异步回调中,调用rescan接口。
customScan.rescan();
} catch (error) {
hilog.error(0x0001, '[Scan Sample]', `Failed to rescan. Code: ${error.code}, message: ${error.message}`);
}
}
build() {
}
}