HarmonyOS 多个二维码扫码时使用ScanCodeRect获取二维码的位置,位置不准确?

使用(left + right) / 2的方法获取二维码中心点坐标,获取的位置与实际二维码位置差距较大

阅读 602
1 个回答

在显示扫码结果的方法中,建议是适配显示区域,使用(left + right) / 2的方法获取二维码中心点坐标的同时,还需要加上相机的偏移量cameraOffsetX;多个二维码扫码时showScanResult相关示例代码可以参考官方给出的sample code适配,或者参考如下代码:

showScanResult(result: Array<scanBarcode.ScanResult>)
// 取到扫描结果后暂停相机流
// 获取二维码四个顶点坐标
let left = result[0].scanCodeRect?.left as number
let right = result[0].scanCodeRect?.right as number
let top = result[0].scanCodeRect?.top as number
let bottom = result[0].scanCodeRect?.bottom as number
// 计算中心点坐标
this.x = (left + right) / 2 + this.cameraOffsetX
this.y = (top + bottom) / 2

if (result[1] != null || undefined) {
  // 取到扫描结果后暂停相机流
  // 获取二维码四个顶点坐标
  let left1 = result[1].scanCodeRect?.left as number
  let right1 = result[1].scanCodeRect?.right as number
  let top1 = result[1].scanCodeRect?.top as number
  let bottom1 = result[1].scanCodeRect?.bottom as number

  this.x1 = (left1 + right1) / 2 + this.cameraOffsetX
  this.y1 = (top1 + bottom1) / 2
  LogUtils.info(TAG, this.x + "left")
  LogUtils.info(TAG, this.y + "top")
  LogUtils.info(TAG, this.cameraOffsetX + "cameraOffsetX")
  LogUtils.info(TAG, this.cameraOffsetY + "cameraOffsetY")
}


//放在自定义扫码界面的顶部返回按钮和扫码提示
// 由于xy都是@State 修饰的值会动态刷新position

Button('')
  .backgroundColor(Color.Black)
  .type(ButtonType.Circle)
  .width('10')
  .height('10')// .position({x:this.x,y:this.y})
  .position({ x: this.x + "vp", y: this.y + "vp" })

Button('')
  .backgroundColor(Color.Black)
  .type(ButtonType.Circle)
  .width('10')
  .height('10')// .position({x:this.x,y:this.y})
  .position({ x: this.x1 + "vp", y: this.y1 + "vp" });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进