当前位置: 首页 > news >正文

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})})})


http://www.mrgr.cn/news/26880.html

相关文章:

  • 【ROS2】将launch启动文件添加的包中
  • Hazel 2024
  • 跨国公司IBM研发中心撤出,挑战与机遇并存:中国IT产业何去何从?
  • 网络的一些基本概念
  • navicate远程linux上的pgsql提示密码失败
  • Codeforces practice C++ 2024/9/11 - 2024/9/13
  • 无需部署,云电脑带你秒变AI绘画大师
  • c++临时对象导致的生命周期问题
  • 阿里云专业翻译api对接
  • 一元三次方程求根
  • [git] MacBook 安装git
  • MySQL8.0.13-函数索引
  • 价值流与核心理论框架对比解析:企业业务架构优化的全景指南
  • yjs04——matplotlib的使用(多个坐标图)
  • 队列OJ题——设计循环队列
  • 如何使用ssm实现物流配送人员车辆调度管理系统的设计与实现+vue
  • Linux 生成 git ssh 公钥
  • VBA语言専攻T3学员领取资料通知0915
  • 特邀文章┆大学录取专业不理想,怎么办?
  • 升级Ubuntu内核的几种方法