基本概念
默认界面扫码能力提供系统级体验一致的扫码界面,Scan Kit对系统相机权限进行了预授权,调用接口时,无需开发者再次申请相机权限。
适用于不同扫码场景的应用开发
场景介绍
默认界面扫码能力提供了系统级体验一致的扫码界面以及相册扫码入口,支持单码和多码识别,支持多种识码类型,请参见ScanType。
无需使用三方库就可帮助开发者的应用快速处理各种扫码场景
使用示例
1、导入默认界面扫码模块,scanCore提供扫码类型定义,scanBarcode提供拉起默认界面扫码的方法和参数
import { scanCore, scanBarcode } from '@kit.ScanKit';
2、调用startScanForResult方法拉起默认扫码界面
①、通过Promise方式得到扫码结果
// 定义扫码参数optionslet options: scanBarcode.ScanOptions = {scanTypes: [scanCore.ScanType.ALL],enableMultiMode: true,enableAlbum: true};try {// 可调用getContext接口获取当前页面关联的UIAbilityContextscanBarcode.startScanForResult(getContext(this), options).then((result: scanBarcode.ScanResult) => {// 解析码值结果跳转应用服务页hilog.info(0x0001, '[Scan CPSample]', `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);}).catch((error: BusinessError) => {hilog.error(0x0001, '[Scan CPSample]',`Failed to get ScanResult by promise with options. Code:${error.code}, message: ${error.message}`);});} catch (error) {hilog.error(0x0001, '[Scan CPSample]',`Failed to start the scanning service. Code:${error.code}, message: ${error.message}`);}
②、通过Callback回调函数得到扫码结果
// 定义扫码参数optionslet options: scanBarcode.ScanOptions = {scanTypes: [scanCore.ScanType.ALL],enableMultiMode: true,enableAlbum: true};try {// 可调用getContext接口获取当前页面关联的UIAbilityContextscanBarcode.startScanForResult(getContext(this), options,(error: BusinessError, result: scanBarcode.ScanResult) => {if (error) {hilog.error(0x0001, '[Scan CPSample]',`Failed to get ScanResult by callback with options. Code: ${error.code}, message: ${error.message}`);return;}// 解析码值结果跳转应用服务页hilog.info(0x0001, '[Scan CPSample]', `Succeeded in getting ScanResult by callback with options, result is ${JSON.stringify(result)}`);})} catch (error) {hilog.error(0x0001, '[Scan CPSample]',`Failed to start the scanning service. Code:${error.code}, message: ${error.message}`);}
约束与限制
1、默认界面扫码能力暂不支持悬浮屏、分屏场景
2、相册扫码只支持单码识别
3、不支持界面UX添加自定义设置
相关文档:
默认界面扫码
界面ui: