HarmonyOS开发中 readLatestImage相关问题?

Camera kit/相机最佳实践/双路预览/ImageReceiver readLatestImage相关问题,需要打开相机,获取最后一帧的图片。

阅读 593
1 个回答

请尝试以下代码转换,其中的宽高可换成,预览组件的宽高。

let sourceOptions: image.SourceOptions = { 
  sourceDensity: 120, 
  sourcePixelFormat: 8, // NV21 
  sourceSize: { 
    height: 400, 
    width: 400 
  }, 
} 
let imageResource = image.createImageSource(imgComponent.byteBuffer, sourceOptions); 
imageResource.createPixelMap({}).then((res)=>{ 
  this.pixel = res; 
});

参考代码如下:
1、设定目标图片尺寸。

imageSize: image.Size = { width: 1920, height: 1080 };

2、获取对应的profile。

// 获取相机设备支持的输出流能力 
let cameraOutputCapability: camera.CameraOutputCapability = this.cameraManager.getSupportedOutputCapability(cameraDevices[0], camera.SceneMode.NORMAL_PHOTO) 
let previewProfile = cameraOutputCapability.previewProfiles[0]; 
cameraOutputCapability.previewProfiles.forEach((profile) => { 
  if (profile.size.width == this.imageSize.width && profile.size.height == this.imageSize.height && profile.format.valueOf() == 1003) { 
    previewProfile = profile; 
    return; 
  } 
}) 
this.imageSize = previewProfile.size; 
// 创建相机预览输出流 
this.previewOutput = this.cameraManager.createPreviewOutput(previewProfile, this.surfaceId); 
//双路预览: 创建 预览流2 输出对象 
this.imageReceiver = image.createImageReceiver(this.imageSize, image.ImageFormat.JPEG, 8); 
let imageReceiverSurfaceId: string = await this.imageReceiver.getReceivingSurfaceId(); 
this.previewOutput2 = this.cameraManager.createPreviewOutput(previewProfile, imageReceiverSurfaceId); 
this.onImageArrival(this.imageReceiver); 
// 创建拍照输出流 
let photoProfile = cameraOutputCapability.photoProfiles[0]; 
cameraOutputCapability.photoProfiles.forEach((profile) => { 
  if (profile.size.width == this.imageSize.width && profile.size.height == this.imageSize.height && profile.format.valueOf() == 1003) { 
    photoProfile = profile; 
    return; 
  } 
}) 
this.photoOutput = this.cameraManager.createPhotoOutput(photoProfile);

3、imageReceiver获取图片帧转换成pixelMap。

// 通过Surface进行数据传递,通过ImageReceiver的surface获取预览图像。 
async onImageArrival(receiver: image.ImageReceiver): Promise<void> { 
  receiver.on('imageArrival', () => { 
  receiver.readNextImage(async (err, nextImage: image.Image) => { 
  console.info(`enter CameraDemo imageArrival, nextImage: ${JSON.stringify(nextImage)}`) 
if (err || nextImage === undefined) { 
  console.error(`CameraDemo imageArrival error, error is ${JSON.stringify(err)} or nextImage is undefined`) 
  return; 
} 
nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => { 
  if (err || imgComponent === undefined) { 
    console.error(`CameraDemo getComponent error, error is ${JSON.stringify(err)} or imgComponent is undefined`) 
    return; 
  } 
  if (imgComponent.byteBuffer as ArrayBuffer) { 
    let sourceOptions: image.SourceOptions = { 
      sourceDensity: 0, 
      sourcePixelFormat: image.PixelMapFormat.NV21, // NV21 
      sourceSize: this.imageSize 
    } 
    let imageSource: image.ImageSource = image.createImageSource(imgComponent.byteBuffer, sourceOptions); 
    let opts: image.InitializationOptions = { 
      editable: true, 
      pixelFormat: image.PixelMapFormat.NV21, 
      size: this.imageSize 
    } 
    let pixelMap = await imageSource.createPixelMap(opts); 
    await pixelMap.rotate(90.0); 
    this.pixelMap = pixelMap; 
    await imageSource.release(); 
  } else { 
    return; 
  } 
  nextImage.release() 
}) 
}) 
}) 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题