Get full screen preview with Android camera2

You should change measured width and height to cover full screen, not to fit the screen as below.

From:

if (width < height * mRatioWidth / mRatioHeight) 

to

if (width > height * mRatioWidth / mRatioHeight)

It worked fine for me.


this is the solution for your problem. In this line the aspect ratio is set to 3/4. I changed chooseVideSize method to pick video size with hd resolution for MediaRecorder.

    private static Size chooseVideoSize(Size[] choices) {
        for (Size size : choices) {
            // Note that it will pick only HD video size, you should create more robust solution depending on screen size and available video sizes
            if (1920 == size.getWidth() && 1080 == size.getHeight()) {
                return size;
            }
        }
        Log.e(TAG, "Couldn't find any suitable video size");
        return choices[choices.length - 1];
    }

Then I corrected this method to pick preview size accordingly to video size aspect ratio and below is result.

private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<Size>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    double ratio = (double) h / w;
    for (Size option : choices) {
        double optionRatio = (double) option.getHeight() / option.getWidth();
        if (ratio == optionRatio) {
            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[1];
    }
}

I hope it will help you!