Camera2 API preview aspect is broken - android

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
and this is how it looks when i rotate the phone and is still in portrait mode

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.

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.

Related

Android: reduce height of TextureView for camera preview

I'm studing this Google project with an example of Camera2 using: https://github.com/googlesamples/android-Camera2Basic
This example works perfectly. Now I want to have the area preview smaller than in this example; so I changed the ratio like this:
mTextureView.setAspectRatio(getScreenWidth(), getScreenHeight() - bottom_area_height);
but the result is bad... the preview doesn't have a good ratio now (is too stretched horizontally):
Have you got an idea guys?
Thank you very much!
You did get what you asked for, which is a different aspect ratio. Unfortunately, there's only one aspect ratio that's correct, since it needs to match that of the camera preview buffers.
If you want a smaller preview, you need to shrink it in both directions to maintain the correct aspect ratio. So if you update the layout to have a narrower space, and keep the aspect ratio the same, the height should also go down. Or reduce the height of the surrounding frame; in any case, the aspect ratio must remain what it is to have no stretching.
AutoFitTextureView isn't designed to crop the preview, which is the other option for avoiding stretching. That could be done by messing with the TextureView transform, but it does mean you're cutting off some of the view.

Image Sized a little-bit to small, and ratio changes on rotation

using the cwac-camera library,
on the left and on the right (more times just on the left), the final image and the preview gets cut after taking a picture.
Also Xperia Z1 -> FFC (Front Facing Camera) ->
left and right side images are cut
http://s7.directupload.net/images/140228/ei5saica.png
http://s14.directupload.net/images/140228/ftnecxau.png
Another issue:
Also I added the library's view into a fullscreen fragment which will not be recreated *on rotation* -> result -> somehow the screen-preview-ratio changes on
- portrait
- landscape
- and taking picture
Means: On portrait the head is a littlebit wider (more width) than on landscape, and after taking picture the preview image is also a little bit more stretched then the (live) preview berfore. But finally the image seems to be the correct one.
This Problem was seen on an HTC One :/
Note that StackOverflow is for programming questions. Nothing here is a question. If you feel that you have identified bugs, post an issue with complete steps to reproduce the problem, including device model(s) and Android OS version(s). Use StackOverflow for questions about the library.
using the cwac-camera library, on the left and on the right (more times just on the left), the final image and the preview gets cut after taking a picture.
I would guess that it is a variation on this issue.
Another issue
Part of this is presumably the same issue. With respect to "the screen-preview-ratio changes on portrait/landscape", if you are not creating a new CameraView, and you have the camera locked to landscape, then I do not see where I would be changing the preview behavior on an orientation change.
You may set preview size and picture size independently. But to achieve the best results, choose picture size that has same aspect ratio as your preview frame. E.g. for preview 640x480, you can use 3264x2448 picture. But if you use 1280x720 preview, you will be better served by a wide picture, e.g. 4128X2322.
Note that usually, the wide (16:9) format is achieved by essentially cropping a 4:3 picture.

Android Camera Preview not Full Screen

I am developing a camera app but the camera is not full screen. I am using common "CameraPreview" class to open camera. it includes Surfaceview and Determining Optimal Size for camera. In tablet and phones, camera is not full screen. For example, tablet size is 1280x728 but camera size is 1024x768. It is shown in FrameLayout for which layout widht and height is FillParent. I could not understand and solve this issue. I wanna open camera in full screen without stretching.
Is there anybody got this issue and fix it?
Judging by the code here:(which you can use actually), you can use the method getSupportedPreviewSizes() and see what are the supported resolutions, 1024x768 could easily be the highest one available for this preview and as you found out, it is really the highest supported.
So those are the supported resolutions, there's no way you can get higher than the supported. You must find a workaround for this, like to put the preview in the center and surrond it with black frame or something like that.

Camera Preview code gives as upside down image view android

I am Using a onCLick over a button to capture an image but when we see the preview it is upside down in vertical mode where as when the same preview is seen through horizontal preview renders correctly.
Please let me know what tweaks are necessary to readjust the preview.
You have to take into account, that camera preview image origin is on top right corner if in portait mode, and axes are swapped - and it does not depend on phone orientation. You will have to flip image to display it in portrait mode properly.
take a look at
android.hardware.Camera.setDisplayOrientation
http://developer.android.com/reference/android/hardware/Camera.Parameters.html
and
android.hardware.Camera.Parameters.setRotation
http://developer.android.com/reference/android/hardware/Camera.html

Why is my camera preview not respecting the layout?

I'm creating a SurfaceView for my camera preview and attempting to have it occupy a rectangle that's smaller than the entire screen, but it's ignoring my layout command.
I assume I'm doing something wrong, but can't find an example of someone setting the preview into a smaller container.
All advice (or pointers to examples) appreciated.
That's how it's done in the default Camera application: layout file and source code.
There's a container that performs a special layout procedure depending on a given aspect ratio. I think you can adapt this solution to your needs.

Categories

Resources