AndroidCamera2拍照(三)——切換攝像頭,延時拍攝和閃光模式
原文:Android Camera2 拍照(三)——切換攝像頭,延時拍攝和閃光模式
一、切換攝像頭
在前後攝像頭之間切換,首先需要關閉之前開啟的攝像頭,關閉preview,之後重新開啟新的攝像頭,重新開啟preview。
public void switchCamera() {
if (mCameraId.equals(CAMERA_FRONT)) {
mCameraId = CAMERA_BACK;
closeCamera();
reopenCamera();
} else if (mCameraId.equals(CAMERA_BACK)) {
mCameraId = CAMERA_FRONT;
closeCamera();
reopenCamera();
}
}
關閉攝像頭:
/**
* Closes the current {@link CameraDevice}.
*/
private void closeCamera() {
try {
mCameraOpenCloseLock.acquire();
synchronized (mCameraStateLock) {
// Reset state and clean up resources used by the camera.
// Note: After calling this, the ImageReaders will be closed after any background
// tasks saving Images from these readers have been completed.
mPendingUserCaptures = 0;
mState = STATE_CLOSED;
if (null != mCaptureSession) {
mCaptureSession.close();
mCaptureSession = null;
}
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
if (null != mJpegImageReader) {
mJpegImageReader.close();
mJpegImageReader = null;
}
if (null != mRawImageReader) {
mRawImageReader.close();
mRawImageReader = null;
}
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
mCameraOpenCloseLock.release();
}
}
開啟新的攝像頭:
public void reopenCamera() {
if (mTextureView.isAvailable()) {
openCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
如果TextureView是available的,那麼直接呼叫openCamera()開啟camera(openCamera()的程式碼參考之前的部落格);如果|TextureView是unavailable的,那麼呼叫TextureView的setSurfaceTextureListener設定SurfaceTextureListener(參考上一篇部落格)。
二、延時拍攝
要實現延時拍攝,核心是實現一個定時器。本例延時分為三種模式:3s延時,10s按時,無延時。可以在三種模式之中自有切換。
private void switchDelayState() {
switch (mDelayState) {
case 0:
mTimer.setImageResource(R.mipmap.ic_3s);
mDelayTime = 3 * 1000;
mDelayState = 1;
break;
case 1:
mTimer.setImageResource(R.mipmap.ic_10s);
mDelayTime = 10 * 1000;
mDelayState = 2;
break;
case 2:
mTimer.setImageResource(R.mipmap.timer);
mDelayTime = 0;
mDelayState = 0;
break;
default:
break;
}
}
變數mDelayTime和mDelaySate的定義:
/*
* Delay state, 0 represents no delay, 1 represents 3s delay, while 2 represents 10s delay
*/
private short mDelayState = 0; // Timer
private short mDelayTime;
當拍攝按鈕被點選時:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.capture: {
if (mDelayState == 0) {
takePicture();
} else {
new CountDownTimer(mDelayTime, TIME_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
mTimeText.setVisibility(View.VISIBLE);
mTimeText.setText("" + millisUntilFinished / TIME_INTERVAL);
}
@Override
public void onFinish() {
mTimeText.setVisibility(View.GONE);
takePicture();
}
}.start();
}
break;
}
但有3s或10s延時的時候,利用CountDownTimer實現倒數計時並每隔一秒更新剩餘時間提示,到時候執行拍照功能。
三、切換閃光模式
支援自動,強制,禁止閃光。與閃光模式有關的屬性是CaptureRequest.CONTROL_AE_MODE和CaptureRequest.FLASH_MODE。 如果要使用FLASH_MODE屬相,閃光燈必須可用,並且,CONTROL_AE_MODE必須設定為on或off,否則,自動曝光屬性的ON_AUTO_FLASH,ON_ALWAYS_FLASH,
ON_AUTO_FLASH_REDEYE將會覆蓋FLASH_MODE的設定。
private void switchFlashMode() {
switch (mFlashMode) {
case 0:
mFlashMode = 1;
mFlashBtn.setImageResource(R.mipmap.flash_auto);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
try {
mCaptureSession.setRepeatingRequest(
mPreviewRequestBuilder.build(),
mPreCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
return;
}
break;
case 1:
mFlashMode = 2;
mFlashBtn.setImageResource(R.mipmap.flash_on);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
try {
mCaptureSession.setRepeatingRequest(
mPreviewRequestBuilder.build(),
mPreCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
return;
}
break;
case 2:
mFlashMode = 0;
mFlashBtn.setImageResource(R.mipmap.flash_off);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
try {
mCaptureSession.setRepeatingRequest(
mPreviewRequestBuilder.build(),
mPreCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
return;
}
break;
}
}
在takePicture()之前,需要再次設定:
private void setFlashMode() {
switch (mFlashMode) {
case 0:
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
break;
case 1:
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
break;
case 2:
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
break;
}
}
程式碼參考:https://github.com/gengqifu/361Camera,歡迎fork/star。
相關文章
- Android呼叫攝像頭拍照Android
- Android提供的攝像頭拍照Android
- 安卓呼叫攝像頭拍照安卓
- iphone 4 攝像頭是前後切換iPhone
- UVC攝像頭按鍵拍照功能
- 【Android】【opencv】實現攝像頭拍照和錄影AndroidOpenCV
- 照片系列之android呼叫攝像頭拍照Android
- Unity 3d UI獲取外部攝像頭拍攝Unity3DUI
- HTML5拍照、攝像機功能實戰HTML
- Android呼叫攝像頭拍照並顯示照片Android
- 瀏覽器呼叫攝像頭進行拍照程式瀏覽器
- 華為nova 4拍照怎麼樣?華為Nova4手機攝像頭拍照測試
- web呼叫攝像頭拍照並上傳到伺服器Web伺服器
- 在Android中呼叫攝像頭拍照並顯示出來Android
- 【音影片】攝像頭
- 攝像頭操作指南
- ToDesk勾上攝像頭會看到我嗎?如何關閉攝像頭
- iPhone XS/iPhone XS Max攝像頭拍照解析:蘋果也玩起了AI拍照iPhone蘋果AI
- python版opencv:如何用筆記本攝像頭拍照儲存PythonOpenCV筆記
- .NET 攝像頭採集
- android opencv 前置攝像頭AndroidOpenCV
- android 開啟攝像頭Android
- Win10攝像頭如何開啟_WIN10攝像頭在哪裡Win10
- 搭建一個攝像頭應用程式 應用程式內部攝像頭
- 純JavaScript實現的呼叫裝置攝像頭並拍照的功能JavaScript
- android studio呼叫攝像頭拍照及具體步驟演示程式碼Android
- 一加7渲染圖曝光:後置三攝和彈出式前置攝像頭設計
- Metal入門教程(三)攝像頭採集渲染
- 大華攝像頭 ZLMediaKit JavaJava
- WebRTC開啟本地攝像頭Web
- 如何使用PYTHON操作攝像頭Python
- 某CCTV攝像頭漏洞分析
- 人工智慧"眼睛"——攝像頭人工智慧
- jQuery webcam plugin呼叫攝像頭jQueryWebPlugin
- 蘋果iPhone XI新功能爆料:將配備"水下模式"+三攝像頭蘋果iPhone模式
- HSmartWindowControl 之 攝像頭實時顯示( 使用 WPF )
- 筆記本攝像頭怎麼開啟 筆記本設定攝像頭教程筆記
- 專訪模組廠商:人臉識別中單攝像頭和雙攝像頭該如何選型