Android video app setting preview size

The android video app setting preview size tutorial describes how to select the preview dimensions from the values provided from the android camera2 api StreamConfigurationMap.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-preview-size

or you can run this command

git https://github.com/mobapptuts/android_camera2_api_video_app.git clone –branch camera2-video-preview-size

Steps

Create a method for comparing sizes by area

static class CompareSizesByArea implements Comparator<Size> {

        @Override
        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());
        }

    }

Create a method for matching the TextureView dimensions against the video & preview dimensions

private static Size chooseOptimalSize(Size[] choices, int width, int height) {
        // Collect the supported resolutions that are at least as big as the preview Surface
        List<Size> bigEnough = new ArrayList<Size>();
        for (Size option : choices) {
            if (option.getHeight() == option.getWidth() * height / width &&
                    option.getWidth() >= width && option.getHeight() >= height) {
                bigEnough.add(option);
            }
        }

        // Pick the smallest of those, assuming we found any
        if (bigEnough.size() > 0) {
            return Collections.min(bigEnough, new CompareSizesByArea());
        } else {
            Log.e(TAG, "Couldn't find any suitable preview size");
            return choices[0];
        }
    }

Add a Size member for the preview

private Size mPreviewSize;

 

 Find the closest matching preview size from the camera sensor

private void setupCamera(int viewWidth, int viewHeight) {
        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);
                // Find the rotation of the device relative to the native device orientation.
                int deviceRotation = getWindowManager().getDefaultDisplay().getRotation();

                // Find the rotation of the device relative to the camera sensor's orientation.
                int totalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceRotation);

                // Swap the view dimensions for calculation as needed if they are rotated relative to
                // the sensor.
                boolean swappedDimensions = totalRotation == 90 || totalRotation == 270;
                int rotatedViewWidth = viewWidth;
                int rotatedViewHeight = viewHeight;
                if (swappedDimensions) {
                    rotatedViewWidth = viewHeight;
                    rotatedViewHeight = viewWidth;
                }
                mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedViewWidth, rotatedViewHeight);
                return;
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

 

Android video app setting preview size summary

In the android video app video preview sizes summary tutorial we were learnt how to find the best matching sizes for the preview & video displays based on the TextureView dimensions supplied.

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>