#鸿蒙通关秘籍#如何在HarmonyOS NEXT中结合双路预览功能进行实时OCR识别?

HarmonyOS
2024-11-28 15:25:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
网络小鬼才

在HarmonyOS NEXT中,实现双路预览结合OCR识别需遵循以下步骤:

  1. 创建XComponentPage页面并添加相机预览视图。在XComponent中,调用getXComponentSurfaceId获取Surface ID,并使用此ID创建预览流。

    XComponent({
      id: 'LOXComponent',
      type: XComponentType.SURFACE,
      libraryname: 'SingleXComponent',
      controller: this.mXComponentController
    })
    .onLoad(() => {
      this.mXComponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight: 1080 });
      this.xComponentSurfaceId = this.mXComponentController.getXComponentSurfaceId();
    })
    .width("100%")
    .height(display.getDefaultDisplaySync().width * 9 / 16)
    
  2. 创建双路预览函数,通过传递的Surface IDImageReceiver实例实现实时的多路相机图像预览。

    import camera from '@ohos.multimedia.camera';
    
    async createDualChannelPreview(cameraManager: camera.CameraManager, XComponentSurfaceId: string, receiver: image.ImageReceiver): Promise<void> {
      let camerasDevices = cameraManager.getSupportedCameras();
      let profiles = cameraManager.getSupportedOutputCapability(camerasDevices[0], camera.SceneMode.NORMAL_PHOTO);
      let previewProfiles = profiles.previewProfiles;
    
      let previewOutput = cameraManager.createPreviewOutput(previewProfiles[0], XComponentSurfaceId);
      let imageReceiverSurfaceId = await receiver.getReceivingSurfaceId();
      let previewOutput2 = cameraManager.createPreviewOutput(previewProfiles[0], imageReceiverSurfaceId);
    
      let cameraInput = cameraManager.createCameraInput(camerasDevices[0]);
      await cameraInput.open();
    
      let photoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
      photoSession.beginConfig();
      photoSession.addInput(cameraInput);
      photoSession.addOutput(previewOutput);
      photoSession.addOutput(previewOutput2);
      await photoSession.commitConfig();
      await photoSession.start();
    }
    
  3. 使用ImageReceiver获取预览图像并进行OCR识别。

    onImageArrival(receiver: image.ImageReceiver): void {
      receiver.on('imageArrival', () => {
        receiver.readNextImage((err, nextImage) => {
          if (err || !nextImage) {
            console.error('readNextImage failed');
            return;
          }
          nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent) => {
            if (err || !imgComponent) {
              console.error('getComponent failed');
              return;
            }
    
            if (imgComponent.byteBuffer as ArrayBuffer) {
              let imageSource = image.createImageSource(imgComponent.byteBuffer as ArrayBuffer);
              try {
                let imagePm = await imageSource.createPixelMap({ desiredSize: { width: 1920, height: 1080 } });
                ImageOCRUtil.recognizeText(imagePm, (res) => {
                  console.info(res);
                });
              } catch (err) {
                console.error(JSON.stringify(err));
              }
            }
            nextImage.release();
          });
        });
      });
    }
    
分享
微博
QQ
微信
回复
2024-11-28 16:20:04
相关问题
有没有实时相机预览结合OCR识别的demo
1687浏览 • 1回复 待解决
如何实现预览+录制功能
998浏览 • 1回复 待解决