Android自定义录像(一)之录像功能

本文介绍了Android项目中实现自定义录像的思路和关键代码,包括MediaRecorder、SurfaceView和Camera类的使用,以及自动对焦功能的实现。提供布局、权限配置和注意事项,为Android开发者提供参考。
摘要由CSDN通过智能技术生成

统一回复:该博客中所给出的demo代码已经不再维护。大致实现逻辑可以参考,Android版本等兼容性问题请自行寻找方案解决。


引言

最近在做一个项目,是有关用手机摄像头做图像实时识别的。所以里面需要自定义一个录像功能。该demo实现了录像录像后文件的保存查看,录像会实时自动对焦(AutoFocus)。根据功能分两篇讲述。这第一篇讲述录像基本实现思路和需要注意的点。


思路

*Android实现录像主要依靠MediaRecorder和SurfaceView这两个类。另外,因为需要对摄像头参数做一些设定,所以也需要Camera类。它们的作用分别是:MediaRecorder通过控制录像音视频源和输出编码等;surfaceview则是作为View的存在提供用户界面,在surfaceview的不同生命周期实现不同的操作;camera类则用于对摄像头参数做一些设定,再调用MediaRecorder的setCamera() 方法将camera对象带入。
##代码
####1.布局

<?xml version="1.0" encoding="utf-8"?>
	<FrameLayout android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:baselineAligned="false"
	    xmlns:android="http://schemas.android.com/apk/res/android">
	    <SurfaceView
	        android:id="@+id/capture_surfaceview"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent" />
	    <LinearLayout
	        android:orientation="horizontal"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:baselineAligned="false">
	        <RelativeLayout
	            android:layout_gravity="left"
	            android:layout_width="0.0dip"
	            android:layout_height="fill_parent"
	            android:layout_weight="5.0">
	            <TextView
	                android:textSize="15.0sp"
	                android:textColor="@color/red_overlay"
	                android:id="@+id/capture_textview_information"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:layout_margin="10.0dip"
	                android:layout_alignParentRight="true"
	                android:layout_alignParentBottom="true" />
	        </RelativeLayout>
	        <RelativeLayout
	            android:layout_gravity="right"
	            android:background="@color/white_overlay"
	            android:padding="20.0dip"
	            android:layout_width="0.0dip"
	            android:layout_height="fill_parent"
	            android:layout_weight="1.0"
	            android:alpha="0.3">
	
	            <ImageButton
	                android:id="@+id/capture_imagebutton_setting"
	                android:tag="setting"
	                android:background="@drawable/settings"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:layout_alignParentTop="true"
	                android:layout_centerHorizontal="true"
	                android:contentDescription="@string/imagedescription" />
	            <ImageButton
	                android:id="
  • 10
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
Android中,我们可以使用MediaRecorder类来进行自定义录像。以下是实现自定义录像的步骤: 1. 首先,我们需要在AndroidManifest.xml文件中添加以下权限: <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2. 创建一个Camera对象,并设置预览界面: Camera mCamera = Camera.open(); mCamera.setPreviewDisplay(holder); // 这里的holder是SurfaceView的SurfaceHolder对象 3. 创建一个MediaRecorder对象: MediaRecorder mMediaRecorder = new MediaRecorder(); 4. 设置MediaRecorder的参数: mMediaRecorder.setCamera(mCamera); // 设置使用的摄像头 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置音频源为麦克风 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 设置视频源为摄像头 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); // 设置输出格式为MP4 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 设置音频编码器 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); // 设置视频编码器 mMediaRecorder.setOutputFile(outputFile); // 设置输出文件路径 5. 准备MediaRecorder: mMediaRecorder.prepare(); 6. 开始录制: mMediaRecorder.start(); 7. 停止录制: mMediaRecorder.stop(); 8. 释放资源: mMediaRecorder.release(); mCamera.release(); 以上就是自定义录像的基本步骤。需要注意的是,在Android 6.0或更高版本中,还需要动态申请权限。另外,还可以根据需求设置更多的参数,如视频分辨率、帧率等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值