欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > HarmonyOS Next鸿蒙扫一扫功能实现

HarmonyOS Next鸿蒙扫一扫功能实现

2024/10/25 11:27:08 来源:https://blog.csdn.net/m940034240/article/details/142264347  浏览:    关键词:HarmonyOS Next鸿蒙扫一扫功能实现

直接使用的是华为官方提供的api,封装成一个工具类方便调用。

import { common } from '@kit.AbilityKit';
import { scanBarcode, scanCore } from '@kit.ScanKit';export namespace ScanUtil {export async function startScan(context: common.Context) : Promise<string> {if (canIUse('SystemCapability.Multimedia.Scan.Core')) {// 定义扫码参数optionslet options: scanBarcode.ScanOptions = {scanTypes: [scanCore.ScanType.ALL],enableMultiMode: true,enableAlbum: true};if (canIUse('SystemCapability.Multimedia.Scan.ScanBarcode')) {let result: scanBarcode.ScanResult = await scanBarcode.startScanForResult(context, options)return result.originalValue}}throw new Error('该设备不支持扫码功能')}
}

使用方法:

Button('打开扫一扫').onClick(()=>{ScanUtil.startScan(getContext()).then((result)=>{//成功识别到二维码内容promptAction.showToast({message: `result:${result}`,duration: 3000})}).catch((error:BusinessError)=>{//一般是模拟器会跑到这里报异常promptAction.showToast({message: `error:${error}`,duration: 3000})})})

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com