diff --git a/zh-cn/application-dev/media/camera/camera-dual-channel-preview.md b/zh-cn/application-dev/media/camera/camera-dual-channel-preview.md index 64afa772722c220e5f3d0f6aafd2e1d637e503ea..9024a5b0de630b32af991e47372b5d1848caa906 100644 --- a/zh-cn/application-dev/media/camera/camera-dual-channel-preview.md +++ b/zh-cn/application-dev/media/camera/camera-dual-channel-preview.md @@ -55,6 +55,10 @@ 3. 注册监听处理预览流每帧图像数据:通过ImageReceiver组件中imageArrival事件监听获取底层返回的图像数据,详细的API说明请参考[Image API参考](../../reference/apis-image-kit/js-apis-image.md)。 +> **说明:** +> +> ImageReceiver组件中解析图像数据设置的Size、Format等属性必须和相机预览输出流previewProfile中配置的Size、Format保持一致,ImageRecevier图片像素格式请参考[PixelMapFormat](../../reference/apis-image-kit/js-apis-image.md#pixelmapformat7),相机预览输出流previewProfile输出格式请参考[CameraFormat](../../reference/apis-camera-kit/arkts-apis-camera-e.md#cameraformat)。 + ```ts function onImageArrival(receiver: image.ImageReceiver): void { // 注册imageArrival监听。 @@ -79,8 +83,9 @@ // stride与width一致。 if (stride == width) { let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { + // pixelMap创建的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 size: { height: height, width: width }, - srcPixelFormat: 8, + srcPixelFormat: image.PixelMapFormat.RGBA_8888, }) } else { // stride与width不一致。 @@ -91,8 +96,9 @@ dstArr.set(srcBuf, j * width) } let pixelMap = await image.createPixelMap(dstArr.buffer, { + // pixelMap创建的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 size: { height: height, width: width }, - srcPixelFormat: 8, + srcPixelFormat: image.PixelMapFormat.NV21, }) } } else { @@ -125,7 +131,8 @@ dstArr.set(srcBuf, j * width); } let pixelMap = await image.createPixelMap(dstArr.buffer, { - size: { height: height, width: width }, srcPixelFormat: 8 + // pixelMap创建使用的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 + size: { height: height, width: width }, srcPixelFormat: image.PixelMapFormat.NV21 }); ``` @@ -134,7 +141,8 @@ ```ts // 创建pixelMap,width宽传行距stride的值。 let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { - size:{height: height, width: stride}, srcPixelFormat: 8}); + // pixelMap创建使用的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 + size:{height: height, width: stride}, srcPixelFormat: image.PixelMapFormat.NV21}); // 裁剪多余的像素。 pixelMap.cropSync({size:{width:width, height:height}, x:0, y:0}); ``` @@ -305,12 +313,14 @@ struct Index { // stride与width一致。 if (stride == width) { let pixelMap = await image.createPixelMap(imgComponent.byteBuffer, { + // pixelMap创建使用的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 size: { height: height, width: width }, - srcPixelFormat: 8, + srcPixelFormat: image.PixelMapFormat.NV21, }) } else { // stride与width不一致。 - const dstBufferSize = width * height * 1.5 // 以NV21为例(YUV_420_SP格式的图片)YUV_420_SP内存计算公式:长x宽+(长x宽)/2。 + // 以NV21为例(YUV_420_SP格式的图片)YUV_420_SP内存计算公式:长x宽+(长x宽)/2。 + const dstBufferSize = width * height * 1.5 const dstArr = new Uint8Array(dstBufferSize) for (let j = 0; j < height * 1.5; j++) { const srcBuf = new Uint8Array(imgComponent.byteBuffer, j * stride, width) @@ -318,7 +328,8 @@ struct Index { } let pixelMap = await image.createPixelMap(dstArr.buffer, { size: { height: height, width: width }, - srcPixelFormat: 8, + // pixelMap创建使用的size、srcPixelFormat需要与相机预览输出流previewProfile中的size、format保持一致。 + srcPixelFormat: image.PixelMapFormat.NV21, }) } } else { @@ -384,9 +395,20 @@ struct Index { this.cameraManager.getSupportedOutputCapability(this.cameras[0], camera.SceneMode.NORMAL_VIDEO); if (!capability) { console.error('initCamera getSupportedOutputCapability'); + } + let surfaceRatio : number = this.imageWidth / this.imageHeight; + let minRatioDiff : number = 0.01; + for (let index = 0; index < capability.previewProfiles.length; index++) { + const tempProfile = capability.previewProfiles[index]; + let tempRatio = tempProfile.size.width >= tempProfile.size.height ? + tempProfile.size.width / tempProfile.size.height : tempProfile.size.height / tempProfile.size.wight; + let currentRatio = Math.abs(tempRatio - surfacerRatio); + // 根据业务需求选择一个支持的预览流profile,此处以CAMERA_FORMAT_YUV_420_SP(NV21)为例。 + // 此处选择最接近16:9宽高比的分辨率Size。 + if (currentRatio <= 0.01 && tempProfile.format = camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP) { + previeProfile = tempProfile; + } } - // 根据业务需求选择一个支持的预览流profile。 - let previewProfile: camera.Profile = capability.previewProfiles[0]; this.imageWidth = previewProfile.size.width; // 更新xComponent组件的宽。 this.imageHeight = previewProfile.size.height; // 更新xComponent组件的高。 console.info(`initCamera imageWidth:${this.imageWidth} imageHeight:${this.imageHeight}`);