ArkTs 生成/扫描二维码

生成二维码组件

// 进行截图
    const pixelMap = await componentSnapshot.get('share')
    // 图片数据
    const imagePacker = image.createImagePacker()
    const arrayBuffer = await imagePacker.packing(pixelMap, { format: 'image/jpeg', quality: 98 })
    // 存储图片到沙箱
    const ctx = getContext(this)
    const imagePath = ctx.cacheDir + '/' + Date.now() + '.jpeg'
    const file = fileIo.openSync(imagePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE)
    fileIo.writeSync(file.fd, arrayBuffer)
    fileIo.closeSync(file.fd)
    //图片在手机存储中的完整路径
    const imageUri = fileUri.getUriFromPath(imagePath)
    const request = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(ctx, imageUri)
    //获取照片访问助手实例
    const helper = photoAccessHelper.getPhotoAccessHelper(ctx)
    //调用applyChanges执行
    await helper.applyChanges(request) //配置权限
    promptAction.showToast({ message: '已保存到相册' })

扫描二维码

if (canIUse('SystemCapability.Multimedia.Scan.ScanBarcode')) {
      const resp = await scanBarcode.startScanForResult(getContext())
      router.pushUrl({ url: 'pages/QuestionPage', params: { id: resp.originalValue } })
      AlertDialog.show({ message: resp.originalValue })
    }
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐