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

相关推荐
一只大侠的侠3 小时前
Flutter开源鸿蒙跨平台训练营 Day 10特惠推荐数据的获取与渲染
flutter·开源·harmonyos
御承扬9 小时前
鸿蒙NDK UI之文本自定义样式
ui·华为·harmonyos·鸿蒙ndk ui
前端不太难9 小时前
HarmonyOS 游戏上线前必做的 7 类极端场景测试
游戏·状态模式·harmonyos
大雷神9 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第29篇:数据管理与备份
华为·harmonyos
讯方洋哥10 小时前
HarmonyOS App开发——关系型数据库应用App开发
数据库·harmonyos
巴德鸟11 小时前
华为手机鸿蒙4回退到鸿蒙3到鸿蒙2再回退到EMUI11 最后关闭系统更新
华为·智能手机·harmonyos·降级·升级·回退·emui
一起养小猫11 小时前
Flutter for OpenHarmony 实战_魔方应用UI设计与交互优化
flutter·ui·交互·harmonyos
一只大侠的侠11 小时前
Flutter开源鸿蒙跨平台训练营 Day7Flutter+ArkTS双方案实现轮播图+搜索框+导航组件
flutter·开源·harmonyos
那就回到过去11 小时前
VRRP协议
网络·华为·智能路由器·ensp·vrrp协议·网络hcip
相思难忘成疾12 小时前
通向HCIP之路:第四步:边界网关路由协议—BGP(概念、配置、特点、常见问题及其解决方案)
网络·华为·hcip