HarmonyOS鸿蒙Next中实现拍照、录音功能示例代码
HarmonyOS鸿蒙Next中实现拍照、录音功能示例代码 介绍 本示例演示如何使用Web组件拍照和录音。
- web拉起系统相机拍照,并返回base64数据给web页面。
- web调用麦克风录音,并保持录音文件到指定沙箱目录。
效果预览

使用说明
-
web调用相机拍照:
- 打开应用后,点击“拍照按钮”
- 在拉起的系统相机中进行拍照
- 拍照完成后web网页会获取到base64数据
-
web调用麦克风录音:
- 打开应用后,点击“开始录音”
- 一段时间后,点击“结束录音”
- 录音完成后录音文件保存在沙箱目录下,文件名为Audio_录音时间.mp4
实现思路
web调用相机拍照
点击拍照按钮后,应用通过@ohos.app.ability.Want接口拉起系统相机进行拍照,并获取图片uri值。通过图片uri值转为ArrayBuff,使用@ohos.file.fs接口读取文件数据,通过Base64Helper接口将文件转化为base64数据返回给web网页。核心代码如下,源码参考 WebPage.ets
// 核心代码
web调用麦克风录音
使用@ohos.multimedia.media接口的AVRecorder方法进行音频录制,调用该方法前,需要先通过createAVRecorder()构建一个AVRecorder实例。使用AVRecorderConfig方法进行音频录制的参数设置,此处为纯音频录制,因此仅需要设置audioSourceType。核心代码如下,源码参考 AudioRecorder.ets
// 核心代码
更多关于HarmonyOS鸿蒙Next中实现拍照、录音功能示例代码的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,实现拍照和录音功能可以通过调用系统提供的API来完成。以下是示例代码:
拍照功能:
import camera from '@ohos.multimedia.camera';
import image from '@ohos.multimedia.image';
async function takePhoto() {
try {
const cameraManager = camera.getCameraManager();
const cameras = cameraManager.getSupportedCameras();
const cameraInput = cameraManager.createCameraInput(cameras[0]);
await cameraInput.open();
const captureSession = cameraManager.createCaptureSession();
await captureSession.beginConfig();
await captureSession.addInput(cameraInput);
const photoOutput = cameraManager.createPhotoOutput();
await captureSession.addOutput(photoOutput);
await captureSession.commitConfig();
await captureSession.start();
const photo = await photoOutput.capture();
const imageSource = image.createImageSource(photo);
const imagePacker = image.createImagePacker();
const arrayBuffer = await imagePacker.packing(imageSource, { format: 'image/jpeg' });
// 保存或处理arrayBuffer
} catch (error) {
console.error('拍照失败:', error);
}
}
录音功能:
import audio from '@ohos.multimedia.audio';
async function startRecording() {
try {
const audioManager = audio.getAudioManager();
const audioCapturer = audioManager.createAudioCapturer({
audioFormat: audio.AudioFormat.AUDIO_FORMAT_PCM_16BIT,
sampleRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
channels: audio.AudioChannel.CHANNEL_1,
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
});
await audioCapturer.start();
const bufferSize = 1024;
const buffer = new ArrayBuffer(bufferSize);
const audioData = await audioCapturer.read(buffer, bufferSize);
// 处理或保存audioData
} catch (error) {
console.error('录音失败:', error);
}
}
以上代码展示了如何在HarmonyOS鸿蒙Next中使用系统API实现拍照和录音功能。
更多关于HarmonyOS鸿蒙Next中实现拍照、录音功能示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,实现拍照和录音功能可以通过调用系统提供的API来完成。以下是示例代码:
- 拍照功能:
// 创建拍照意图
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置照片存储路径
Uri photoUri = FileProvider.getUriForFile(context, "com.example.fileprovider", new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "photo.jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
// 启动拍照
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
- 录音功能:
// 创建录音意图
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
// 启动录音
startActivityForResult(intent, REQUEST_AUDIO_CAPTURE);
在onActivityResult方法中处理返回的拍照或录音结果。确保在AndroidManifest.xml中声明必要的权限和FileProvider配置。

