HarmonyOS 自定义相机功能

想要实现相机录入功能,自定义相机样式,有对应的文档,示例或者demo吗?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

官网文档中提供了相机开发的最佳实践,可参考该内容进行自定义相机的开发,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/camera-shooting-case-V5

demo代码如下:

import { abilityAccessCtrl, PermissionRequestResult, Permissions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import common from '@ohos.app.ability.common';
import camera from '@ohos.multimedia.camera';
@Entry
@Component
export struct CameraPage {
  @StorageProp('statusBarHeight') statusBarHeight: number = 0;
  private xcomponentController: XComponentController = new XComponentController;
  private surfaceId: string = '';
  requestCameraPermission(): void {
    let permission: Permissions[] = ['ohos.permission.CAMERA'];
    let context: Context = getContext(this) as Context;
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    atManager.requestPermissionsFromUser(context, permission).then((data: PermissionRequestResult) => {
      let grantStatus: Array<number> = data.authResults;
      let length: number = grantStatus.length;
      for (let i = 0; i < length; i++) {
        if (grantStatus[i] != 0) {
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
          return;
        }
      }
      // 授权成功
    }).catch((err: BusinessError) => {
      console.error(`${err.code}:${err.message}`)
    })
  }
  async aboutToAppear(): Promise<void> {
    this.requestCameraPermission()
  }
  build() {
    Column() {
      Row() {
        XComponent({
          id: 'xcomponent',
          type: XComponentType.SURFACE,
          controller: this.xcomponentController
        })
          .onLoad(() => {
            this.xcomponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight: 1080 });
            this.surfaceId = this.xcomponentController.getXComponentSurfaceId()
          })
          .width('720px')
          .height('480px')
      }
      .backgroundColor(Color.Black)
      Button("开始预览")
        .onClick(() => {
          previewCamera(getContext(this), this.surfaceId);
        })
    }
  }
}
export async function previewCamera(baseContext: common.BaseContext, surfaceId: string): Promise<void> {
  //获取CameraManager
  let cameraManager: camera.CameraManager = camera.getCameraManager(baseContext);
  //获取相机列表
  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  //创建相机输入流
  let cameraInput: camera.CameraInput | undefined = undefined;
  try{
    cameraInput = cameraManager.createCameraInput(cameraArray[0]);
    cameraInput.on('error', cameraArray[0], (error: BusinessError) =>{
      console.error(`Camera input error code: ${error.code}`);
    });
    await cameraInput.open();
  } catch (e) {
    let error:BusinessError = e as BusinessError;
    console.error(`${error.code}:${error.message}`)
  }
  //获取相机设备支持的输出流能力
  let cameraOutputCap: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[0]);
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
  if (!previewProfilesArray) {
    console.error("cameraManager.getSupportedOutputCapability error");
    return undefined;
  }
  //创建预览输出流
  let previewOutput: camera.PreviewOutput | undefined = undefined;
  previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
  //创建会话
  let captureSession: camera.PhotoSession | undefined = undefined;
  captureSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
  try {
    captureSession.beginConfig();
    captureSession.addInput(cameraInput);
    captureSession.addOutput(previewOutput);
    await captureSession.commitConfig();
    await captureSession.start();
  } catch (e) {
    let error: BusinessError = e as BusinessError;
    console.error(`${error.code}:${error.message}`)
  }
}

还需要在module.json5文件中声明如下权限:

{
  "name": "ohos.permission.CAMERA",
"usedScene": {
  "abilities": [
  "EntryAbility"
  ],
  "when": "inuse"
},
  "reason": "$string:reason"
},{
  "name": "ohos.permission.READ_IMAGEVIDEO",
  "usedScene": {
    "abilities": [
    "EntryAbility"
    ],
    "when": "inuse"
  },
  "reason": "$string:reason"
},{
  "name": "ohos.permission.WRITE_IMAGEVIDEO",
  "usedScene": {
    "abilities": [
    "EntryAbility"
    ],
    "when": "inuse"
  },
  "reason": "$string:reason"
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 关于自定义相机功能
55浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
358浏览 • 1回复 待解决
HarmonyOS 如何自定义相机
36浏览 • 1回复 待解决
HarmonyOS 自定义相机预览问题
71浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
249浏览 • 1回复 待解决
HarmonyOS 如何自定义相机背景
34浏览 • 1回复 待解决
HarmonyOS 自定义拍照功能
52浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照不成功
43浏览 • 1回复 待解决
HarmonyOS 使用自定义相机左边有间距
45浏览 • 1回复 待解决
能够提供HarmonyOS自定义相机案例吗?
351浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
826浏览 • 1回复 待解决
HarmonyOS 地图自定义气泡功能
91浏览 • 1回复 待解决
HarmonyOS 自定义相机前置摄像头变形
90浏览 • 1回复 待解决
HarmonyOS 自定义扫一扫功能
45浏览 • 1回复 待解决
【求助】自定义相机Camera2焦距异常
8019浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1181浏览 • 1回复 待解决