4

I am using the sample code for Camera2, and I was wondering how I can make the preview and captured image in full screen? This SO question seems to solve it in video mode, but I can't find any solution for image captures. The sample fragment has a blue area at the bottom and also has the status bas. I want to hide both of these and use the entire screen to show the preview, and also capture the image in full screen size.

Community
  • 1
  • 1
Kæmpe Klunker
  • 805
  • 1
  • 9
  • 27

2 Answers2

6

Eddy is correct about the aspect ratio.The camera sensor is 4:3. The screen is usually 16:9. The sample code choose to show the entire camera preview, so part of the screen is not filled. You need to stretch to fill the whole screen, however in that case the images captured include areas not shown in the preview.

To view it in full screen, in the AutoFitTextureView, change the onMeasure method to this:

    @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);
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            } else {
                //setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            }
        }
    }

Update: As pointed out by Eddy, this will show the upper left part of the camera preview on the full screen, leaving the bottom and right side out of the view, the result is that the image is off center. In my particular case the subject needs to be centered horizontally, so I modified the transform matrix to center the subject horizontally. Here is the code:

// adjust the x shift because we are not showing the whole camera sensor on the screen, need to center the capture area
    int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
    Log.d(TAG, "screen width " + screenWidth);
    int xShift = (viewWidth - screenWidth)/2;
    matrix.setTranslate(-xShift, 0);

        mTextureView.setTransform(matrix);
Ray
  • 15,087
  • 5
  • 26
  • 50
  • 2
    As a note, this will most likely not center the preview on screen - it'll cut off on the right and bottom (though I'd have to check exactly how the containing parent expands in this case). This may or may not matter to you. – Eddy Talvala Feb 22 '18 at 21:37
  • @EddyTalvala you are right about that. I modified the transform matrix to center the preview. Updated the sample code in the post. – Ray Feb 26 '18 at 20:30
  • Translation helped! Thank you! – prex Apr 07 '20 at 05:48
2

Note that the aspect ratio of the camera sensor and the aspect ratio of your device screen most often do not match - sensors are generally 4:3 ratio and screens are generally around 16:9. Though there is wide variation in both.

Since the aspect ratios don't match, you'll need to decide if you're going to have black bars, or zoom the camera preview so that it fills the screen (but some part of the image is not visible).

In any case, to have the preview be full-screen, you just need to have the TextureView (or SurfaceView) be the entire layout. So basically, edit this layout file: fragment_camera2_basic.xml, remove all the other Views, and remove references to them in the source code. Or change them to be on top of the TextureView, not next to - this is all standard Android UI layout stuff. And you'll need to make your Activity be full-screen.

By default, AutoFillTextureView will try to maintain aspect ratio, so it'll produce black bars. If you want no black bars, then you'll have to change AutoFillTextureView, which is not straightforward, and I won't get into it here.

Eddy Talvala
  • 15,449
  • 2
  • 37
  • 42
  • Eddy, if you know how solve the "capture an image with 16:9 aspect ratio" would you be able to add that to your answer or put a link to the answer? I've been Googling, but no luck so far... – Aaron Lelevier Apr 27 '17 at 14:09
  • I'm not sure what you're asking - did you mean to link to some other StackOverflow question? If you want to capture 16:9 JPEGs, you need to select a 16:9 resolution for the ImageReader you're using to capture them with. – Eddy Talvala Apr 27 '17 at 19:44
  • This is what I was going to try next: http://stackoverflow.com/questions/26583489/android-5-0-wrong-crop-regions-on-preview-surface-and-captured-still-image Okay, thank you for your reply. I will try that as well! – Aaron Lelevier Apr 27 '17 at 21:08