How to use camera2 to set full-screen preview, requirements to fit all screen sizes?

I want to achieve proportional full-screen preview in camera2. But in the process, referring to online materials, including the official demo, the preview screen made a compromise to change its aspect ratio. On my Samsung note8 phone, which has an aspect ratio of nearly 2:1, the preview screen can only be part of the full screen. But I also found that Wechat"s photo is a proportional full-screen preview, including note8.
I hope that my seniors can answer this question, or simply give me some directions.

Oct.14,2021

the onMeasure method is overridden in TextureView in the official demo, as follows:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

this code maximizes the display of TextureView without exceeding the width and height of the phone screen. The
solution is to make the TextureView always reach the maximum boundary, and the excess part is not previewed (it can actually be photographed). The code is as follows:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width > height * mRatioWidth / mRatioHeight) {    //""""
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

is actually replacing "<" in the judgment condition with ">". God knows how many programmers this small piece of code has harmed. No, no, no.


@ mo_chan Why didn't I see the big guy's answer earlier?
my requirements are different: while previewing the full screen, send the preview image to the object detection algorithm, get the detection result (containing the matrix information of the identified object, which can be framed on the incoming image) and draw a detection box on the preview image in real time.
when using a method such as @ mo_chan , you need to zoom the image to ensure that the frame can be accurately painted on the object, but zooming will cause the preview to stretch, so you finally use a compromise: make a "fit" between the resolution provided by the camera and the screen resolution, and make the preview size as close to the full screen as possible without stretching.
here wrote a blog: Portal


solved my problem, really strong. I can't figure it out by myself. How did you analyze it?

Menu