Get full screen preview with Android camera2

后端 未结 2 1334
一向
一向 2021-02-07 08:37

I\'m building a custom camera using the new camera2 API. My code is based on the code sample provided by Google here.

I can\'t find a way to get the camera preview in f

相关标签:
2条回答
  • 2021-02-07 08:50

    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.

    0 讨论(0)
  • 2021-02-07 08:52

    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!

    0 讨论(0)
提交回复
热议问题