Camera2 API surface preview sizes

Camera2 surface preview sizes & cameraId

Setting up the surface preview width & height and cameraId for the camera2 API’s.

Part 2 of the camera2 API tutorial series which describes how to setup the camera2 API surface preview sizes.

It is important that the supported preview sizes from the camera device closely match the desired preview size of the applications preview display.

GitHub

You can clone the code from github and then Tag “camera2-surface-preview-sizes” or else run this command

git clone --branch camera2-surface-preview-sizes https://github.com/mobapptuts/recyclerview_image_gallery.git

CODE CHANGES

Create TextureView & TextureView surfaceTextureListener

private TextureView mTextureView;
    private TextureView.SurfaceTextureListener mSurfaceTextureListener =
            new TextureView.SurfaceTextureListener() {
                @Override
                public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                    setupCamera(width, height);
                }

                @Override
                public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

                }

                @Override
                public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                    return false;
                }

                @Override
                public void onSurfaceTextureUpdated(SurfaceTexture surface) {

                }
            };

Implement Activity onResume

@Override
   public void onResume() {
       super.onResume();

       if(mTextureView.isAvailable()) {

       } else {
           mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
       }
   }

Get surface preview sizes & camera Id

private void setupCamera(int width, int height) {
       CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
       try {
           for(String cameraId : cameraManager.getCameraIdList()) {
               CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
               if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
                       CameraCharacteristics.LENS_FACING_FRONT){
                   continue;
               }
               StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
               mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height);
               mCameraId = cameraId;
               return;
           }
       } catch (CameraAccessException e) {
           e.printStackTrace();
       }
   }

   private Size getPreferredPreviewSize(Size[] mapSizes, int width, int height) {
       List<Size> collectorSizes = new ArrayList<>();
       for(Size option : mapSizes) {
           if(width > height) {
               if(option.getWidth() > width &&
                       option.getHeight() > height) {
                   collectorSizes.add(option);
               }
           } else {
               if(option.getWidth() > height &&
                       option.getHeight() > width) {
                   collectorSizes.add(option);
               }
           }
       }
       if(collectorSizes.size() > 0) {
           return Collections.min(collectorSizes, new Comparator<Size>() {
               @Override
               public int compare(Size lhs, Size rhs) {
                   return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() * rhs.getHeight());
               }
           });
       }
       return mapSizes[0];
   }

Execute the application & debug the methods to confirm the values.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>