目录

分段式拍照(ArkTS)

分段式拍照是相机的重要功能之一,即应用下发拍照任务后,系统将分多阶段上报不同质量的图片。

通过分段式拍照,优化了系统的拍照响应时延,从而提升用户体验。

应用开发分段式拍照主要分为以下步骤:

  • 分段式拍照能力由设备和模式决定,不同的设备支持的模式各异,对应分段式能力也有所差异,因此应用在切换设备或模式后分段式能力可能会发生变化。
  • 应用无需主动使能分段式拍照能力,相机框架会在配流期间判断设备和模式是否支持分段式,如果支持会使能该功能。

开发步骤

详细的API说明请参考@ohos.multimedia.camera (相机管理)。

  1. 导入依赖,需要导入相机框架、媒体库、图片相关领域依赖。

    import { camera } from '@kit.CameraKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { photoAccessHelper } from '@kit.MediaLibraryKit';
  2. 确定拍照输出流。

    通过CameraOutputCapability中的photoProfiles属性,可获取当前设备支持的拍照输出流,通过createPhotoOutput方法创建拍照输出流。

    function getPhotoOutput(cameraManager: camera.CameraManager,
      cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined {
      let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles;
      if (photoProfilesArray===null || photoProfilesArray===undefined) {
        console.error("createOutput photoProfilesArray is null!");
        return undefined;
      }
      let photoOutput: camera.PhotoOutput | undefined = undefined;
      try {
       if (photoProfilesArray.length > 0) {
           photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]);
       } else {
           console.error("the length of photoProfilesArray<=0!");
           return undefined;
       }
      } catch (error) {
        let err = error as BusinessError;
        console.error(`Failed to createPhotoOutput. error: ${err}`);
      }
      return photoOutput;
    }
  3. 设置拍照photoAssetAvailable的回调。

    如果已经注册了photoAssetAvailable回调,并且在Session开始之后又注册了photoAvailable回调,photoAssetAvailable和photoAvailable同时注册,会导致流被重启,仅photoAssetAvailable生效。

    不建议开发者同时注册photoAvailable和photoAssetAvailable。

    function getPhotoAccessHelper(context: Context): photoAccessHelper.PhotoAccessHelper {
      let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
      return phAccessHelper;
    }
    
    function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput, context: Context): void {
      photoOutput.on('photoAssetAvailable', (err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset) => {
        if (err) {
          console.error(`photoAssetAvailable error: ${err}.`);
          return;
        }
        console.info('photoOutPutCallBack photoAssetAvailable');
        // 开发者可通过photoAsset调用媒体库相关接口,自定义处理图片。
        // 处理方式一:调用媒体库落盘接口保存一阶段图,二阶段图就绪后媒体库会主动帮应用替换落盘图片。
        mediaLibSavePhoto(photoAsset, getPhotoAccessHelper(context));
        // 处理方式二:调用媒体库接口请求图片并注册一阶段图或二阶段图buffer回调,自定义使用。
        mediaLibRequestBuffer(photoAsset, context);
      });
    }
    
    async function mediaLibSavePhoto(photoAsset: photoAccessHelper.PhotoAsset,
      phAccessHelper: photoAccessHelper.PhotoAccessHelper): Promise<void> {
      try {
        let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
        assetChangeRequest.saveCameraPhoto();
        await phAccessHelper.applyChanges(assetChangeRequest);
        console.info('apply saveCameraPhoto successfully');
      } catch (err) {
        console.error(`apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`);
      }
    }
    
    class MediaDataHandler implements photoAccessHelper.MediaAssetDataHandler<ArrayBuffer> {
      onDataPrepared(data: ArrayBuffer) {
        if (data === undefined) {
          console.error('Error occurred when preparing data');
          return;
        }
        // 应用获取到图片buffer后可自定义处理。
        console.info('on image data prepared');
      }
    }
    
    async function mediaLibRequestBuffer(photoAsset: photoAccessHelper.PhotoAsset, context: Context) {
      let requestOptions: photoAccessHelper.RequestOptions = {
        // 按照业务需求配置回图模式。
        // FAST_MODE:仅接收一阶段低质量图回调。
        // HIGH_QUALITY_MODE:仅接收二阶段全质量图回调。
        // BALANCE_MODE:接收一阶段及二阶段图片回调。
        deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE,
      }
      const handler = new MediaDataHandler();
      await photoAccessHelper.MediaAssetManager.requestImageData(context, photoAsset, requestOptions, handler);
      console.info('requestImageData successfully');
    }

    落盘图片参考媒体库接口:saveCameraPhoto

    请求图片参考媒体库接口:requestImageData 和 onDataPrepared

  4. 拍照时的会话配置及触发拍照的方式,与普通拍照相同,请参考拍照的步骤4-5。

状态监听

在相机应用开发过程中,可以随时监听拍照输出流状态,包括拍照流开始、拍照帧的开始与结束、拍照输出流的错误。