写在前面的话:每一个实例的代码都会附上相应的代码片或者图片,保证代码完整展示在博客中。最重要的是保证例程的完整性!!!方便自己也方便他人~欢迎大家交流讨论~
1.新建一个Android项目,取名为startcamera
java文件:startcamera.java主文件
Customcamera.java,ResultActivity.java
layout文件:activity_startcamera.xml,custom.xml,result.xml
2.处理界面布局
2.1activity_startcamera.xml
定义一个放置开始拍照按钮的界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FirstActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="customCamera"
android:text="@string/button_name" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2.2custom.xml
这个界面有拍照按钮和实时预览界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_name1"
android:onClick="capture"/>
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:lay