如何修改playercameramanagerr摄像头卡顿

Android进阶——你所知道的Camera2和你所不知道的Camera2完全解析 - 简书
Android进阶——你所知道的Camera2和你所不知道的Camera2完全解析
一切源于在项目过程中的一个Bug:我的需求是在MainActivity 实现自动预览,也可以点击跳到签到SignedActivity去实现拍照签到,第一次进入界面的时候都是正常的,但是有时候返回来的时候预览失败,即从MainActivity跳转到SignedActivity偶尔预览失败和从SignedActivity返回MainActivity偶尔失败,都是报(CAMERA_IN_USE)ERRO=1的错误,奇怪的是的的确确做了完全释放操作,加上以前用的更多的是Camera api 对于Camer2 的机制没有完整去研究过,一下子懵了,于是乎先去找了Stack Overflow,查到一个解决方案是:"我弃用了新API,换回旧API",ORZ,找了其他的也没有答案,可是我不服呀,我就把官方的文档全部啃了一遍,于是乎便有了以下的理解,我想如果你不懂得怎么使用Camera2的话,这篇绝对值得你去阅读,你会发现Camera2 并非像大多数说得那样使用起来很复杂。
一、Camera2新架构概述
全新的android.hardware.Camera2 。Android 5.0对拍照API进行了全新的设计,新增了全新设计的Camera 2 API,这些API不仅大幅提高了Android系统拍照的功能,还能支持RAW照片输出,甚至允许程序调整相机的对焦模式、曝光模式、快门等。
这里写图片描述
如上图所示,Camera2 API相比原来android.hardware.Camera API 在架构上有了很大的改变,虽然让手机拍照功能更加强大,但同时也增加了开发复杂度了,从以下的Camera2 架构图中所有参与类角色不能看出。
这里写图片描述
引入了管道的概念将安卓设备和摄像头之间联系起来,系统向摄像头发送 Capture 请求,而摄像头会返回 CameraMetadata,这一切建立在一个叫作 CameraCaptureSession 的会话中。
二、Camera2架构主要的类角色说明
在Camera2 架构在核心参与类角色有:CameraManager、CameraDevice、CameraCharacteristics、CameraRequest与CameraRequest.Builder、CameraCaptureSession以及CaptureResult。
1、CameraManager
位于android.hardware.camera2.CameraManager下,也是Android 21(5.0)添加的,和其他系统服务一样通过 Context.getSystemService(CameraManager.class ) 或者Context.getSystemService(Context.CAMERA_SERVICE) 来完成初始化,主要用于管理系统摄像头:
通过getCameraIdList()方法获取Android设备的摄像头列表
getCameraCharacteristics(String cameraId)获取摄像头的详细参数和支持的功能
openCamera(String cameraId, CameraDevice.StateCallback callback, Handler handler)打开指定Id的摄像头**
CameraManager manager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
2、CameraDevice
CameraDevice是Camera2中抽象出来的一个对象,直接与系统硬件摄像头相联系。因为不可能所有的摄像头都会支持高级功能(即摄像头功能可被分为limit 和full 两个级别),当摄像头处于limited 级别时候,此时Camera2和早期的Camera功能差不多,除此之外在Camera2架构中,CameraDevice还承担其他两项重要任务:
通过CameraDevice.StateCallback监听摄像头的状态(主要包括onOpened、onClosed、onDisconnected、onErro四种状态)
管理CameraCaptureSession,-通过方法createCaptureSession(List&Surface& outputs, CameraCaptureSession.StateCallback callback, Handler handler)方法和createReprocessableCaptureSession(InputConfiguration inputConfig, List&Surface& outputs, CameraCaptureSession.StateCallback callback, Handler handler)方法创建会话 (其中第三个参数: The handler on which the callback should be invoked, or null to use the current thread's looper.),通常会在CameraDevice.StateCallback中调用对应方法创建预览会话。
管理CaptureRequest,主要包括通过createCaptureRequest(int templateType)创建捕获请求,在需要预览、拍照、再次预览的时候都需要通过创建请求来完成。
3、CameraCaptureSession
正如前面所说,系统向摄像头发送 Capture 请求,而摄像头会返回 CameraMetadata,这一切都是在由对应的CameraDevice创建的CameraCaptureSession 会话完成,当程序需要预览、拍照、再次预览时,都需要先通过会话。(A configured capture session for a CameraDevice, used for capturing images from the camera or reprocessing images captured from the camera in the same session previously.A CameraCaptureSession is created by providing a set of target output surfaces to createCaptureSession, or by providing an InputConfiguration and a set of target output surfaces to createReprocessableCaptureSession for a reprocessable capture session. Once created, the session is active until a new session is created by the camera device, or the camera device is closed.)CameraCaptureSession一旦被创建,直到对应的CameraDevice关闭才会死掉。虽然CameraCaptureSession会话用于从摄像头中捕获图像,但是只有同一个会话才能再次从同一摄像头中捕获图像。另外,创建会话是一项耗时的异步操作,可能需要几百毫秒,因为它需要配置相机设备的内部管道并分配内存缓冲区以将图像发送到所需的目标,因而createCaptureSession和createReprocessableCaptureSession会将随时可用的CameraCaptureSession发送到提供的监听器的onConfigured回调中。如果无法完成配置,则触发onConfigureFailed回调,并且会话将不会变为活动状态。最后需要注意的是,如果摄像头设备创建了一个新的会话,那么上一个会话是被关闭的,并且会回调与其关联的onClosed,如果不处理好,当会话关闭之后再次调用会话的对应方法那么所有方法将会跑出IllegalStateException异常。关闭的会话清除任何重复的请求(和调用了stopRepeating()方法类似),但是在新创建的会话接管并重新配置摄像机设备之前,关闭的会话仍然会正常完成所有正在进行的捕获请求。简而言之,在Camera2中CameraCaptureSession承担很重要的角色:
管理CameraCaptureSession.StateCallback状态回调,用于接收有关CameraCaptureSession状态的更新的回调对象,主要回调方法有两个当CameraDevice 完成配置,对应的会话开始处理捕获请求时触发onConfigured(CameraCaptureSession session)方法,反之配置失败时候触发onConfigureFailed(CameraCaptureSession session)方法。
管理CameraCaptureSession.CaptureCallback捕获回调,用于接收捕获请求状态的回调,当请求触发捕获已启动时;捕获完成时;在捕获图像时发生错误的情况下;都会触发该回调对应的方法。
通过调用方法capture(CaptureRequest request, CameraCaptureSession.CaptureCallback listener, Handler handler)提交捕获图像请求(Submit a request for an image to be captured by the camera device.)即拍照,其中该请求定义了捕获单个图像的所有参数,包括传感器,镜头,闪光灯和后处理参数,每一次请求的结果将产生一个CaptureResult,可以为一个或多个Surface生成新的帧,然后通过CaptureRequest.Builder的addTarget(Surface)方法附着到对应的Surface上显示,而且这个参数Surface必须是会话创建时候的一个子集,会话一次可以处理多个常规和重新处理请求。但如果只有常规请求或重新处理请求在进行,则以先进先出的顺序处理它们;如果两者都在进行中则分别以各自的先进先出顺序处理他们;然而,处理常规请求和重新处理请求的顺序并不是特定的,换言之,一个常规请求在下一个常规请求提交前被处理,同理重新处理请求也一样,但是一个常规请求不一定是在下一个重新处理请求提交之前被处理。通过capture方法提交的请求处理优先级比通过其他方式( setRepeatingRequest(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler) 或者setRepeatingBurst(List, CameraCaptureSession.CaptureCallback, Handler))提交的请求的处理优先级高,一旦当前的repeat / repeatBurst处理完成,就会被处理。最后一点,所有CaptureSession可用于从相机捕获图像,但只有由createReprocessableCaptureSession创建的会话才可以提交重新处理捕获请求,
将重新处理请求提交到常规捕获会话将导致IllegalArgumentException。
通过调用方法setRepeatingRequest(CaptureRequest request, CameraCaptureSession.CaptureCallback listener, Handler handler)请求不断重复捕获图像,即实现预览
通过方法调用stopRepeating()实现停止捕获图像,即停止预览。
这里写图片描述
4、CameraCharacteristics
描述Cameradevice属性的对象,可以使用CameraManager通过getCameraCharacteristics(String cameraId)进行查询。
5、CameraRequest和CameraRequest.Builder
CameraRequest代表了一次捕获请求,而CameraRequest.Builder用于描述捕获图片的各种参数设置,包含捕获硬件(传感器,镜头,闪存),对焦模式、曝光模式,处理流水线,控制算法和输出缓冲区的配置。,然后传递到对应的会话中进行设置,CameraRequest.Builder则负责生成CameraRequest对象。当程序调用setRepeatingRequest()方法进行预览时,或调用capture()方法进行拍照时,都需要传入CameraRequest参数。CameraRequest可以通过CameraRequest.Builder来进行初始化,通过调用createCaptureRequest来获得。
6、CaptureResult
CaptureRequest描述是从图像传感器捕获单个图像的结果的子集的对象。(CaptureResults are produced by a CameraDevice after processing a CaptureRequest)当CaptureRequest被处理之后由CameraDevice生成。
7、Camera2 主要角色之间的联系
CameraManager处于顶层管理位置负责检测获取所有摄像头及其特性和传入指定的CameraDevice.StateCallback回调打开指定摄像头,CameraDevice是负责管理抽象对象,包括监听Camera 的状态回调CameraDevice.StateCallback、创建CameraCaptureSession和CameraRequest,CameraCaptureSession用于描述一次图像捕获操作,主要负责监听自己会话的状态回调CameraCaptureSession.StateCallback和CameraCaptureSession.CaptureCallback捕获回调,还有发送处理CameraRequest;CameraRequest则可以看成是一个"JavaBean"的作用用于描述希望什么样的配置来处理这次请求;最后三个回调用于监听对应的状态。
三、Camera2 使用步骤
初始化并启动HandlerThread(因为创建会话是好时的操作不宜放在主线程)
private void startBackgroundThread() {
LogUtil.showModelLog("摄像头"+"启动HandlerThread");
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
实现相关Surface的回调(是为了能在相关Surface可以用的时候自动开启预览)
private final TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
LogUtil.showModelLog("摄像头"+"当TextureView 可用时");
//3.在TextureView可用的时候尝试打开摄像头
openCamera(width, height);
public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
configureTransform(width, height);
public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
public void onSurfaceTextureUpdated(SurfaceTexture texture) {
实现CameraDevice.StateCallback回调
初始化CameraManager对象,进行一些参数设置工作之后,使用CameraManager实例打开Camera
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
当在CameraDevice.StateCallback回调中监听到Camera成功被打开时,初始化CameraDevice并通过CameraDevice创建捕获图像请求并把对应的Surface附着到请求上,完成CameraRequest的创建。
实现CameraCaptureSession.StateCallback和CameraCaptureSession.CaptureCallback回调
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
LogUtil.showModelLog("摄像头"+"在CameraCaptureSession.CaptureCallback捕获回调处理CaptureResult,在process里预览成功工作");
// We have nothing to do when the camera preview is working normally.
case STATE_WAITING_LOCK: {
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (afState == null) {
captureStillPicture();
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
runPrecaptureSequence();
case STATE_WAITING_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
mState = STATE_WAITING_NON_PRECAPTURE;
case STATE_WAITING_NON_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
public void onCaptureProgressed(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull CaptureResult partialResult) {
process(partialResult);
LogUtil.showDebugLog("onCaptureProgressed");
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
process(result);
LogUtil.showModelLog("摄像头"+"在CameraCaptureSession.CaptureCallback捕获回调处理CaptureResult");
再通过CameraDevice创建CameraCaptureSession,在会话配置完成并开始处理请求时候会触发CameraCaptureSession.StateCallback的onConfiged方法,在完成请求的参数配置后发送CameraRequest
private void createCameraPreviewSession() {
SurfaceTexture texture = cameraViewSigned.getSurfaceTexture();
assert texture !=
// We configure the size of default buffer to be the size of camera preview we want.
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);
LogUtil.showModelLog("摄像头"+"通过调用CameraDevice.createCaptureRequest方法创建mPreviewRequestBuilder预览请求,并把要附着的Surface通过addTarget 封到请求中");
// We set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder= mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
LogUtil.showModelLog("摄像头"+"通过调用CameraDevice.createCaptureSession方法并传入CameraCaptureSession.StateCallbac创建预览会话mCaptureSession");
// Here, we create a CameraCaptureSession for camera preview.
mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
new CameraCaptureSession.StateCallback() {
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
LogUtil.showModelLog("摄像头"+"当预览会话完成配置并开始处理请求时候,把闪光灯,模式等参数封装到预览请求");
// When the session is ready, we start displaying the preview.
mCaptureSession = cameraCaptureS
// Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
LogUtil.showModelLog("摄像头"+"接着把预览请求设置到预览会话mCaptureSession,传入CameraCaptureSession.CaptureCallback捕获回调并发出不断捕获图像的请求");
mCaptureSession.setRepeatingRequest(mPreviewRequest,
mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
showToast("拍照失败");
} catch (CameraAccessException e) {
e.printStackTrace();
此时CameraCaptureSession.CaptureCallback监听被激活,基本完成预览流程
然后拍照的流程就很简单了,本质上就是改变了CameraCaptureSession.CaptureCallback里状态常量的值,然后再次发送请求,然后触发了回调,此时状态常量已经改变,就会执行不同的逻辑,太简单了不想多说,最后附上一张Camera2
预览的完整流程日志。
这里写图片描述
CameraManager处于顶层管理位置负责检测检测获取所有摄像头并设置输出参数,传入指定的CameraDevice.StateCallback回调,然后打开指定摄像头,并触发CameraDevice.StateCallback中的onOpened方法,并在onOpened方法里开始通过调用创建预览会话,,CameraDevice负责创建请求CameraCharacteristics、CameraRequest与CameraRequest.Builder、CameraCaptureSession以及CaptureResult则可以看成是一个JavaBean的作用用于描述以什么样的配置来处理这次请求。
四、Camera2 实战使用并封装Camera2Helper类
Camera2Helper类只是简单的封装了下,为了让Camera2的初始化和Activity 高度分离,这个类只是Demo 阶段部分有待优化,另外结合我具体的业务,对于图片大小有限制,所以我都是默认采用采样压缩率方式对图片进行压缩
package com.crazyview.crazycamera2;
import android.M
import android.app.A
import android.content.C
import android.content.pm.PackageM
import android.content.res.C
import android.graphics.B
import android.graphics.ImageF
import android.graphics.M
import android.graphics.P
import android.graphics.RectF;
import android.graphics.SurfaceT
import android.hardware.camera2.CameraAccessE
import android.hardware.camera2.CameraCaptureS
import android.hardware.camera2.CameraC
import android.hardware.camera2.CameraD
import android.hardware.camera2.CameraM
import android.hardware.camera2.CameraM
import android.hardware.camera2.CaptureR
import android.hardware.camera2.CaptureR
import android.hardware.camera2.TotalCaptureR
import android.hardware.camera2.params.StreamConfigurationM
import android.media.I
import android.media.ImageR
import android.os.B
import android.os.H
import android.os.HandlerT
import android.support.annotation.NonN
import android.support.annotation.RequiresA
import android.support.v4.app.ActivityC
import android.util.S
import android.util.SparseIntA
import android.view.S
import android.view.TextureV
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import java.nio.ByteB
import java.util.ArrayL
import java.util.A
import java.util.C
import java.util.C
import java.util.L
import java.util.concurrent.S
import java.util.concurrent.TimeU
* Auther: Crazy.Mo
* DateTime:
* Summary:
public class Camera2Helper {
private static A
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
private static final int STATE_PREVIEW = 0;
private static final int STATE_WAITING_LOCK = 1;//Camera state: Waiting for the focus to be locked.
private static final int STATE_WAITING_PRECAPTURE = 2;//Camera state: Waiting for the exposure to be pre capture state.
private static final int STATE_WAITING_NON_PRECAPTURE = 3;//Camera state: Waiting for the exposure state to be something other than precapture.
private static final int STATE_PICTURE_TAKEN = 4;//Camera state: Picture was taken.
private static final int MAX_PREVIEW_WIDTH = 1920;//Max preview width that is guaranteed by Camera2 API
private static final int MAX_PREVIEW_HEIGHT = 1080;//Max preview height that is guaranteed by Camera2 API
private AutoFitTextureView textureV
private String mCameraId;
private CameraCaptureSession mCaptureS
private static CameraDevice mCameraD
private Size mPreviewS
private HandlerThread mBackgroundT//An additional thread for running tasks that shouldn't block the UI.
private Handler mBackgroundH//A {@link Handler} for running tasks in the background.
private ImageReader mImageR
private static File mFile =
private Semaphore mCameraOpenCloseLock = new Semaphore(1);//以防止在关闭相机之前应用程序退出
private boolean mFlashS
private int mSensorO
private CaptureRequest.Builder mPreviewRequestB//{@link CaptureRequest.Builder} for the camera preview
private CaptureRequest mPreviewR//{@link CaptureRequest} generated by {@link #mPreviewRequestBuilder}
private int mState = STATE_PREVIEW;//{#see mCaptureCallback}The current state of camera state for taking pictures.
private static CameraM
private AfterDoL
private boolean isNeedHideProgressbar=
//从屏幕旋转转换为JPEG方向
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
//This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a still image is ready to be saved.
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new Camera2Helper.ImageSaver(reader.acquireNextImage(), mFile));
private final TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
//3.在TextureView可用的时候尝试打开摄像头
openCamera(width, height);
public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
configureTransform(width, height);
public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
public void onSurfaceTextureUpdated(SurfaceTexture texture) {
//实现监听CameraDevice状态回调
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
public void onOpened(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
mCameraDevice = cameraD
createCameraPreviewSession();//要想预览、拍照等操作都是需要通过会话来实现,所以创建会话用于预览
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice =
public void onError(@NonNull CameraDevice cameraDevice, int error) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice =
* A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture.
private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
switch (mState) {
case STATE_PREVIEW: {
if(isNeedHideProgressbar) {
listener.onAfterPreviewBack();
isNeedHideProgressbar=
// We have nothing to do when the camera preview is working normally.
case STATE_WAITING_LOCK: {
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (afState == null) {
captureStillPicture();
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
runPrecaptureSequence();
case STATE_WAITING_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
mState = STATE_WAITING_NON_PRECAPTURE;
case STATE_WAITING_NON_PRECAPTURE: {
// CONTROL_AE_STATE can be null on some devices
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture();
public void onCaptureProgressed(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull CaptureResult partialResult) {
process(partialResult);
LogUtil.showDebugLog("onCaptureProgressed");
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
process(result);
* {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state.
private volatile static Camera2H///注意:用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最的值
private Camera2Helper(Activity act, AutoFitTextureView view) {
private Camera2Helper(Activity act, AutoFitTextureView view, File file) {
activity =
textureView =
manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
public static Camera2Helper getInstance(Activity act, AutoFitTextureView view, File file) {
if (singleton == null) {
synchronized (Camera2Helper.class) {
singleton = new Camera2Helper(act, view, file);
* 开启相机预览界面
public void startCameraPreView() {
startBackgroundThread();
//1、如果TextureView 可用则直接打开相机
new Handler().postDelayed(new Runnable() {
public void run() {
if (textureView != null) {
if (textureView.isAvailable()) {
openCamera(textureView.getWidth(), textureView.getHeight());
textureView.setSurfaceTextureListener(mSurfaceTextureListener);//设置TextureView 的回调后,当满足之后自动回调到
},300);//建议加上尤其是你需要在多个界面都要开启预览界面时候
* 开启HandlerThread
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
* Stops the background thread and its {@link Handler}.
private void stopBackgroundThread() {
if (mBackgroundThread == null) {
mBackgroundThread.quitSafely();
mBackgroundThread.join();
mBackgroundThread =
mBackgroundHandler =
} catch (InterruptedException e) {
e.printStackTrace();
public void takePicture() {
lockFocus();
* 通过会话提交捕获图像的请求,通常在捕获回调回应后调用
private void captureStillPicture() {
if (null == mCameraDevice) {
//创建用于拍照的CaptureRequest.Builder
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
// 使用和预览一样的模式 AE and AF
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
setAutoFlash(captureBuilder);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
unlockFocus();
listener.onAfterTakePicture();
LogUtil.showErroLog("onCaptureCompleted" + "保存照片成功");
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
* Lock the focus as the first step for a still image capture.
private void lockFocus() {
if (mCaptureSession == null) {
// This is how to tell the camera to lock focus.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
// Tell #mCaptureCallback to wait for the lock.
mState = STATE_WAITING_LOCK;
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
* Unlock the focus. This method should be called when still image capture sequence is
* finished.
private void unlockFocus() {
// Reset the auto-focus trigger
if (mCaptureSession == null) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
setAutoFlash(mPreviewRequestBuilder);
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
// After this, the camera will go back to the normal state of preview,重新预览
mState = STATE_PREVIEW;
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
* 设置自动闪光灯
* @param requestBuilder
private void setAutoFlash(CaptureRequest.Builder requestBuilder) {
if (mFlashSupported) {
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
* 从指定的屏幕旋转中检索JPEG方向。
* @param rotation The screen rotation.
* @return The JPEG orientation (one of 0, 90, 270, and 360)
private int getOrientation(int rotation) {
// Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X)
// We have to take that into account and rotate JPEG properly.
// For devices with orientation of 90, we simply return our mapping from ORIENTATIONS.
// For devices with orientation of 270, we need to rotate the JPEG 180 degrees.
return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360;
* Run the precapture sequence for capturing a still image. This method should be called when
* we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}
private void runPrecaptureSequence() {
// This is how to tell the camera to trigger.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
// Tell #mCaptureCallback to wait for the precapture sequence to be set.
mState = STATE_WAITING_PRECAPTURE;
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
private void createCameraPreviewSession() {
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture !=
// 将默认缓冲区的大小配置为我们想要的相机预览的大小。
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);
// set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);// 把显示预览界面的TextureView添加到到CaptureRequest.Builder中
// create a CameraCaptureSession for camera preview.
mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
// When the session is ready, we start displaying the preview.
mCaptureSession = cameraCaptureS
// 设置自动对焦参数并把参数设置到CaptureRequest.Builder中
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
//设置闪光灯自动模式
setAutoFlash(mPreviewRequestBuilder);
// 封装好CaptureRequest.Builder后,调用build 创建封装好CaptureRequest 并发送请求
mPreviewRequest = mPreviewRequestBuilder.build();
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
} catch (CameraAccessException e) {
e.printStackTrace();
private void setUpCameraOutputs(int width, int height) {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
一般来说当你的Android智能设备有前后摄像头的话,那么后置摄像头的id为0 前置的为1
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
//前置摄像头
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
}else if(facing != null && facing == CameraCharacteristics.LENS_FACING_BACK){
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) {
// For still image captures, we use the largest available size.
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new Camera2Helper.CompareSizesByArea());
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, /*maxImages*/2);//初始化ImageReader
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);//设置ImageReader监听
//处理图片方向相关
int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
boolean swappedDimensions =
switch (displayRotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
if (mSensorOrientation == 90 || mSensorOrientation == 270) {
swappedDimensions =
case Surface.ROTATION_90:
case Surface.ROTATION_270:
if (mSensorOrientation == 0 || mSensorOrientation == 180) {
swappedDimensions =
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
int rotatedPreviewWidth =
int rotatedPreviewHeight =
int maxPreviewWidth = displaySize.x;
int maxPreviewHeight = displaySize.y;
if (swappedDimensions) {
rotatedPreviewWidth =
rotatedPreviewHeight =
maxPreviewWidth = displaySize.y;
maxPreviewHeight = displaySize.x;
if (maxPreviewWidth & MAX_PREVIEW_WIDTH) {
maxPreviewWidth = MAX_PREVIEW_WIDTH;
if (maxPreviewHeight & MAX_PREVIEW_HEIGHT) {
maxPreviewHeight = MAX_PREVIEW_HEIGHT;
// Danger, W.R.! Attempting to use too large a preview size could
exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
maxPreviewHeight, largest);
// We fit the aspect ratio of TextureView to the size of preview we picked.
int orientation = activity.getResources().getConfiguration().
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
textureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
textureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
// 设置是否支持闪光灯
Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
mFlashSupported = available == null ? false :
mCameraId = cameraId;
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
* 为了避免太大的预览大小会超过相机总线的带宽限
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List&Size& bigEnough = new ArrayList&&();
// Collect the supported resolutions that are smaller than the preview Surface
List&Size& notBigEnough = new ArrayList&&();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getWidth() &= maxWidth && option.getHeight() &= maxHeight && option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() &= textureViewWidth && option.getHeight() &= textureViewHeight) {
bigEnough.add(option);
notBigEnough.add(option);
if (bigEnough.size() & 0) {
return Collections.min(bigEnough, new Camera2Helper.CompareSizesByArea());
} else if (notBigEnough.size() & 0) {
return Collections.max(notBigEnough, new Camera2Helper.CompareSizesByArea());
return choices[0];
* 打开指定摄像头
private void openCamera(int width, int height) {
//4、设置参数
setUpCameraOutputs(width, height);
configureTransform(width, height);
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
* Closes the current {@link CameraDevice},异步、异步、异步操作
private void closeCamera() {
mCameraOpenCloseLock.acquire();
if (null != mCaptureSession) {
mCaptureSession.close();
mCaptureSession =
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice =
if (null != mImageReader) {
mImageReader.close();
mImageReader =
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
mCameraOpenCloseLock.release();
* Configures the necessary {@link android.graphics.Matrix} transformation to `textureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `textureView` is fixed.
* @param viewWidth
The width of `textureView`
* @param viewHeight The height of `textureView`
private void configureTransform(int viewWidth, int viewHeight) {
if (null == textureView || null == mPreviewSize) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
textureView.setTransform(matrix);
* 释放Act和View
public void onDestroyHelper() {
stopBackgroundThread();
closeCamera();
activity =
textureView =
private static class CompareSizesByArea implements Comparator&Size& {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public int compare(Size lhs, Size rhs) {
// We cast here to ensure the multiplications won't overflow
return Long.signum((long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight());
private static class ImageSaver implements Runnable {
private final Image mI
private final File mF
public ImageSaver(Image image, File file) {
public void run() {
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
FileOutputStream output =
buffer.get(bytes);
Bitmap bitmap = BitmapUtil.bytes2Bitmap(bytes);//原始的转为Bitmap
Bitmap bitmapAfter = BitmapUtil.compressBitmapBySampleSize(bitmap, 4);//压缩
byte[] bytesAfter = BitmapUtil.bitmap2Bytes(bitmapAfter);//压缩后的Bitmap 转为Bytes
output = new FileOutputStream(mFile);
output.write(bytesAfter);//写到文件输出流
} catch (IOException e) {
LogUtil.showErroLog(e.getMessage().toString());
e.printStackTrace();
} finally {
mImage.close();
if (null != output) {
output.close();
} catch (IOException e) {
e.printStackTrace();
public void setAfterDoListener(AfterDoListener listener){
this.listener=
public interface AfterDoListener {
void onAfterPreviewBack();
void onAfterTakePicture();
在Activity中使用
public class MainActivity extends AppCompatActivity implements Camera2Helper.AfterDoListener {
private Camera2Helper camera2H
private AutoFitTextureView textureV
private ImageView imageV
private ProgressBar progressB
public static final String PHOTO_PATH = Environment.getExternalStorageDirectory().getPath();
public static final String PHOTO_NAME = "camera2";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onResume() {
super.onResume();
camera2Helper.startCameraPreView();
protected void onDestroy() {
super.onDestroy();
camera2Helper.onDestroyHelper();
private void init(){
textureView= (AutoFitTextureView) findViewById(R.id.texture);
imageView= (ImageView) findViewById(R.id.imv_photo);
button= (Button) findViewById(R.id.btn_take_photo);
progressBar= (ProgressBar) findViewById(R.id.progressbar_loading);
file = new File(PHOTO_PATH, PHOTO_NAME + ".jpg");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
camera2Helper.takePicture();
camera2Helper=Camera2Helper.getInstance(MainActivity.this,textureView,file);
camera2Helper.setAfterDoListener(this);
public void onAfterPreviewBack() {
runOnUiThread(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
public void onAfterTakePicture() {
runOnUiThread(new Runnable() {
public void run() {
InputStream input =
input = new FileInputStream(file);
byte[] byt = new byte[input.available()];
input.read(byt);
imageView.setImageBitmap(BitmapUtil.bytes2Bitmap(byt));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
其他参见源码(注意Demo中没有做动态权限的处理)
饱醉豚,嗟,来食!
上一篇介绍了如何使用系统相机简单、快速的进行拍照,本篇将介绍如何使用框架提供的API直接控制摄像机硬件。 控制相机 直接控制设备相机比从现有相机应用程序中请求图片或视频要复杂很多。但是,如果我们要创建专门的相机应用程序或完全集成到应用程序的界面中,就不得不通过这种方式实现。...
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Android 获取 View 宽高的常用正确方式,避免为零 - 掘金相信有很多朋友...
介绍 今天为大家介绍一下如何在 Android 上进行视频采集。在 Android 系统下有两套 API 可以进行视频采集,它们是 Camera 和 Camera2 。Camera是以前老的 API ,从 Android 5.0(21)之后就已经放弃了。我今天主要给大家介绍...
学习资料: 肾虚将军android camera2 详解说明 极客学院android.hardware.camera2 使用指南 2017 月 12 月 16 号补充一个学习资料 Android平台Camera开发实践指南 :25 注意权限,注意权限,...
原文链接http://www.cnblogs.com/kenshincui/p/4186022.html 音频在iOS中音频播放从形式上可以分为音效播放和音乐播放。前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度、循环等控制。后者指的是一些较长的音...
大致安排 时间:七月一下午五点点半出发,八月十五左右回来。 内容:找一份或几份兼职工作,在北京生活一个月半左右,把一些重要东西重要地方都高质量体验一遍。 目的:锻炼锻炼自己求职的能力,锻炼做一些计划的能力,开阔下自己眼界,给暑假一个交代。 目标:赚到四千到五千以上,把北京一...
我喜欢你, 尽管技术很low
受时任武汉大学教务处主任的朱光潜老师劝告,齐邦媛先生于大二的时候放弃哲学系转入外文系。自此,徜徉于中外文学世界,如鱼得水。朱光潜老师选教材之精炼,开始时所选之诗都以教育文学品味为主,教学生什么是好诗。第二部分则以知性为主。齐邦媛先生称这是她想象力初启的双耳,带着双眼望向窗...
函数作为参数 以乘法为例 1.multiply为定义的方法,作为参数传入performOperation 2.把multiply函数作为一个块,需要改变书写的语法 3.因为swift可以做到类型推断,因此可以进一步将块简化 4.由于performOperation方法知道调...

我要回帖

更多关于 cameramanager.java 的文章

 

随机推荐