1# 性能提升方案(仅对系统应用开放) 2 3相机启动性能受限于底层器件上电、流程Pipeline初始化等耗时操作影响,本文档将为开发者提供更进一步的指导,提升相机启动速度以及拍照返回缩略图速度。相关能力与底层器件相关,请开发者在使用前需确认是否支持相关特性。 4 5相关特性分别在打开相机设备过程、配流过程以及拍照过程中。本文档针对三个场景分别进行介绍。 6 7## 延时配流 8 9经典的相机启动过程经过“相机设备打开”、“配置数据流”、“启动数据流”等流程,而配流启流之前需要得到XComponent组件的surfaceId。 10 11延时配流方案是把配流启流与surface解耦,在组件尚未给应用surface之前,可以先进行配流启流,只需要在启流结束之前提供surface,可以提升启动速度,防止影响其他启动优化方案的落地。 12 13 14 15优化前:配流动作依赖surface对象,surface对象依赖于UI加载完成。也就是在UI加载完成后,才可以创建Session、配置输入输出流、启动Session,由相机HDI进行配流。 16 17优化后:配流动作不依赖surface对象,界面加载和配流并行执行。完成参数准备后,即可开始创建Session。 18 19### 接口说明 20 21详细的API参考说明,请参考[Camera API文档](../reference/apis/js-apis-camera.md)。 22 23| 接口 | 说明 | 24| ---- | ---- | 25| createDeferredPreviewOutput(profile: Profile): Promise\<PreviewOutput> | 创建延迟预览输出对象,在配流时替代普通的预览输出对象加入数据流。 | 26| addDeferredSurface(surfaceId: string): Promise\<void> | 配置延迟预览的Surface,可以在session.commitConfig()配流和session.start()启流之后运行。 | 27 28### 开发示例 29 30接口调用流程建议如下图所示: 31 32 33 34[BaseContext获取方式](../reference/apis/js-apis-inner-application-baseContext.md)。 35 36```ts 37import camera from '@ohos.multimedia.camera'; 38import common from '@ohos.app.ability.common'; 39 40async function preview(baseContext: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, photoSurfaceId: string, previewSurfaceId: string): Promise<void> { 41 const cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 42 const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo); 43 const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 44 const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, photoSurfaceId); 45 const session: camera.CaptureSession = cameraManager.createCaptureSession(); 46 session.beginConfig(); 47 session.addInput(cameraInput); 48 session.addOutput(previewOutput); 49 session.addOutput(photoOutput); 50 await session.commitConfig(); 51 await session.start(); 52 previewOutput.addDeferredSurface(previewSurfaceId); 53} 54``` 55 56## 快速缩略图 57 58相机拍照性能依赖算法处理的速度,算法链越复杂、效果就越好,但同时处理时间就越长。 59 60通过相机快速缩略图,相机拍照可单独输出拇指缩略图,在真图没有上报之前,可以提前上报一张缩略图给应用去做显示,提升shot2see用户感知拍照速度。 61 62这样从拍照流程上进行优化,既可以满足后处理算法处理的要求,又不会阻塞前台的拍照速度。 63 64### 接口说明 65 66详细的API参考说明,请参考[Camera API文档](../reference/apis/js-apis-camera.md)。 67 68| 接口 | 说明 | 69| ---- | ---- | 70| isQuickThumbnailSupported() : boolean | 是否支持快速缩略图。 | 71| enableQuickThumbnail(enabled:bool): void | 使能/去使能快速缩略图。 | 72| on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void | 相机缩略图监听回调。 | 73 74> **说明:** 75> 76> - isQuickThumbnailSupported及enableQuickThumbnail接口的调用需要在CaptureSession.addOutput、CaptureSession.addInput后,CaptureSession.commitConfig()之前。 77> - on接口需要在enableQuickThumbnail(true)之后生效。 78 79### 开发示例 80 81接口调用流程建议如下图所示: 82 83 84 85[BaseContext获取方式](../reference/apis/js-apis-inner-application-baseContext.md)。 86```ts 87import camera from '@ohos.multimedia.camera'; 88import { BusinessError } from '@ohos.base'; 89import image from '@ohos.multimedia.image'; 90import common from '@ohos.app.ability.common'; 91 92async function enableQuickThumbnail(baseContext: common.BaseContext, surfaceId: string, photoProfile: camera.Profile): Promise<void> { 93 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 94 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 95 // 创建CaptureSession实例 96 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 97 // 开始配置会话 98 captureSession.beginConfig(); 99 // 把CameraInput加入到会话 100 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 101 cameraInput.open(); 102 captureSession.addInput(cameraInput); 103 // 把PhotoOutPut加入到会话 104 let photoOutPut: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 105 captureSession.addOutput(photoOutPut); 106 let isSupported: boolean = photoOutPut.isQuickThumbnailSupported(); 107 if (isSupported) { 108 // 使能快速缩略图 109 photoOutPut.enableQuickThumbnail(true); 110 photoOutPut.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => { 111 if (err || pixelMap === undefined) { 112 console.error('photoOutPut on thumbnail failed'); 113 return; 114 } 115 // 显示或保存pixelmap 116 showOrSavePicture(pixelMap); 117 }) 118 } 119} 120 121function showOrSavePicture(pixelMap: image.PixelMap): void { 122 //do something 123} 124``` 125 126## 预热启动 127 128普通情况下相机应用的启动是用户通过点击桌面相机图标触发的。桌面应用感知用户点击相机图标,然后通知应用管理器启动对应的相机应用(进程),这个过程是耗时较长。进入相机应用后,开始进入相机启动流程。经典的相机启动过程会经过,“相机设备打开”,“配置数据流”,“启动数据流等”,这个过程也较为耗时。 129 130相机启动方案是把“相机设备打开”这个动作提前到相机应用启动之前,即在用户点击相机图标, 131还没等相机应用启动的时候,触发相机设备打开的动作,从而缩短相机应用内启动相机的流程,加速相机启动。使用预热启动前后的相机应用流程对比如下: 132 133 134 135### 接口说明 136 137详细的API参考说明,请参考[Camera API文档](../reference/apis/js-apis-camera.md)。 138 139| 接口 | 说明 | 140| ---- | ---- | 141| isPrelaunchSupported(camera: CameraDevice) : boolean | 判断指定cameraDevice是否支持预热启动。 | 142| setPrelaunchConfig(prelaunchConfig: PrelaunchConfig) : void | 配置相机预热参数。 | 143| prelaunch() : void | 用户点击系统相机图标,拉起相机应用的同时调用,下发预热请求,使能相机预热启动。 | 144 145### 开发示例 146 147接口调用流程建议如下图所示: 148 149 150 151[BaseContext获取方式](../reference/apis/js-apis-inner-application-baseContext.md)。 152 153- **桌面应用** 154 155 ```ts 156 import camera from '@ohos.multimedia.camera'; 157 import { BusinessError } from '@ohos.base'; 158 import common from '@ohos.app.ability.common'; 159 160 function preLaunch(baseContext: common.BaseContext): void { 161 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 162 try { 163 cameraManager.prelaunch(); 164 } catch (error) { 165 let err = error as BusinessError; 166 console.error(`catch error: Code: ${err.code}, message: ${err.message}`); 167 } 168 } 169 ``` 170 171- **相机应用** 172 173 使用该功能前,应用需要**申请权限**:ohos.permission.CAMERA 174 175 具体申请方式及校验方式,请参考[访问控制授权申请指导](../security/accesstoken-guidelines.md)。 176 177 ```ts 178 import camera from '@ohos.multimedia.camera'; 179 import { BusinessError } from '@ohos.base'; 180 import common from '@ohos.app.ability.common'; 181 182 function setPreLaunchConfig(baseContext: common.BaseContext): void { 183 let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext); 184 let cameras: Array<camera.CameraDevice> = []; 185 try { 186 cameras = cameraManager.getSupportedCameras() 187 } catch (error) { 188 let err = error as BusinessError; 189 console.error(`getSupportedCameras catch error: Code: ${err.code}, message: ${err.message}`); 190 } 191 if (cameras.length <= 0) { 192 return; 193 } 194 if(cameraManager.isPrelaunchSupported(cameras[0])) { 195 try { 196 cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); 197 } catch (error) { 198 let err = error as BusinessError; 199 console.error(`setPrelaunchConfig catch error: Code: ${err.code}, message: ${err.message}`); 200 } 201 } 202 } 203 ``` 204