鸿蒙next版开发:相机开发-录像(ArkTS)

在HarmonyOS 5.0中,ArkTS提供了一套完整的API来管理相机功能,特别是录像功能。本文将详细介绍如何在ArkTS中实现录像功能,并提供代码示例进行详细解读。

录像功能开发步骤

1. 导入相关接口

首先,需要导入相机相关的接口,以便使用相机服务。

复制代码
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';

2. 创建Surface

XComponent组件为预览流提供的Surface(获取surfaceId请参考 getXcomponentSurfaceId 方法),而XComponent的能力由UI提供。

3. 获取相机输出能力

通过CameraOutputCapability类获取当前设备支持的预览能力,并创建预览输出流。

复制代码
function getPreviewOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability, surfaceId: string): camera.PreviewOutput | undefined {
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles;
  let previewOutput: camera.PreviewOutput | undefined = undefined;
  try {
    previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
  } catch (error) {
    let err = error as BusinessError;
    console.error("Failed to create the PreviewOutput instance. error code: " + err.code);
  }
  return previewOutput;
}

4. 创建会话并开始录像

创建相机会话,配置输入流和输出流,然后开始录像。

复制代码
async function startRecordingOutput(cameraManager: camera.CameraManager, previewOutput: camera.PreviewOutput, surfaceId: string): Promise<void> {
  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  if (cameraArray.length == 0) {
    console.error('no camera.');
    return;
  }
  let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraArray[0]);
  let isSupportVideoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_VIDEO) >= 0;
  if (!isSupportVideoMode) {
    console.error('video mode not support');
    return;
  }
  let cameraInput: camera.CameraInput | undefined = undefined;
  cameraInput = cameraManager.createCameraInput(cameraArray[0]);
  if (cameraInput === undefined) {
    console.error('cameraInput is undefined');
    return;
  }
  await cameraInput.open();
  let recordingSession: camera.VideoSession = cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession;
  recordingSession.beginConfig();
  recordingSession.addInput(cameraInput);
  recordingSession.addOutput(previewOutput);
  // 创建录像输出流
  let recordingOutput: camera.VideoOutput = cameraManager.createVideoOutput(surfaceId);
  recordingSession.addOutput(recordingOutput);
  await recordingSession.commitConfig();
  await recordingSession.start();
}

5. 监听录像输出流状态

在相机应用开发过程中,可以随时监听录像输出流状态,包括录像流启动、录像流结束、录像流输出错误。

复制代码
function onRecordingOutputFrameStart(recordingOutput: camera.VideoOutput): void {
  recordingOutput.on('frameStart', (err: BusinessError) => {
    if (err !== undefined && err.code !== 0) {
      return;
    }
    console.info('Recording frame started');
  });
}

function onRecordingOutputFrameEnd(recordingOutput: camera.VideoOutput): void {
  recordingOutput.on('frameEnd', (err: BusinessError) => {
    if (err !== undefined && err.code !== 0) {
      return;
    }
    console.info('Recording frame ended');
  });
}

结语

通过本文的介绍,你应该对如何在HarmonyOS 5.0中使用ArkTS实现录像功能有了基本的了解。录像功能是相机应用的核心,合理利用这些API可以使你的应用更加专业和高效。希望本文能够帮助你在开发过程中更好地利用ArkTS的相机录像功能。

相关推荐
程序猿追7 小时前
HarmonyOS 7 ArkUI 秒表分圈:单圈用时与累计用时应该怎样核对
华为·harmonyos
默 语7 小时前
HarmonyOS7 悬停事件实战:onHover、hoverEffect 四态与设备适配(附完整案例)
harmonyos
程序猿追7 小时前
HarmonyOS 7 多标签草稿,关掉当前页之后该留下谁
pytorch·深度学习·harmonyos
大凡光学9 小时前
平面标定板vs三维立体标定板:双目/多目系统的选型分水岭
数码相机·机器视觉·双目系统·多目系统·三维数据采集
默 语9 小时前
HarmonyOS7基础属性全解(六):浮层 overlay——角标水印挂上去,容器层级一点不加
harmonyos
默 语12 小时前
HarmonyOS7基础属性全解-组件标识id
harmonyos
默 语12 小时前
HarmonyOS7基础属性全解(八):隐私遮罩 obscured——屏幕上看得清清楚楚,录屏里只剩占位图
wpf·harmonyos
默 语12 小时前
HarmonyOS7基础属性全解(五):背景设置——颜色垫底、图片其上、模糊封顶
harmonyos
默 语12 小时前
HarmonyOS7基础属性全解(七):Z序控制 zIndex——谁浮在上面,代码说了算
harmonyos
ovenslove15 小时前
权限申请:为什么你的相机打不开
harmonyos·arkts