统一回复:该博客中所给出的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="