鸿蒙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的相机录像功能。

相关推荐
奶糖不太甜1 小时前
鸿蒙开发组件问题:方法论与技术探索
harmonyos
鸿蒙小灰1 小时前
鸿蒙开发问题之网络请求库适配
网络协议·harmonyos
HarmonyOS_SDK5 小时前
汽车之家联合HarmonyOS SDK,深度构建鸿蒙生态体系
harmonyos
whysqwhw5 小时前
鸿蒙沉浸式
harmonyos
whysqwhw6 小时前
鸿蒙Stack使用
harmonyos
whysqwhw6 小时前
鸿蒙Flex使用
harmonyos
whysqwhw7 小时前
鸿蒙Row/Column使用
harmonyos
一只栖枝14 小时前
华为 HCIE 大数据认证中 Linux 命令行的运用及价值
大数据·linux·运维·华为·华为认证·hcie·it
zhanshuo18 小时前
在鸿蒙里优雅地处理网络错误:从 Demo 到实战案例
harmonyos