标签:override note 变量 覆盖 摄像头 http mod wim public
原文: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);
}
}
要实现延时拍摄,核心是实现一个定时器。本例延时分为三种模式: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;
}
}
/*
* 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;
}
支持自动,强制,禁止闪光。与闪光模式有关的属性是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;
}
}
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 Camera2 拍照(三)——切换摄像头,延时拍摄和闪光模式
标签:override note 变量 覆盖 摄像头 http mod wim public
原文地址:https://www.cnblogs.com/lonelyxmas/p/9175776.html