Camera2与TextureView使用

  1 package com.intsig.bcrsdk.demo.Activity;
  2 
  3 import android.annotation.TargetApi;
  4 import android.app.Activity;
  5 import android.graphics.SurfaceTexture;
  6 import android.hardware.camera2.CameraAccessException;
  7 import android.hardware.camera2.CameraCaptureSession;
  8 import android.hardware.camera2.CameraCharacteristics;
  9 import android.hardware.camera2.CameraDevice;
 10 import android.hardware.camera2.CameraManager;
 11 import android.hardware.camera2.CaptureRequest;
 12 import android.hardware.camera2.CaptureResult;
 13 import android.hardware.camera2.TotalCaptureResult;
 14 import android.os.Build;
 15 import android.os.Bundle;
 16 import android.os.Handler;
 17 import android.os.HandlerThread;
 18 import android.util.Log;
 19 import android.view.Surface;
 20 import android.view.TextureView;
 21 
 22 
 23 import com.intsig.bcrsdkdemo.R;
 24 
 25 import java.util.Arrays;
 26 
 27 
 28 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 29 public class Camera2Activity extends Activity {
 30     private TextureView textureView;
 31     private String TAG="测试";
 32 
 33     private HandlerThread mThreadHandler;
 34     private Handler mHandler;
 35 
 36     private CaptureRequest.Builder mPreviewBuilder;
 37     @Override
 38     protected void onCreate(Bundle savedInstanceState) {
 39         super.onCreate(savedInstanceState);
 40         setContentView(R.layout.activity_camera2);
 41         mThreadHandler = new HandlerThread("CAMERA2");
 42         mThreadHandler.start();
 43         mHandler = new Handler(mThreadHandler.getLooper());
 44 
 45         textureView= (TextureView) findViewById(R.id.tv);
 46         textureView.setSurfaceTextureListener(textureListener);
 47 
 48     }
 49 
 50     private TextureView.SurfaceTextureListener textureListener=new TextureView.SurfaceTextureListener() {
 51         @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 52         @Override
 53         public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
 54             Log.e(TAG,"可用");
 55 
 56             //CameraManaer 摄像头管理器,用于检测摄像头,打开系统摄像头
 57             CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
 58             try {
 59                 String[] CameraIdList=cameraManager.getCameraIdList();//获取可用相机列表
 60                 Log.e(TAG,"可用相机的个数是:"+CameraIdList.length);
 61                 CameraCharacteristics cameraCharacteristics=cameraManager.getCameraCharacteristics(CameraIdList[0]);//获取某个相机(摄像头特性)
 62                 cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);//检查支持
 63 
 64                 cameraManager.openCamera(CameraIdList[0],mCameraDeviceStateCallback, mHandler);
 65             } catch (CameraAccessException e) {
 66                 e.printStackTrace();
 67             }
 68 
 69         }
 70 
 71         @Override
 72         public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
 73             Log.e(TAG,"改变");
 74         }
 75 
 76         @Override
 77         public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
 78             Log.e(TAG,"释放");
 79             return false;
 80         }
 81 
 82         @Override
 83         public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
 84             Log.e(TAG,"更新");
 85         }
 86     };
 87 
 88     //CameraDeviceandroid.hardware.Camera也就是Camera1的Camera
 89     private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
 90         @Override
 91         public void onOpened(CameraDevice camera) {
 92             try {
 93                 startPreview(camera);
 94             } catch (CameraAccessException e) {
 95                 e.printStackTrace();
 96             }
 97         }
 98 
 99         @Override
100         public void onDisconnected(CameraDevice camera) {
101 
102         }
103 
104         @Override
105         public void onError(CameraDevice camera, int error) {
106 
107         }
108     };
109 
110 
111     /**
112      * @param camera
113      * @throws CameraAccessException
114      * 开始预览
115      */
116     private void startPreview(CameraDevice camera) throws CameraAccessException {
117         SurfaceTexture texture = textureView.getSurfaceTexture();
118         texture.setDefaultBufferSize(textureView.getWidth(), textureView.getHeight());
119         Surface surface = new Surface(texture);
120         try {
121             //CameraRequest表示一次捕获请求,用来对z照片的各种参数设置,比如对焦模式、曝光模式等。CameraRequest.Builder用来生成CameraRequest对象
122             mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
123 
124         } catch (CameraAccessException e) {
125             e.printStackTrace();
126         }
127         mPreviewBuilder.addTarget(surface);
128         camera.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, mHandler);
129     }
130 
131     //CameraCaptureSession 这个对象控制摄像头的预览或者拍照
132     //setRepeatingRequest()开启预览,capture()拍照
133     //StateCallback监听CameraCaptureSession的创建
134     private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
135         @Override
136         public void onConfigured(CameraCaptureSession session) {
137             Log.e(TAG,"相机创建成功!");
138             try {
139                 session.capture(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//拍照
140                 session.setRepeatingRequest(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//返回结果
141             } catch (CameraAccessException e) {
142                 e.printStackTrace();
143                 Log.e(TAG,"这里异常");
144             }
145         }
146 
147         @Override
148         public void onConfigureFailed(CameraCaptureSession session) {
149             Log.e(TAG,"相机创建失败!");
150         }
151     };
152 
153 
154     //CameraCaptureSession.CaptureCallback监听拍照过程
155     private CameraCaptureSession.CaptureCallback mSessionCaptureCallback = new CameraCaptureSession.CaptureCallback() {
156 
157                 @Override
158                 public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
159                     Log.e(TAG,"这里接受到数据"+result.toString());
160                 }
161 
162                 @Override
163                 public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult){
164 
165                 }};
166 }

 

posted @ 2017-10-20 18:25  点滴之水  阅读(3352)  评论(0编辑  收藏  举报