Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preview streched in Camera2 API when creating layout programmaticaly

After an update of our application we now use the Camera2 API, unfortunately the preview is streched on our test device: Samsung SM-J330F/DS, Android-Version 8.0.0, API 26

Because we don't experience this problem with Googles Camera2Basic project on the same device, we tried to adjust our project to use the same the implementations of the preview. Currently we just can't figure out the exact reason for the different previews between both projects.

Streched preview experienced with our preview.

This is expected:

Correct preview of the Camera2Basic project on the same device

In contrast to the Camera2Basic project our RelativeLayout is created programmatically in the Activity class and the AutoFitTextureView is then added to this object:

mMainLayout = new FrameLayout(this);
mMainLayout.setBackgroundColor(Color.BLACK);
mMainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));

mPreview = new AutoFitTextureView(this);
mPreview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT, Gravity.TOP));

mMainLayout.addView(mPreview);
this.setContentView(mMainLayout);

The implementation of AutoFitTextureView is the same as in the Camera2Basic project.

Later in the Camera2 class after setting the preview in setUpCameraOutputs() to 640x480 px (a resolution that should be supported by all devices) we call setAspectRatio(width, height) on mPreview:

mPreviewSize = new Size(640, 480);

if (mFrameOrientation == Configuration.ORIENTATION_LANDSCAPE) {
    mTextureView.setAspectRatio(
        mPreviewSize.getWidth(), mPreviewSize.getHeight()
    );
} else {
    mTextureView.setAspectRatio(
        mPreviewSize.getHeight(), mPreviewSize.getWidth()
    );
}

Note: Also on newer devices (like Honor 10) the preview looks fine.

like image 886
Entertain Avatar asked Nov 06 '22 23:11

Entertain


1 Answers

I think the problem is you fixed the value of mPreviewSize. For this reason the camera preview streched in some device which has resolution greater than 640*480.

mPreviewSize should be same size of AutoFitTextureView.

mPreviewSize = new Size(mPreview.getHeight(), mPreview.getWidth());
like image 173
Papan Avatar answered Nov 14 '22 22:11

Papan