參考資料:
Camera2官方API
Camera 官方API
Camera API 指南
Android.Camera2相機超詳細講解
Android實戰技巧之三十三:android.hardware.camera2使用指南
需求
現在的手機拍照的質量越來越好了,照片也越來越大。這樣給開發就帶來了一個苦惱,就是需要上傳的圖片文件太大,而且下載也太耗流量了。所以我們需要控制一下拍攝的照片的大小,而且還有一點就是一些APP或應用場景對圖片的寬高比和大小都是有要求的,比如1:1、2:1、大小不超過20kb、1M這樣的要求,但是讓用戶使用自己手機拍照後,再去修圖實在太麻煩了(不是每個人都會安裝美圖秀秀之類的修圖軟件的O(∩_∩)O~~~,而且增加了操作步驟還是跨應用的,不夠優雅),所以我們現在就可以學習怎麼在自己的應用中打開自己的自定義相機,然後在拍照的時候配置我們需要的圖片的大小和尺寸。
如果需要使用Android系統自帶的相機拍攝照片或者記錄視頻,可以查看這裏Taking Photos Simply 和 Recording Videos Simply
官方API
我們使用android.hardware.camera2
(Android5.0,API21及以上)來實現自定義相機的功能,不使用Camera
(API21以下,不包括API21)。
實現
谷歌demo參考 —— Android Camera2 Basic Sample
1、先設置權限
請求相機權限
<uses-permission android:name="android.permission.CAMERA" />
如果你的應用用到了相機,又不想讓GooglePlay
阻止你安裝到哪些沒有相機的Android應用上,你就需要加上下面這句
<uses-feature android:name="android.hardware.camera" android:required="false" />
關於uses-feature
的使用可以看官方介紹 ,或者自行百度“AndroidManifest.xml清單中uses-feature的使用”
如果你要保存圖片到SD卡,請加上下面的權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果要用自定義的相機去捕獲視頻的話,還需要下面的權限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
如果你想在你拍攝的照片上加上位置信息,你還需要加上下面的權限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
2、Camera2的使用
camera2
的使用可以參考camera2
的API
camera2這個包通過攝像頭設備不間斷的捕獲圖像,我們可以通過SurfaceView或TextureView(通過其SurfaceTexture)來預覽這些圖像,然後通過發送(拍照)請求去捕獲單個的幀生成圖片,通過發送攝影(開始、結束)請求去捕獲一定時間段內的幀(圖像)生成視頻。
參考博客中有camera2
執行拍照的示意流程圖,如下:
這些圖只是爲了方便自己理解,各位碼友如果要看這些圖的介紹可以去這兩篇blog
Android.Camera2相機超詳細講解
Android實戰技巧之三十三:android.hardware.camera2使用指南
3、寫代碼
3-1、自定義TextureView
定義一個自適應的TextureView
(從官方demo中copy的),在佈局文件中使用
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
*
* @param width Relative horizontal size
* @param height Relative vertical size
*/
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
}
3-2、定義相機界面
定義一個ZCameraActivity
作爲相機界面,佈局文件如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cnbs.camera2demo.ZCameraActivity">
<com.cnbs.camera2demo.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@+id/control_top"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:background="@color/colorGray">
<ImageView
android:id="@+id/camera_flash"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="20dp"
android:layout_gravity="start|center_vertical"
android:src="@mipmap/camera_flash_off" />
<TextView
android:id="@+id/quality_picture"
android:layout_width="40dp"
android:layout_height="20dp"
android:drawableEnd="@mipmap/arrow_down"
android:layout_gravity="center"
android:gravity="center"
android:text="100 "
android:textSize="12dp"
android:textColor="@color/colorWhite"
android:src="@mipmap/camera_flash_off" />
<TextView
android:id="@+id/rate_picture"
android:layout_width="40dp"
android:layout_height="20dp"
android:drawableEnd="@mipmap/arrow_down"
android:layout_gravity="end|center_vertical"
android:layout_marginRight="20dp"
android:gravity="center"
android:text="1:1 "
android:textSize="12dp"
android:textColor="@color/colorWhite"
android:src="@mipmap/camera_flash_off" />
</FrameLayout>
<FrameLayout
android:id="@+id/control_bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@color/colorGray">
<ImageView
android:id="@+id/pictures"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_gravity="start|center_vertical"
android:src="@mipmap/picture_default" />
<ImageView
android:id="@+id/take_picture"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@mipmap/picasa_off" />
<ImageView
android:id="@+id/b_f_camera"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:layout_gravity="end|center_vertical"
android:src="@mipmap/B_amp_F_camera" />
</FrameLayout>
</RelativeLayout>
UI效果如圖:
3-3、打開相機
補充
文中涉及到一些可能不常用的類、方法或者資源,都在這裏補充說明一下。
1、HandlerThread
我們對Handler 和 Thread都比較熟悉了,一般都是用來處理子線程和主線程之間的通信問題;但是HandlerThread我們用到的比較少, 可能不太熟悉,其實 HandlerThread 是谷歌官方提供的用來處理子線程與子線程之間的通信問題的類。特點是:
- HandlerThread本質上是一個線程類,它繼承了Thread;
- HandlerThread有自己的內部Looper對象,可以進行looper循環;
- 通過獲取HandlerThread的looper對象傳遞給Handler對象,可以在handleMessage方法中執行異步任務。
- 創建HandlerThread後必須先調用HandlerThread.start()方法,Thread會先調用run方法,創建Looper對象。
參考文章:
Thread、Handler和HandlerThread關係何在?
Android 多線程之HandlerThread 完全詳解
HandlerThread谷歌官方API
2、佈局使用的圖標
文章中佈局使用的圖標都是在這個圖標庫網站下載的 https://www.easyicon.net/