前言:简略的介绍android中关于控制摄像头的一些基本操作,实现简易的拍照功能。
权限设置
在AndroidManifest.xml文件中,加入代码:
<uses-permission android:name="android.permission.CAMERA" /> //获取照相机权限(必须)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//获取外部存储权限(可选)
界面设计
界面设计的较为简单,FrameLayout用来显示预览内容,Button用来点击拍照,布局代码如下:
<?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"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<Button
android:id="@+id/button_capture"
android:text="照相"
android:layout_width="wrap_content"
android:layout_he