通过摄像头实时预览拍摄画面,并截取一帧用于识别转换PixelMap时报错

onImageArrival(receiver: image.ImageReceiver): void { 
  receiver.on('imageArrival', () => { 
  receiver.readNextImage((err: BusinessError, nextImage: image.Image) => { 
  if (err || nextImage === undefined) { 
  console.error('readNextImage failed'); 
  return; 
} 
nextImage.getComponent(image.ComponentType.JPEG, async (err: BusinessError, imgComponent: image.Component) => { 
  if (err || imgComponent === undefined) { 
    console.error('getComponent failed'); 
  } 
  if (imgComponent && imgComponent.byteBuffer as ArrayBuffer) { 
    let buffer = imgComponent.byteBuffer as ArrayBuffer 
    try { 
      let pixelMap: image.PixelMap = 
        await image.createImageSource(buffer).createPixelMap() 
      console.error(`result ${pixelMap.getDensity()}`); 
    } catch (e) { 
      console.error(JSON.stringify(e)) 
    } 
  } else { 
    console.error('byteBuffer is null'); 
  } 
  nextImage.release(); 
}) 
}) 
}) 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

详细日志:

05-27 16:46:22.152   12178-12424  C02B03/HeifFormatAgent         com.examp...lication  E     [nodict]head type is not ftyp. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/heif. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/jpeg. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/png. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/svg+xml. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/vnd.wap.wbmp. 
05-27 16:46:22.152   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]check mismatched format :image/webp. 
05-27 16:46:22.155   12178-12424  C02B03/RawDecoder              com.examp...lication  E     [nodict]DoDecodeHeader header decode fail, ret=[62980103] 
05-27 16:46:22.155   12178-12424  C02B03/RawDecoder              com.examp...lication  E     [nodict][GetImageSize]decode header error on get image size, ret:62980103. 
05-27 16:46:22.155   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]decode the image info fail. 
05-27 16:46:22.155   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]image info decode fail, ret:62980116. 
05-27 16:46:22.155   12178-12424  C02B05/ImageSource             com.examp...lication  E     [nodict][ImageSource]get valid image status fail on create pixel map, ret:62980116. 
05-27 16:46:22.155   12178-12424  C02B05/ImageSourceNapi         com.examp...lication  E     [nodict]Create PixelMap error
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
HarmonyOS
2024-11-13 11:44:14
728浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

从代码日志中查看错误码:62980103,可能出现问题的地方是:解码过程中异常退出。图片格式不支持。未读取到图片。参考关于相机能读取到每一帧的demo。

import camera from '@ohos.multimedia.camera'; 
import image from '@ohos.multimedia.image'; 
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 
import common from '@ohos.app.ability.common'; 
import fs from '@ohos.file.fs'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import PhotoAccessHelper from '@ohos.file.photoAccessHelper'; 
 
@Entry 
@Component 
struct Scan1 { 
  @State message: string = 'Hello World' 
  private mXComponentController: XComponentController = new XComponentController; 
  private surfaceId: string = '-1'; 
  @State imgUrl: PixelMap | undefined = undefined; 
  private context: ESObject = undefined 
  private previewProfilesObj2: camera.Profile | undefined = undefined; 
  private receiver: image.ImageReceiver | undefined = undefined; 
  @State pixma: PixelMap | undefined = undefined 
  @State photoOutput: camera.PhotoOutput | undefined = undefined; 
  aboutToAppear() { 
    //申请权限 
    let context = getContext() as common.UIAbilityContext; 
    abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, ['ohos.permission.CAMERA']).then(() => { 
 
      this.createDualChannelPreview(this.surfaceId); 
    }); 
 
  } 
  onPageShow(): void { 
    this.createDualChannelPreview(this.surfaceId); 
 
  } 
  async createDualChannelPreview(XComponentSurfaceId: string): Promise<void> { 
    let cameraManager = await camera.getCameraManager(getContext() as ESObject); 
    let camerasDevices: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); // 获取支持的相机设备对象 
    // 获取profile对象 
    let profiles: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camerasDevices[0]); // 获取对应相机设备profiles 
    let previewProfiles: Array<camera.Profile> = profiles.previewProfiles; 
    // 预览流2 
    this.previewProfilesObj2 = previewProfiles[0]; 
    this.receiver = image.createImageReceiver(this.previewProfilesObj2.size.width, this.previewProfilesObj2.size.height, 2000, 8); 
    // 创建 预览流2 输出对象 
    let imageReceiverSurfaceId: string = await this.receiver.getReceivingSurfaceId(); 
    let previewOutput2: camera.PreviewOutput = cameraManager.createPreviewOutput(this.previewProfilesObj2, imageReceiverSurfaceId); 
    // 创建拍照输出流 
    // let photoOutput: camera.PhotoOutput | undefined = undefined; 
    let photoProfilesArray: Array<camera.Profile> = profiles.photoProfiles; 
    try { 
      this.photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]); 
    } catch (error) { 
      let err = error as BusinessError; 
      console.error('Failed to createPhotoOutput errorCode = ' + err.code); 
    } 
    if (this.photoOutput === undefined) { 
      return; 
    } 
    // 创建cameraInput对象 
    let cameraInput: camera.CameraInput = cameraManager.createCameraInput(camerasDevices[0]); 
    // 打开相机 
    await cameraInput.open(); 
    // 会话流程 
    let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 
    // 开始配置会话 
    captureSession.beginConfig(); 
    // 把CameraInput加入到会话 
    captureSession.addInput(cameraInput); 
    // 把 预览流2 加入到会话 
    captureSession.addOutput(previewOutput2); 
    try { 
      captureSession.addOutput(this.photoOutput); 
    } catch (error) { 
      let err = error as BusinessError; 
      console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code); 
    } 
    // 提交配置信息 
    await captureSession.commitConfig(); 
    // 会话开始 
    await captureSession.start(); 
    this.onImageArrival(this.receiver); 
    this.setPhotoOutputCb(this.photoOutput) 
  } 
  async savePicture(buffer: ArrayBuffer, img: image.Image) { 
    const context = getContext(this); 
    let photoAccessHelper: PhotoAccessHelper.PhotoAccessHelper = PhotoAccessHelper.getPhotoAccessHelper(context); 
    let options: PhotoAccessHelper.CreateOptions = { 
      title: Date.now().toString() 
    }; 
    let photoUri: string = await photoAccessHelper.createAsset(PhotoAccessHelper.PhotoType.IMAGE, 'jpg', options); 
    //createAsset的调用需要ohos.permission.READ_IMAGEVIDEO和ohos.permission.WRITE_IMAGEVIDEO的权限 
    let file: fs.File = fs.openSync(photoUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
    await fs.write(file.fd, buffer); 
    fs.closeSync(file); 
    img.release(); 
  } 
  setPhotoOutputCb(photoOutput: camera.PhotoOutput) { 
    //设置回调之后,调用photoOutput的capture方法,就会将拍照的buffer回传到回调中 
    photoOutput.on('photoAvailable', (errCode: BusinessError, photo: camera.Photo): void => { 
      console.info('getPhoto start'); 
      console.info(`err: ${JSON.stringify(errCode)}`); 
      if (errCode || photo === undefined) { 
        console.error('getPhoto failed'); 
        return; 
      } 
      let imageObj = photo.main; 
      imageObj.getComponent(image.ComponentType.JPEG, (errCode: BusinessError, component: image.Component): void => { 
        console.info('getComponent start'); 
        if (errCode || component === undefined) { 
          console.error('getComponent failed'); 
          return; 
        } 
        let buffer: ArrayBuffer; 
        if (component.byteBuffer) { 
          buffer = component.byteBuffer; 
          if (component.byteBuffer as ArrayBuffer) { 
            let sourceOptions: image.SourceOptions = { 
              sourceDensity: 120, 
              // sourcePixelFormat: 3, // NV21 
              sourceSize: { 
                height: 240, 
                width: 320 
              }, 
            } 
            try { 
              let imageResource = image.createImageSource(component.byteBuffer); 
              imageResource.createPixelMap({}).then((res)=>{ 
                this.pixma = res; 
              }); 
            }catch (error) { 
              let err = error as BusinessError; 
              console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code); 
            } 
          } else { 
            return; 
          } 
        } else { 
          console.error('byteBuffer is null'); 
          return; 
        } 
        this.savePicture(buffer, imageObj); 
      }); 
    }); 
  } 
  async onImageArrival(receiver: image.ImageReceiver): Promise<void> { 
    receiver.on('imageArrival', () => { 
      console.error("imageArrival callback"); 
      receiver.readLatestImage((err, nextImage: image.Image) => { 
        if (err || nextImage === undefined) { 
          return; 
        } 
        nextImage.getComponent(image.ComponentType.JPEG, async (err, imgComponent: image.Component) => { 
          if (err || imgComponent === undefined) { 
            return; 
          } 
          this.saveImageToFile(imgComponent.byteBuffer); 
          if (imgComponent.byteBuffer as ArrayBuffer) { 
            let sourceOptions: image.SourceOptions = { 
              sourceDensity: 120, 
              sourcePixelFormat: 8, // NV21 
              sourceSize: { 
                height: this.previewProfilesObj2!.size.height, 
                width: this.previewProfilesObj2!.size.width 
              }, 
            } 
            let imageResource = image.createImageSource(imgComponent.byteBuffer, sourceOptions); 
            let decodingOptions : image.DecodingOptions = { 
              editable: true, 
              desiredPixelFormat: 3, 
 
            } 
            imageResource.createPixelMap(decodingOptions).then((res)=>{ 
              this.imgUrl = res; 
            }); 
          } else { 
            return; 
          } 
          nextImage.release(); 
        }) 
      }) 
    }) 
  } 
  saveImageToFile(data: ArrayBuffer) { 
    const context = getContext(this); 
    let filePath = context.tempDir + "/test.jpg"; 
    console.info("path is " + filePath); 
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
    fs.write(file.fd, data, (err, writeLen) => { 
      if (err) { 
        console.info("write failed with error message: " + err.message + ", error code: " + err.code); 
      } else { 
        console.info("write data to file succeed and size is:" + writeLen); 
        fs.closeSync(file); 
      } 
    }); 
  } 
  build() { 
    Column() { 
      Row() { 
        // 将编辑好的pixelMap传递给状态变量imagePixelMap后,通过Image组件进行渲染 
        Image(this.imgUrl).objectFit(ImageFit.None) 
      }.width('100%').height('50%').backgroundColor('#F0F0F0') 
      Row() { 
        Button() { 
          Text("拍照") 
            .fontColor(Color.Black) 
            .alignSelf(ItemAlign.Center) 
            .onClick(() => { 
              // this.capture(this.photoOutPut, null) 
              let settings: camera.PhotoCaptureSetting = { 
                quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // 设置图片质量高 
                rotation: camera.ImageRotation.ROTATION_0, // 设置图片旋转角度0 
                mirror: false // 设置镜像使能开关(默认关) 
              }; 
              if (this.photoOutput){ 
                this.photoOutput.capture(settings, (err: BusinessError) => { 
                  if (err) { 
                    console.error(`Failed to capture the photo. error: ${JSON.stringify(err)}`); 
                    return; 
                  } 
                  console.info('Callback invoked to indicate the photo capture request success.'); 
                }); 
              } 
 
            }) 
        } 
        .width(100) 
        .height(100) 
        Image(this.pixma) 
          .width(200) 
          .height(200) 
      } 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
分享
微博
QQ
微信
回复
2024-11-13 17:02:58


相关问题
HarmonyOS 摄像头预览画面方向错误
1189浏览 • 1回复 待解决
相机预览及切换摄像头
1918浏览 • 1回复 待解决
HarmonyOS 摄像头录制时,回调数据
848浏览 • 1回复 待解决
如何获取前置摄像头预览图像
3423浏览 • 1回复 待解决
HarmonyOS 获取摄像头能力
921浏览 • 1回复 待解决
HarmonyOS 摄像头切换时卡死
810浏览 • 1回复 待解决
HarmonyOS 前置摄像头开启
1003浏览 • 1回复 待解决
HarmonyOS 打开摄像头失败
856浏览 • 1回复 待解决
HarmonyOS 摄像头录制问题
1010浏览 • 1回复 待解决
HarmonyOS 录制屏幕 录制摄像头咨询
1226浏览 • 1回复 待解决
请问3.1如何调用摄像头
3259浏览 • 1回复 待解决
HarmonyOS 切换前置摄像头黑屏
755浏览 • 1回复 待解决