HarmonyOS 相机预览尺寸设置

相机是否支持预览大小的设置,文档中没看到api,XComponent尺寸设置不合适的话会模糊?类似于setPreviewSize,我可以获取现在相机支持的所有尺寸,然后根据预览view的大小选择分辨率最接近的相机尺寸

HarmonyOS
2024-12-27 17:21:35
591浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

1、可通过此接口设置分辨率

let previewProfilesObj: camera.Profile = previewProfiles[0];
  • 1.

2、获取预览数据可通过

receiver.on('imageArrival', () => {
  console.error("imageArrival callback");
  receiver.readNextImage((err, nextImage: image.Image) => {
    let a = nextImage.format
    nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => {
      if (err || imgComponent === undefined) {
        return;
      }
      this.saveImageToFile(imgComponent.byteBuffer);
      if (imgComponent.byteBuffer as ArrayBuffer) {
        let sourceOptions: image.SourceOptions = {
          sourceDensity: 120,
          sourcePixelFormat: 8, // NV21
          sourceSize: {
            height: this.previewProfilesObj2!.size.height,
            width: this.previewProfilesObj2!.size.width
          },
        }
        let imageResource = image.createImageSource(imgComponent.byteBuffer, sourceOptions);
        let imagePackerApi = image.createImagePacker();
        let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
        const filePath: string = getContext().cacheDir + "/image.jpg";
        let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
        imagePackerApi.packToFile(imageResource, file.fd, packOpts).then(() => {
          console.error('pack success: ' + filePath);
        }).catch((error: BusinessError) => {
          console.error('Failed to pack the image. And the error is: ' + error);
        })
        imageResource.createPixelMap({editable:true,desiredPixelFormat:image.PixelMapFormat.NV21}).then((res)=>{
          this.imgUrl = res;
        });
      } else {
        return;
      }
      nextImage.release();
    })
  })
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-12-27 19:28:39
相关问题
HarmonyOS 相机采集尺寸问题
562浏览 • 1回复 待解决
HarmonyOS 相机-拍照之后预览
1414浏览 • 1回复 待解决
HarmonyOS 创建相机预览失败
1157浏览 • 1回复 待解决
HarmonyOS 相机预览的时候预览图被拉伸
1018浏览 • 1回复 待解决
HarmonyOS 相机预览拍照流程报错
1212浏览 • 1回复 待解决
如何创建相机预览输出?
943浏览 • 0回复 待解决
HarmonyOS 相机预览是否支持放大缩小
682浏览 • 1回复 待解决
HarmonyOS 自定义相机预览问题
1004浏览 • 1回复 待解决
ConstraintSize尺寸设置问题
1137浏览 • 1回复 待解决
HarmonyOS 自定义相机预览拉伸问题
891浏览 • 1回复 待解决
ConstraintSize尺寸设置不生效
3221浏览 • 1回复 待解决
相机预览及切换摄像头
1946浏览 • 1回复 待解决
如何连续获取相机预览流数据
1496浏览 • 1回复 待解决