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
- Android提供的攝像頭拍照Android
- Unity 3d UI獲取外部攝像頭拍攝Unity3DUI
- C#實現攝像頭呼叫、拍照C#
- 【Android】【opencv】實現攝像頭拍照和錄影AndroidOpenCV
- HTML5拍照、攝像機功能實戰HTML
- 照片系列之android呼叫攝像頭拍照Android
- Android開發之呼叫攝像頭拍照Android
- html5呼叫攝像頭實現拍照HTML
- 瀏覽器呼叫攝像頭進行拍照程式瀏覽器
- Android呼叫攝像頭拍照並顯示照片Android
- Android 呼叫攝像頭功能【拍照與視訊】Android
- AngularJS中使用HTML5攝像頭拍照AngularJSHTML
- 想用手機拍攝虛擬現實影片? 看看Teleport智慧攝像頭
- C# 使用AForge呼叫筆記本攝像頭拍照C#筆記
- 攝像頭操作指南
- Android切換前後置攝像頭並錄製視訊Android
- iPhone XS/iPhone XS Max攝像頭拍照解析:蘋果也玩起了AI拍照iPhone蘋果AI
- ToDesk勾上攝像頭會看到我嗎?如何關閉攝像頭
- web呼叫攝像頭拍照並上傳到伺服器Web伺服器
- Android呼叫攝像頭Android
- .NET 攝像頭採集
- 在Android中呼叫攝像頭拍照並顯示出來Android
- Android多媒體應用開發-控制攝像頭拍照Android
- Win10攝像頭如何開啟_WIN10攝像頭在哪裡Win10
- 搭建一個攝像頭應用程式 應用程式內部攝像頭
- 攝像頭不能用怎麼辦 攝像頭不能用解決辦法
- 蘋果iPhone XI新功能爆料:將配備"水下模式"+三攝像頭蘋果iPhone模式
- 攝像頭黑屏怎麼辦 各種攝像頭不能用的解決方法
- 純JavaScript實現的呼叫裝置攝像頭並拍照的功能JavaScript
- python版opencv:如何用筆記本攝像頭拍照儲存PythonOpenCV筆記
- 如何使用HTML5實現利用攝像頭拍照上傳功能HTML
- Metal入門教程(三)攝像頭採集渲染
- 某CCTV攝像頭漏洞分析
- android opencv 前置攝像頭AndroidOpenCV