• Camera2 学习总结


    Camera2预览步骤学习总结

    • 1.1 获取Camera服务打开Camera
    1. private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
    2. @Override
    3. public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
    4. Log.i(TAG,"onSurfaceTextureAvailable width:" + width + " height:" + height);
    5. if(mCameraManager == null){
    6. mCameraManager = (CameraManager)CameraActivity.this.getSystemService(Context.CAMERA_SERVICE);
    7. }
    8. try {
    9. String[] cameraIds = mCameraManager.getCameraIdList();
    10. for(String id : cameraIds){
    11. Log.i(TAG,"id = " + id);
    12. }
    13. //open background camera
    14. if(cameraIds.length > 1) {
    15. mCameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraIds[1]);
    16. List> requestKeyList = mCameraCharacteristics.getAvailableSessionKeys();
    17. for (CaptureRequest.Key requestKey : requestKeyList) {
    18. Log.i(TAG,"requestKey.getName() = " +requestKey.getName());
    19. }
    20. StreamConfigurationMap configMap = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    21. //Size[] candidateSizes = configMap.getOutputSizes(Surface.class);
    22. Size[] candidateSizes = configMap.getOutputSizes(ImageFormat.JPEG);
    23. for (Size candidateSize : candidateSizes){
    24. Log.i(TAG,"prview Width = " + candidateSize.getWidth() + " height = " + candidateSize.getHeight());
    25. }
    26. Log.i(TAG,"orientation = " + mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION));
    27. //Log.i(TAG,"scenes = " + mCameraCharacteristics.get("com.mediatek.vsdoffeature.vsdofFeaturePreviewSize").toString());
    28. mCameraManager.openCamera(cameraIds[1], mDeviceCallback, mBgHander);
    29. }
    30. //android.util.Log.i("zmao", "cameraIds = " + cameraIds.toString());
    31. } catch (Exception e){
    32. Log.i(TAG,"onSurfaceTextureAvailable exception " + e);
    33. }
    34. }
    • 1.2 CameraDevice.StateCallback回调创建预览请求
    1. private void createCameraPreviewSession(Handler handler) {
    2. try {
    3. Log.i(TAG,"createCameraPreviewSession");
    4. // 获取SurfaceTexture并设置默认缓冲区大小
    5. SurfaceTexture texture = mPreview.getSurfaceTexture();
    6. texture.setDefaultBufferSize(mPreview.getWidth(), mPreview.getHeight());
    7. // 创建预览Surface
    8. Surface surface = new Surface(texture);
    9. // 创建CaptureRequest.Builder并设置预览Surface为目标
    10. mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    11. mPreviewRequestBuilder.addTarget(surface);
    12. // 创建ImageReader并设置回调
    13. // mImageReader = ImageReader.newInstance(mTextureView.getWidth(), mTextureView.getHeight(), ImageFormat.JPEG, 1);
    14. mImageReader = ImageReader.newInstance(mPreview.getWidth(), mPreview.getHeight(), ImageFormat.JPEG, 1);
    15. // mImageReader = ImageReader.newInstance(mPreview.getWidth(), mPreview.getHeight(), ImageFormat.YUV_420_888, 2);
    16. mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, handler);
    17. // 将ImageReader的Surface添加到CaptureRequest.Builder中
    18. Surface readerSurface = mImageReader.getSurface();
    19. mPreviewRequestBuilder.addTarget(readerSurface);
    20. // 创建预览会话
    21. mCameraDevice.createCaptureSession(Arrays.asList(surface, readerSurface), mSessionCallback, handler);
    22. Log.i(TAG,"createCameraPreviewSession end");
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. }
    • 1.3 CameraCaptureSession.StateCallback中增加重复请求实施刷新
    1. private void updatePreview() {
    2. try {
    3. Log.i(TAG,"updatePreview");
    4. // 设置自动对焦模式
    5. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
    6. // 构建预览请求
    7. // 发送预览请求
    8. mCameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
    9. } catch (Exception e) {
    10. e.printStackTrace();
    11. }
    12. }
    •  1.4 ImageReader.OnImageAvailableListener中监听刷新 
    1. private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
    2. @Override
    3. public void onImageAvailable(ImageReader reader) {
    4. if(DBG) {
    5. Log.d(TAG, "onImageAvailable");
    6. }
    7. //获得mage
    8. Image image = null;
    9. try {
    10. //image = reader.acquireLatestImage();
    11. image = reader.acquireNextImage();
    12. if(mCapature){
    13. Log.i(TAG,"capature save");
    14. ImageSaver imageSaver = new ImageSaver(CameraActivity.this, image);
    15. new Thread(imageSaver).start();
    16. } else {
    17. image.close();
    18. }
    19. }catch(Exception e){
    20. Log.i(TAG,"OnImageAvailableListener error");
    21. }

    如此Camera2预览基本实现

  • 相关阅读:
    【Java】怎么处理Oracle的Clob类型字段
    Oracle 笔记
    PHP:类的自动加载
    安装ZooKeeper集群
    贪心之跳跃
    Go语言基础面试题
    书写高质量sql的一些建议
    6.2.2 【MySQL】InnoDB中的索引方案
    python接口自动化测试(八)-unittest-生成测试报告
    微信小程序下载一个视频到本地相册,显示下载进度,可取消下载
  • 原文地址:https://blog.csdn.net/tjpuzm/article/details/133376686