1# 分段式拍照(ArkTS) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--Designer: @leo_ysl--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9分段式拍照是相机的重要功能之一,即应用下发拍照任务后,系统将分多阶段上报不同质量的图片。 10 11- 在第一阶段,系统快速上报轻量处理的图片,轻量处理的图片比全质量图低,出图速度快。应用通过回调会收到一个PhotoAsset对象,通过该对象可调用媒体库接口,读取图片或落盘图片。 12- 在第二阶段,相机框架会根据应用的请求图片诉求或者在系统闲时,进行图像增强处理得到全质量图,将处理好的图片传回给媒体库,替换轻量处理的图片。 13 14通过分段式拍照,优化了系统的拍照响应时延,从而提升用户体验。 15 16应用开发分段式拍照主要分为以下步骤: 17 18- 通过PhotoOutput,监听photoAssetAvailable回调,获取[photoAccessHelper](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper.md)的PhotoAsset对象。 19- 通过PhotoAsset对象,调用媒体库相关接口,读取或落盘图片。 20 21> **说明:** 22> 23> - 分段式拍照能力是根据**设备**和**模式**决定的,不同的设备支持不同的模式,不同的模式下分段式能力也各有不同,所以应用在切换设备或模式后分段式能力可能会发生变化。 24> - 分段式拍照能力应用无需主动使能,相机框架会在配流期间判断设备和模式是否支持分段式,如果支持会使能分段式拍照。 25 26## 开发步骤 27 28详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/arkts-apis-camera.md)。 29 301. 导入依赖,需要导入相机框架、媒体库、图片相关领域依赖。 31 32 ```ts 33 import { camera } from '@kit.CameraKit'; 34 import { BusinessError } from '@kit.BasicServicesKit'; 35 import { photoAccessHelper } from '@kit.MediaLibraryKit'; 36 ``` 37 382. 确定拍照输出流。 39 40 通过[CameraOutputCapability](../../reference/apis-camera-kit/arkts-apis-camera-i.md#cameraoutputcapability)中的photoProfiles属性,可获取当前设备支持的拍照输出流,通过[createPhotoOutput](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#createphotooutput11)方法创建拍照输出流。 41 42 ```ts 43 function getPhotoOutput(cameraManager: camera.CameraManager, 44 cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput | undefined { 45 let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles; 46 if (photoProfilesArray===null || photoProfilesArray===undefined) { 47 console.error("createOutput photoProfilesArray is null!"); 48 return undefined; 49 } 50 let photoOutput: camera.PhotoOutput | undefined = undefined; 51 try { 52 if (photoProfilesArray.length > 0) { 53 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 54 } else { 55 console.log("the length of photoProfilesArray<=0!"); 56 return undefined; 57 } 58 } catch (error) { 59 let err = error as BusinessError; 60 console.error(`Failed to createPhotoOutput. error: ${err}`); 61 } 62 return photoOutput; 63 } 64 ``` 65 663. 设置拍照photoAssetAvailable的回调。 67 68 > **注意:** 69 > 70 > 如果已经注册了photoAssetAvailable回调,并且在Session开始之后又注册了photoAvailable回调,photoAssetAvailable和photoAvailable同时注册,会导致流被重启,仅photoAssetAvailable生效。 71 > 72 > 不建议开发者同时注册[photoAvailable](../../reference/apis-camera-kit/arkts-apis-camera-PhotoOutput.md#onphotoavailable11)和[photoAssetAvailable](../../reference/apis-camera-kit/arkts-apis-camera-PhotoOutput.md#onphotoassetavailable12)。 73 74 ```ts 75 function getPhotoAccessHelper(context: Context): photoAccessHelper.PhotoAccessHelper { 76 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 77 return phAccessHelper; 78 } 79 80 function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput, context: Context): void { 81 photoOutput.on('photoAssetAvailable', (err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset) => { 82 if (err) { 83 console.error(`photoAssetAvailable error: ${err}.`); 84 return; 85 } 86 console.info('photoOutPutCallBack photoAssetAvailable'); 87 // 开发者可通过photoAsset调用媒体库相关接口,自定义处理图片。 88 // 处理方式一:调用媒体库落盘接口保存一阶段图,二阶段图就绪后媒体库会主动帮应用替换落盘图片。 89 mediaLibSavePhoto(photoAsset, getPhotoAccessHelper(context)); 90 // 处理方式二:调用媒体库接口请求图片并注册一阶段图或二阶段图buffer回调,自定义使用。 91 mediaLibRequestBuffer(photoAsset, context); 92 }); 93 } 94 95 async function mediaLibSavePhoto(photoAsset: photoAccessHelper.PhotoAsset, 96 phAccessHelper: photoAccessHelper.PhotoAccessHelper): Promise<void> { 97 try { 98 let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset); 99 assetChangeRequest.saveCameraPhoto(); 100 await phAccessHelper.applyChanges(assetChangeRequest); 101 console.info('apply saveCameraPhoto successfully'); 102 } catch (err) { 103 console.error(`apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`); 104 } 105 } 106 107 class MediaDataHandler implements photoAccessHelper.MediaAssetDataHandler<ArrayBuffer> { 108 onDataPrepared(data: ArrayBuffer) { 109 if (data === undefined) { 110 console.error('Error occurred when preparing data'); 111 return; 112 } 113 // 应用获取到图片buffer后可自定义处理。 114 console.info('on image data prepared'); 115 } 116 } 117 118 async function mediaLibRequestBuffer(photoAsset: photoAccessHelper.PhotoAsset, context: Context) { 119 let requestOptions: photoAccessHelper.RequestOptions = { 120 // 按照业务需求配置回图模式。 121 // FAST_MODE:仅接收一阶段低质量图回调。 122 // HIGH_QUALITY_MODE:仅接收二阶段全质量图回调。 123 // BALANCE_MODE:接收一阶段及二阶段图片回调。 124 deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE, 125 } 126 const handler = new MediaDataHandler(); 127 await photoAccessHelper.MediaAssetManager.requestImageData(context, photoAsset, requestOptions, handler); 128 console.info('requestImageData successfully'); 129 } 130 ``` 131 132 落盘图片参考媒体库接口:[saveCameraPhoto](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#savecameraphoto12) 133 134 请求图片参考媒体库接口:[requestImageData](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetManager.md#requestimagedata11) 和 [onDataPrepared](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetDataHandler.md#ondataprepared11) 135 1364. 拍照时的会话配置及触发拍照的方式,与普通拍照相同,请参考[拍照](camera-shooting.md)的步骤4-5。 137 138## 状态监听 139 140在相机应用开发过程中,可以随时监听拍照输出流状态,包括拍照流开始、拍照帧的开始与结束、拍照输出流的错误。 141 142- 通过注册固定的captureStartWithInfo回调函数获取监听拍照开始结果,photoOutput创建成功时即可监听,相机设备已经准备开始这次拍照时触发,该事件返回此次拍照的captureId。 143 144 ```ts 145 function onPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 146 photoOutput.on('captureStartWithInfo', (err: BusinessError, captureStartInfo: camera.CaptureStartInfo) => { 147 if (err !== undefined && err.code !== 0) { 148 return; 149 } 150 console.info(`photo capture started, captureId : ${captureStartInfo.captureId}`); 151 }); 152 } 153 ``` 154 155- 通过注册固定的captureEnd回调函数获取监听拍照结束结果,photoOutput创建成功时即可监听,该事件返回结果为拍照完全结束后的相关信息[CaptureEndInfo](../../reference/apis-camera-kit/arkts-apis-camera-i.md#captureendinfo)。 156 157 ```ts 158 function onPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 159 photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { 160 if (err !== undefined && err.code !== 0) { 161 return; 162 } 163 console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`); 164 console.info(`frameCount : ${captureEndInfo.frameCount}`); 165 }); 166 } 167 ``` 168 169- 通过注册固定的captureReady回调函数获取监听可拍下一张结果,photoOutput创建成功时即可监听,当下一张可拍时触发,该事件返回结果为下一张可拍的相关信息。 170 171 ```ts 172 function onPhotoOutputCaptureReady(photoOutput: camera.PhotoOutput): void { 173 photoOutput.on('captureReady', (err: BusinessError) => { 174 if (err !== undefined && err.code !== 0) { 175 return; 176 } 177 console.info(`photo capture ready`); 178 }); 179 } 180 ``` 181 182- 通过注册固定的error回调函数获取监听拍照输出流的错误结果。callback返回拍照输出接口使用错误时的对应错误码,错误码类型参见[Camera错误码](../../reference/apis-camera-kit/arkts-apis-camera-e.md#cameraerrorcode)。 183 184 ```ts 185 function onPhotoOutputError(photoOutput: camera.PhotoOutput): void { 186 photoOutput.on('error', (error: BusinessError) => { 187 console.error(`Photo output error code: ${error.code}`); 188 }); 189 } 190 ```