1# 双路预览(ArkTS) 2 3在开发相机应用时,需要先参考开发准备[申请相关权限](camera-preparation.md)。 4 5双路预览,即应用可同时使用两路预览流,一路用于在屏幕上显示,一路用于图像处理等其他操作,提升处理效率。 6 7相机应用通过控制相机,实现图像显示(预览)、照片保存(拍照)、视频录制(录像)等基础操作。相机开发模型为Surface模型,即应用通过Surface进行数据传递,通过ImageReceiver的surface获取拍照流的数据、通过XComponent的surface获取预览流的数据。 8 9如果要实现双路预览,即将拍照流改为预览流,将拍照流中的surface改为预览流的surface,通过ImageReceiver的surface创建previewOutput,其余流程与拍照流和预览流一致。 10 11详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/js-apis-camera.md)。 12 13## 约束与限制 14 15- 暂不支持动态添加流,即不能在没有调用[session.stop](../../reference/apis-camera-kit/js-apis-camera.md#stop11)的情况下,调用[addOutput](../../reference/apis-camera-kit/js-apis-camera.md#addoutput11)添加流。 16- 对ImageReceiver组件获取到的图像数据处理后,需要将对应的图像Buffer释放,确保Surface的BufferQueue正常轮转。 17 18## 调用流程 19 20双路方案调用流程图建议如下: 21 22 23 24## 开发步骤 25 26- 用于处理图像的第一路预览流:创建ImageReceiver对象,获取SurfaceId创建第一路预览流,注册图像监听,按需处理预览流每帧图像。 27- 用于显示画面的第二路预览流:创建XComponent组件,获取SurfaceId创建第二路预览流,预览流画面直接在组件内渲染。 28- 创建预览流获取数据:创建上述两路预览流,配置进相机会话,启动会话后,两路预览流同时获取数据。 29 30### 用于处理图像的第一路预览流 31 321. 获取第一路预览流SurfaceId:创建ImageReceiver对象,通过ImageReceiver对象可获取其SurfaceId。 33 34 ```ts 35 import { image } from '@kit.ImageKit'; 36 let imageWidth: number = 1920; // 请使用设备支持profile的size的宽。 37 let imageHeight: number = 1080; // 请使用设备支持profile的size的高。 38 39 async function initImageReceiver():Promise<void>{ 40 // 创建ImageReceiver对象。 41 let size: image.Size = { width: imageWidth, height: imageHeight }; 42 let imageReceiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8); 43 // 获取取第一路流SurfaceId。 44 let imageReceiverSurfaceId = await imageReceiver.getReceivingSurfaceId(); 45 console.info(`initImageReceiver imageReceiverSurfaceId:${imageReceiverSurfaceId}`); 46 } 47 ``` 48 492. 注册监听处理预览流每帧图像数据:通过ImageReceiver组件中imageArrival事件监听获取底层返回的图像数据,详细的API说明请参考[Image API参考](../../reference/apis-image-kit/js-apis-image.md)。 50 51 ```ts 52 import { BusinessError } from '@kit.BasicServicesKit'; 53 import { image } from '@kit.ImageKit'; 54 55 function onImageArrival(receiver: image.ImageReceiver): void { 56 // 注册imageArrival监听。 57 receiver.on('imageArrival', () => { 58 // 获取图像。 59 receiver.readNextImage((err: BusinessError, nextImage: image.Image) => { 60 if (err || nextImage === undefined) { 61 console.error('readNextImage failed'); 62 return; 63 } 64 // 解析图像内容。 65 nextImage.getComponent(image.ComponentType.JPEG, async (err: BusinessError, imgComponent: image.Component) => { 66 if (err || imgComponent === undefined) { 67 console.error('getComponent failed'); 68 } 69 if (imgComponent.byteBuffer) { 70 // 详情见下方解析图片buffer数据参考,本示例以方式一为例。 71 let width = nextImage.size.width; // 获取图片的宽。 72 let height = nextImage.size.height; // 获取图片的高。 73 let stride = imgComponent.rowStride; // 获取图片的stride。 74 console.debug(`getComponent with width:${width} height:${height} stride:${stride}`); 75 // stride与width一致。 76 if (stride == width) { 77 let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { 78 size: { height: height, width: width }, 79 srcPixelFormat: 8, 80 }) 81 } else { 82 // stride与width不一致。 83 const dstBufferSize = width * height * 1.5 84 const dstArr = new Uint8Array(dstBufferSize) 85 for (let j = 0; j < height * 1.5; j++) { 86 const srcBuf = new Uint8Array(imgComponent.byteBuffer, j * stride, width) 87 dstArr.set(srcBuf, j * width) 88 } 89 let pixelMap = await image.createPixelMap(dstArr.buffer, { 90 size: { height: height, width: width }, 91 srcPixelFormat: 8, 92 }) 93 } 94 } else { 95 console.error('byteBuffer is null'); 96 } 97 // 确保当前buffer没有在使用的情况下,可进行资源释放。 98 // 如果对buffer进行异步操作,需要在异步操作结束后再释放该资源(nextImage.release())。 99 nextImage.release(); 100 }) 101 }) 102 }) 103 } 104 ``` 105 106 通过 [image.Component](../../reference/apis-image-kit/js-apis-image.md#component9) 解析图片buffer数据参考: 107 108 > **注意:** 109 > 需要确认图像的宽width是否与行距rowStride一致,如果不一致可参考以下方式处理: 110 111 方式一:去除imgComponent.byteBuffer中stride数据,拷贝得到新的buffer,调用不支持stride的接口处理buffer。 112 113 ```ts 114 // 以NV21为例(YUV_420_SP格式的图片)YUV_420_SP内存计算公式:长x宽+(长x宽)/2。 115 const dstBufferSize = width * height * 1.5; 116 const dstArr = new Uint8Array(dstBufferSize); 117 // 逐行读取buffer数据。 118 for (let j = 0; j < height * 1.5; j++) { 119 // imgComponent.byteBuffer的每行数据拷贝前width个字节到dstArr中。 120 const srcBuf = new Uint8Array(imgComponent.byteBuffer, j * stride, width); 121 dstArr.set(srcBuf, j * width); 122 } 123 let pixelMap = await image.createPixelMap(dstArr.buffer, { 124 size: { height: height, width: width }, srcPixelFormat: 8 125 }); 126 ``` 127 128 方式二:根据stride*height创建pixelMap,然后调用pixelMap的cropSync方法裁剪掉多余的像素。 129 130 ```ts 131 // 创建pixelMap,width宽传行距stride的值。 132 let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { 133 size:{height: height, width: stride}, srcPixelFormat: 8}); 134 // 裁剪多余的像素。 135 pixelMap.cropSync({size:{width:width, height:height}, x:0, y:0}); 136 ``` 137 138 方式三:将原始imgComponent.byteBuffer和stride信息一起传给支持stride的接口处理。 139 140 141 142### 用于显示画面的第二路预览流 143 144获取第二路预览流SurfaceId:创建XComponent组件用于预览流显示,获取surfaceId请参考XComponent组件提供的[getXcomponentSurfaceId](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md#getxcomponentsurfaceid9)方法,而XComponent的能力由UI提供,相关介绍可参考[XComponent组件参考](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md)。 145 146```ts 147@Component 148struct example { 149 xComponentCtl: XComponentController = new XComponentController(); 150 surfaceId:string = ''; 151 imageWidth: number = 1920; 152 imageHeight: number = 1080; 153 154 build() { 155 XComponent({ 156 id: 'componentId', 157 type: 'surface', 158 controller: this.xComponentCtl 159 }) 160 .onLoad(async () => { 161 console.info('onLoad is called'); 162 this.surfaceId = this.xComponentCtl.getXComponentSurfaceId(); // 获取组件surfaceId。 163 // 使用surfaceId创建预览流,开启相机,组件实时渲染每帧预览流数据。 164 }) 165 // surface的宽、高设置与XComponent组件的宽、高设置相反,或使用.renderFit(RenderFit.RESIZE_CONTAIN)自动填充显示无需设置宽、高。 166 .width(px2vp(this.imageHeight)) 167 .height(px2vp(this.imageWidth)) 168 } 169} 170``` 171 172 173 174### 创建预览流获取数据 175 176通过两个SurfaceId分别创建两路预览流输出,加入相机会话,启动相机会话,获取预览流数据。 177 178```ts 179function createDualPreviewOutput(cameraManager: camera.CameraManager, previewProfile: camera.Profile, 180session: camera.Session, 181imageReceiverSurfaceId: string, xComponentSurfaceId: string): void { 182 // 使用imageReceiverSurfaceId创建第一路预览。 183 let previewOutput1 = cameraManager.createPreviewOutput(previewProfile, imageReceiverSurfaceId); 184 if (!previewOutput1) { 185 console.error('createPreviewOutput1 error'); 186 } 187 // 使用xComponentSurfaceId创建第二路预览。 188 let previewOutput2 = cameraManager.createPreviewOutput(previewProfile, xComponentSurfaceId); 189 if (!previewOutput2) { 190 console.error('createPreviewOutput2 error'); 191 } 192 // 添加第一路预览流输出。 193 session.addOutput(previewOutput1); 194 // 添加第二路预览流输出。 195 session.addOutput(previewOutput2); 196} 197``` 198 199## 完整示例 200 201```ts 202import { camera } from '@kit.CameraKit'; 203import { image } from '@kit.ImageKit'; 204import { BusinessError } from '@kit.BasicServicesKit'; 205 206@Entry 207@Component 208struct Index { 209 private imageReceiver: image.ImageReceiver | undefined = undefined; 210 private imageReceiverSurfaceId: string = ''; 211 private xComponentCtl: XComponentController = new XComponentController(); 212 private xComponentSurfaceId: string = ''; 213 @State imageWidth: number = 1920; 214 @State imageHeight: number = 1080; 215 private cameraManager: camera.CameraManager | undefined = undefined; 216 private cameras: Array<camera.CameraDevice> | Array<camera.CameraDevice> = []; 217 private cameraInput: camera.CameraInput | undefined = undefined; 218 private previewOutput1: camera.PreviewOutput | undefined = undefined; 219 private previewOutput2: camera.PreviewOutput | undefined = undefined; 220 private session: camera.VideoSession | undefined = undefined; 221 222 onPageShow(): void { 223 console.info('onPageShow'); 224 this.initImageReceiver(); 225 if (this.xComponentSurfaceId !== '') { 226 this.initCamera(); 227 } 228 } 229 230 onPageHide(): void { 231 console.info('onPageHide'); 232 this.releaseCamera(); 233 } 234 235 /** 236 * 获取ImageReceiver的SurfaceId 237 * @param receiver 238 * @returns 239 */ 240 async initImageReceiver(): Promise<void> { 241 if (!this.imageReceiver) { 242 // 创建ImageReceiver。 243 let size: image.Size = { width: this.imageWidth, height: this.imageHeight }; 244 this.imageReceiver = image.createImageReceiver(size, image.ImageFormat.JPEG, 8); 245 // 获取取第一路流SurfaceId。 246 this.imageReceiverSurfaceId = await this.imageReceiver.getReceivingSurfaceId(); 247 console.info(`initImageReceiver imageReceiverSurfaceId:${this.imageReceiverSurfaceId}`); 248 // 注册监听处理预览流每帧图像数据。 249 this.onImageArrival(this.imageReceiver); 250 } 251 } 252 253 /** 254 * 注册ImageReceiver图像监听 255 * @param receiver 256 */ 257 onImageArrival(receiver: image.ImageReceiver): void { 258 // 注册imageArrival监听。 259 receiver.on('imageArrival', () => { 260 console.info('image arrival'); 261 // 获取图像。 262 receiver.readNextImage((err: BusinessError, nextImage: image.Image) => { 263 if (err || nextImage === undefined) { 264 console.error('readNextImage failed'); 265 return; 266 } 267 // 解析图像内容。 268 nextImage.getComponent(image.ComponentType.JPEG, async (err: BusinessError, imgComponent: image.Component) => { 269 if (err || imgComponent === undefined) { 270 console.error('getComponent failed'); 271 } 272 if (imgComponent.byteBuffer) { 273 // 请参考步骤7解析buffer数据,本示例以方式一为例。 274 let width = nextImage.size.width; // 获取图片的宽。 275 let height = nextImage.size.height; // 获取图片的高。 276 let stride = imgComponent.rowStride; // 获取图片的stride。 277 console.debug(`getComponent with width:${width} height:${height} stride:${stride}`); 278 // stride与width一致。 279 if (stride == width) { 280 let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { 281 size: { height: height, width: width }, 282 srcPixelFormat: 8, 283 }) 284 } else { 285 // stride与width不一致。 286 const dstBufferSize = width * height * 1.5 // 以NV21为例(YUV_420_SP格式的图片)YUV_420_SP内存计算公式:长x宽+(长x宽)/2。 287 const dstArr = new Uint8Array(dstBufferSize) 288 for (let j = 0; j < height * 1.5; j++) { 289 const srcBuf = new Uint8Array(imgComponent.byteBuffer, j * stride, width) 290 dstArr.set(srcBuf, j * width) 291 } 292 let pixelMap = await image.createPixelMap(dstArr.buffer, { 293 size: { height: height, width: width }, 294 srcPixelFormat: 8, 295 }) 296 } 297 } else { 298 console.error('byteBuffer is null'); 299 } 300 // 确保当前buffer没有在使用的情况下,可进行资源释放。 301 // 如果对buffer进行异步操作,需要在异步操作结束后再释放该资源(nextImage.release())。 302 nextImage.release(); 303 console.info('image process done'); 304 }) 305 }) 306 }) 307 } 308 309 build() { 310 Column() { 311 XComponent({ 312 id: 'componentId', 313 type: 'surface', 314 controller: this.xComponentCtl 315 }) 316 .onLoad(async () => { 317 console.info('onLoad is called'); 318 this.xComponentSurfaceId = this.xComponentCtl.getXComponentSurfaceId(); // 获取组件surfaceId。 319 // 初始化相机,组件实时渲染每帧预览流数据。 320 this.initCamera() 321 }) 322 .width(px2vp(this.imageHeight)) 323 .height(px2vp(this.imageWidth)) 324 }.justifyContent(FlexAlign.Center) 325 .height('100%') 326 .width('100%') 327 } 328 329 // 初始化相机。 330 async initCamera(): Promise<void> { 331 console.info(`initCamera imageReceiverSurfaceId:${this.imageReceiverSurfaceId} xComponentSurfaceId:${this.xComponentSurfaceId}`); 332 try { 333 // 获取相机管理器实例。 334 this.cameraManager = camera.getCameraManager(getContext(this)); 335 if (!this.cameraManager) { 336 console.error('initCamera getCameraManager'); 337 } 338 // 获取当前设备支持的相机device列表。 339 this.cameras = this.cameraManager.getSupportedCameras(); 340 if (!this.cameras) { 341 console.error('initCamera getSupportedCameras'); 342 } 343 // 选择一个相机device,创建cameraInput输出对象。 344 this.cameraInput = this.cameraManager.createCameraInput(this.cameras[0]); 345 if (!this.cameraInput) { 346 console.error('initCamera createCameraInput'); 347 } 348 // 打开相机。 349 await this.cameraInput.open().catch((err: BusinessError) => { 350 console.error(`initCamera open fail: ${JSON.stringify(err)}`); 351 }) 352 // 获取相机device支持的profile。 353 let capability: camera.CameraOutputCapability = 354 this.cameraManager.getSupportedOutputCapability(this.cameras[0], camera.SceneMode.NORMAL_VIDEO); 355 if (!capability) { 356 console.error('initCamera getSupportedOutputCapability'); 357 } 358 // 根据业务需求选择一个支持的预览流profile。 359 let previewProfile: camera.Profile = capability.previewProfiles[0]; 360 this.imageWidth = previewProfile.size.width; // 更新xComponent组件的宽。 361 this.imageHeight = previewProfile.size.height; // 更新xComponent组件的高。 362 console.info(`initCamera imageWidth:${this.imageWidth} imageHeight:${this.imageHeight}`); 363 // 使用imageReceiverSurfaceId创建第一路预览。 364 this.previewOutput1 = this.cameraManager.createPreviewOutput(previewProfile, this.imageReceiverSurfaceId); 365 if (!this.previewOutput1) { 366 console.error('initCamera createPreviewOutput1'); 367 } 368 // 使用xComponentSurfaceId创建第二路预览。 369 this.previewOutput2 = this.cameraManager.createPreviewOutput(previewProfile, this.xComponentSurfaceId); 370 if (!this.previewOutput2) { 371 console.error('initCamera createPreviewOutput2'); 372 } 373 // 创建录像模式相机会话。 374 this.session = this.cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession; 375 if (!this.session) { 376 console.error('initCamera createSession'); 377 } 378 // 开始配置会话。 379 this.session.beginConfig(); 380 // 添加相机设备输入。 381 this.session.addInput(this.cameraInput); 382 // 添加第一路预览流输出。 383 this.session.addOutput(this.previewOutput1); 384 // 添加第二路预览流输出。 385 this.session.addOutput(this.previewOutput2); 386 // 提交会话配置。 387 await this.session.commitConfig(); 388 // 开始启动已配置的输入输出流。 389 await this.session.start(); 390 } catch (error) { 391 console.error(`initCamera fail: ${JSON.stringify(error)}`); 392 } 393 } 394 395 // 释放相机。 396 async releaseCamera(): Promise<void> { 397 console.info('releaseCamera E'); 398 try { 399 // 停止当前会话。 400 await this.session?.stop(); 401 // 释放相机输入流。 402 await this.cameraInput?.close(); 403 // 释放预览输出流。 404 await this.previewOutput1?.release(); 405 // 释放拍照输出流。 406 await this.previewOutput2?.release(); 407 // 释放会话。 408 await this.session?.release(); 409 } catch (error) { 410 console.error(`initCamera fail: ${JSON.stringify(error)}`); 411 } 412 } 413} 414```