HarmonyOS 二维码在图片和预览界面中的坐标

自定义相机使用双路预览,通过ScanKit中detectBarcode.decodeImage方法对ImageReceiver获取的图片进行扫码,目前得到二维码坐标。怎样转换成在图片中的坐标和界面上(XComponent)的坐标?

目前设置的制定相机的profile为1920*1080XComponent设置如下,XComponent设置的是全屏宽度,目前根据文档,计算出来还是有偏差。

XComponent({
  //组件的唯一标识
  id: 'LOXComponent',
  // surface:EGL/OpenGLES和媒体数据写入  component:开发者定制绘制内容
  type: XComponentType.SURFACE,
  //应用Native层编译输出动态库名称,仅XComponent类型为"surface"时有效
  libraryname: 'LOSingleXComponent',
  //给组件绑定一个控制器,通过控制器调用组件方法,仅XComponent类型为"surface"时有效
  controller: this.mXComponentController
})//插件加载完成时回调事件
  .onLoad(() => {
    this.mXComponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight:1080  });
    // 获取Surface ID
    this.xComponentSurfaceId = this.mXComponentController.getXComponentSurfaceId();
  })//插件卸载完成时回调事件
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

目前计算的是在屏幕XComponent中的点并绘制,主要代码:

let scaleRadio = display.getDefaultDisplaySync().width * 9 / 16 / 1080
console.log("我的变换大小" + scaleRadio)
console.log(display.getDefaultDisplaySync().width.toString())
console.log(display.getDefaultDisplaySync().height.toString())
let bottomRight: Point = {
  x: (1080 - singleScanResult.cornerPoints![0].y) * scaleRadio,
  y: (singleScanResult.cornerPoints![0].x) * scaleRadio
}
// 左下角(x, y):(1080 - cornerPoints[1].y, cornerPoints[1].x)
let bottomLeft: Point = {
  x: (1080 - singleScanResult.cornerPoints![1].y) * scaleRadio,
  y: (singleScanResult.cornerPoints![1].x) * scaleRadio
}
// 左上角(x, y):(1080 - cornerPoints[2].y, cornerPoints[2].x)
let topLeft: Point = {
  x: (1080 - singleScanResult.cornerPoints![2].y) * scaleRadio,
  y: (singleScanResult.cornerPoints![2].x * scaleRadio)
}
// 右上角(x, y):(1080 - cornerPoints[3].y, cornerPoints[3].x)
let topRight: Point = {
  x: (1080 - singleScanResult.cornerPoints![3].y) * scaleRadio,
  y: singleScanResult.cornerPoints![3].x * scaleRadio
}   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

但是以上计算不准确。

HarmonyOS
2024-12-24 16:18:00
715浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

请尝试以下方案:

1、调整下XComponent的宽高设置。原代码:

.width('100%').height((display.getDefaultDisplaySync().width / 16 * 9) )  
  • 1.

删除,改为给父组件stack设置宽高。

.width(display.getDefaultDisplaySync().width + 'px') 
.height((display.getDefaultDisplaySync().width / 9 * 16) + 'px')
  • 1.
  • 2.

Xcomponent宽高设置为100%,这一步可以设置相机部分为9:16。

2、改下获取的坐标。相较于原代码,区别在于使用了px2vp换算。

let bottomRight: Point = {
  x: px2vp((1080 - singleScanResult.cornerPoints![0].y) * scaleRadio),
  y: px2vp((singleScanResult.cornerPoints![0].x) * scaleRadio)
}
// 左下角(x, y):(1080 - cornerPoints[1].y, cornerPoints[1].x)
let bottomLeft: Point = {
  x: px2vp((1080 - singleScanResult.cornerPoints![1].y) * scaleRadio),
  y: px2vp((singleScanResult.cornerPoints![1].x) * scaleRadio)
}
// 左上角(x, y):(1080 - cornerPoints[2].y, cornerPoints[2].x)
let topLeft: Point = {
  x: px2vp((1080 - singleScanResult.cornerPoints![2].y) * scaleRadio),
  y: px2vp((singleScanResult.cornerPoints![2].x * scaleRadio))
}
// 右上角(x, y):(1080 - cornerPoints[3].y, cornerPoints[3].x)
let topRight: Point = {
  x: px2vp((1080 - singleScanResult.cornerPoints![3].y) * scaleRadio),
  y: px2vp(singleScanResult.cornerPoints![3].x * scaleRadio)
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
分享
微博
QQ
微信
回复
2024-12-24 19:31:52


相关问题
HarmonyOS zxing二维
997浏览 • 1回复 待解决
HarmonyOS 二维生成失败
794浏览 • 1回复 待解决
HarmonyOS 二维生成demo
1165浏览 • 2回复 待解决
HarmonyOS 二维条码扫描识别
1334浏览 • 1回复 待解决
HarmonyOS 支持扫描二维吗?
1174浏览 • 1回复 待解决
HarmonyOS如何无感知扫描二维
1180浏览 • 1回复 待解决
HarmonyOS 二维显示导出base64
683浏览 • 1回复 待解决
HarmonyOS扫描二维方案是什么?
2993浏览 • 1回复 待解决
HarmonyOS APP可以生成二维,扫安装
1590浏览 • 1回复 待解决
二维扫描三方库推荐
957浏览 • 1回复 待解决
HarmonyOS h5加载二维屏幕变亮
835浏览 • 1回复 待解决
Canvas组件实现二维中心内嵌图标
1580浏览 • 1回复 待解决