Kæmpe Klunker
Kæmpe Klunker

Reputation: 865

Camera2 API preview aspect is broken

I am using the Camera2 API to setup an imagecapture fragment. I only want to use the camera in portrait mode. So i have set this line in the manifest in the fragment's parent activity:

android:screenOrientation="portrait"

Which locks the camera preview to portrait. However, the image aspect is just wrong. When the image is captured, there is no issues with the aspect, and the image looks as it should, but is still not in full screen. I suspect the camera output is stretched to fill the screen in my preview. How do i make both the preview and the captured image fill the entire screen and keep its aspect ratio like Snapchat does it?

I have been using the Google sample as my reference, and changed the AutoFitTextureView to match_parent in the XML layout file.

this is how it looks in normal portrait enter image description here

and this is how it looks when i rotate the phone and is still in portrait mode enter image description here

Upvotes: 0

Views: 647

Answers (2)

Prem Adithya
Prem Adithya

Reputation: 11

For anyone who has been facing this issue like I was and if you have been using the google sample from before Jun 2017, then tweaking a line in your AutoFitTextureView should fix the preview's aspect ratio. It's as simple as changing "<" to ">" which you can see here.

Upvotes: 1

Eddy Talvala
Eddy Talvala

Reputation: 18107

AutoFillTextureView attempts to keep the aspect ratio correct, but if you're changing its layout parameters, it may not be able to.

Do things work correctly if you don't make it 'match_parent'?

In general, Android UI elements will fill themselves with the input data, so you need to set the View's size to the right aspect ratio to avoid stretching.

Upvotes: 1

Related Questions