HarmonyOS中如何在相机回调的拍照数据做裁切?

HarmonyOS中如何在相机回调的拍照数据做裁切?目前想实现根据下面函数回调的nextImage 数据,对数据做裁切,类似传入矩形位置,然后拿到裁切后的数据。

try { 
  imageReceiver.on('imageArrival', () => { 
    imageReceiver.readNextImage((err: BusinessError, nextImage: image.Image) => { 
      if (err || nextImage === undefined) { 
        ConsoleUtils.LOG_SDK_WARN(ConsoleUtils.CAMERA, "onImageArrival  readNextImage err:" + err.code) 
        return; 
      } 
      nextImage.getComponent(image.ComponentType.JPEG, (err: BusinessError, imgComponent: image.Component) => { 
        //ConsoleUtils.LOG_SDK_WARN(ConsoleUtils.CAMERA,IdcardConfig.SDK_TAG_CameraController+ "getComponent") 
        if (err || imgComponent === undefined) { 
          ConsoleUtils.LOG_SDK_WARN(ConsoleUtils.CAMERA, "onImageArrival getComponent err:" + err.code) 
          return; 
        } 
        //ConsoleUtils.LOG_SDK_WARN(ConsoleUtils.CAMERA,IdcardConfig.SDK_TAG_CameraController+ "isPreviewMode:" + this.isPreviewMode); 
        if (imgComponent.byteBuffer as ArrayBuffer) { 
          // 回调数据 
          onReceiver(imgComponent); 
          nextImage.release(); // 必须进行释放否则无法持续获取帧数据 
        } else { 
          nextImage.release(); 
          return; 
        } 
      }) 
    }) 
  }) 
} catch (error) { 
}
阅读 718
1 个回答

你可以参考一下Demo将图片转成pixelmap,再根据文档https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V...,进行图片的裁切处理。

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); 
        imageResource.createPixelMap({}).then((res)=>{ 
          this.imgUrl = res; 
        }); 
      } else { 
        return; 
      } 
      nextImage.release(); 
    }) 
  }) 
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题