一、切换摄像头
在前后摄像头之间切换,首先需要关闭之前打开的摄像头,关闭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) {