import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, scanCore, customScan } from '@kit.ScanKit';
let options: scanBarcode.ScanOptions = {
scanTypes: [scanCore.ScanType.ALL],
enableMultiMode: true,
enableAlbum: true
};
@Entry
@Component
struct customScanPage {
// 设置预览流高度,默认单位:vp
@State cameraHeight: number = 640
// 设置预览流宽度,默认单位:vp
@State cameraWidth: number = 360
private mXComponentController: XComponentController = new XComponentController();
build() {
Stack() {
XComponent({
id: 'componentId',
type: 'surface',
controller: this.mXComponentController
})
.onLoad(() => {
hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called.');
// 获取XComponent的surfaceId
let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
// 设置ViewControl相应字段
let viewControl: customScan.ViewControl = {
width: this.cameraWidth,
height: this.cameraHeight,
surfaceId: surfaceId
};
customScan.init(options);
customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
}).catch((error: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
});
})// 预览流宽、高,默认单位vp,支持px、lpx、vp
.height(this.cameraHeight)
.width(this.cameraWidth)
.position({ x: 0, y: 0 })
}
.alignContent(Alignment.Bottom)
.height('100%')
.width('100%')
.position({ x: 0, y: 0 })
}
}
代码为示例代码
问题:
相机权限授权了在跳转此页面进行扫码展示,但是进入就会报错Code: 1000500001, message: The internal error.
API参考里面给的是部分的代码不是整个端到端的流程。当前代码的问题是没有申请相机的权限。 可以参考自定义扫码指南查看
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/scan-customscan-V5
几个要点: