前两个月项目要求不能调用系统的相机,那就只能用自定义的了,查了一些资料,自己再研究了一下,自定义的相机还是有点复杂的,布局和代码中都要用到一个重要的SurfaceView。
一、建立布局,布局的背景框可以让美工给出,这里姑且就是一个蓝色的边框,然后下面有三个按钮,我里布局文件activity_custom_camera.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<FrameLayout
android:id="@+id/layout_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/view_text_top"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="请将证件对准方框"
android:textSize="18dp"
android:visibility="visible"
android:textColor="@color/blue"
android:background="#000000"
android:alpha="0.5"/>
<LinearLayout
android:id="@+id/view_main_content"
android:layout_marginTop="50dp"
android:layout_above="@+id/layout_button_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:id="@+id/view_left"
android:layout_width="30dp"
android:layout_height="match_parent"
android:background="#000000"
android:alpha="0.5"
android:visibility="visible"/>
<!--中心布局,取景处-->
<View
android:id="@+id/bg_center_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="@drawable/qr_code_bg_take_photo"/>
<TextView
android:id="@+id/view_right"
android:textColor="@color/blue"
android:gravity="center"
android:layout_width="30dp"
android:layou