HarmonyOS camera api 相机拍照分辨率设置问题

操作步骤:

1、PhotoOutput 获取拍照图片时,无法设置分辨率

2、ImageReceiver 获取预览流图片时,保存到本地图片格式错误.

HarmonyOS
2024-12-25 15:43:20
1326浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

PhotoOutput获取拍照图片时,无法设置分辨率,这个问题,可以设置分辨率,我们这边设置的是相机支持的分辨率,PhotoOutput中的profile属性不能自定义,只能通过接口获取,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-camera-V5#getsupportedoutputcapability11

第二个问题,开发者输出出来的图片无法预览,是因为开发者的带直接写进文件的是yuv数据,请参考下面的代码,生成yuv格式的图片:

async yuv(receiver: image.ImageReceiver): Promise<void> {
  receiver.on('imageArrival', () => {
  console.error("imageArrival callback");
  receiver.readNextImage((err, nextImage: image.Image) => {
  let a = nextImage.format
  nextImage.getComponent(image.ComponentType.JPEG, (err: BusinessError, imgComponent: image.Component) => {
  if (err || imgComponent === undefined) {
  // CLog.error('getComponent failed');
}
if (imgComponent) {
  let path: string = getContext().filesDir + "/image1.yuv";
  if (fileIo.accessSync(path)) {
    fileIo.rmdirSync(path)
  }
  let file = fileIo.openSync(path, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
  let opt: WriteOptions = {
    // 多出2048字节数据
    length: imgComponent.byteBuffer.byteLength
  }
  fileIo.writeSync(file.fd, imgComponent.byteBuffer, opt)
  fileIo.close(file)
} else {
  // CLog.error('byteBuffer is null');
}
})
})
})
}
  • 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.

如果想获取JPEG,请参考这个demo:

async onImageArrival(receiver: image.ImageReceiver): Promise<void> {
  receiver.on('imageArrival', () => {
  console.error("imageArrival callback");
  receiver.readNextImage((err, nextImage: image.Image) => {
  nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => {
  if (err || imgComponent === undefined) {
  return;
}

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 decodingOptions: image.DecodingOptions = {
    sampleSize:1, // 缩略图采样大小
    editable: true, // 是否可编辑
    desiredSize:{ width:500, height:500}, // 期望输出大小
    rotate: 270, // 旋转角度
    desiredPixelFormat:8, // 解码的像素格式
    desiredRegion: { size: { height:500, width:500 }, x: 0, y: 0 }, // 解码的区域
    index:0 // 图片序号
  };
  imageResource.createPixelMap(decodingOptions).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.
  • 39.
  • 40.
分享
微博
QQ
微信
回复
2024-12-25 18:04:41


相关问题
如何设置图片显示的分辨率
1336浏览 • 1回复 待解决
应用图标分辨率规范问题
1914浏览 • 1回复 待解决
HarmonyOS 手机分辨率怎么获取?
1181浏览 • 1回复 待解决
HarmonyOS 图片自定义分辨率问题
809浏览 • 1回复 待解决
HarmonyOS 如何获取屏幕分辨率
1595浏览 • 1回复 待解决
如何在图片显示的分辨率
2784浏览 • 1回复 待解决
获取鸿蒙的分辨率高度不对
7223浏览 • 1回复 待解决
HarmonyOS 如何获取视频时长和分辨率
799浏览 • 1回复 待解决
OpenGL无法正常渲染某些分辨率YUV数据
1342浏览 • 0回复 待解决