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.
This is expected:
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With