Android - Distorted Camera Preview - android

I am currently writing an android app which runs displays a circle drawable on the preview frame, runs edge detection and then takes a photo once the user lines up a coin perfectly in the circle.
For some reason (despite being fullscreen with no title etc.), I am started to realize the camera preview is every so slightly distorted.
I have tried setting the previewsize to 480x640 while also setting the surfaceholder size to 480x640, however if I save the preview to bitmap and also take a photo, you can notice the distortion.
Has anybody else had this kind of problem / or have any ideas as to a solution?

I have encountered the same issue.
Hopefully I found that ApiDemos includes a working example with Camera.
You can find it here: com.example.android.apis.graphics.CameraPreview.
I didn't digged the example, but I saw it's working fine just as my real camera, no distortion at all.

Related

Camera2 preview stretched when cropping with SCALER_CROP_REGION

I want to display a preview in fullscreen using camera2 api. The camera size I selected is 4160x3120 (16:9) and my screen is 1080x1920 (9:16). Therefore if I want the preview to be right scaled I have to crop the camera output :
Rect zoomCrop = new Rect(0, 0, 1755, 3120);
captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCrop);
The cropped preview should be of size 1755x3120 which has a ratio of 9:16 as well. Still the preview is streched.
Question :
Am I using SCALER_CROP_REGION correctly ? If not, how am I supposed to use it ?
Thanks in advance
I've been struggling with the same sort of problem, as I want to implement a camera preview stream into a square TextureView.
Working with the SCALER_CROP_REGION seemed like the obvious place to resolve issues with incompatible aspect ratios, but I don't think that operation will ever actually change the aspect ratio of the result, hence it doesn't prevent stretching/warping behaviour. (the docs talk about this)
I've made progress by implementing a similar transform to the one in the Camera2Basic example project - see the function configureTransform in Camera2BasicFragment).
Using this sort of view-level scaling matrix, you can manage the scaling/cropping of the camera preview without altering the size of your view.
It sounds like you are getting a landscape image and displaying it in a portrait view - this means that you will have 'pillar box' lines above and below the image if you want to avoid distortion.
If you did want to fill the screen, and not have distortion or the pillar box lines then you would have to accept that part of your image will not be visible, as its outside the shape of the viewing area.
SCALER_CROP_REGION is tricky to understand without diagrams - I found the best explanation to be in the Andoir source documentation here: https://source.android.com/devices/camera/camera3_crop_reprocess
One diagram reproduced from that helps as an example here - the reason for highlight this is to show how the co-ordinates are being used. If you look at your crop region it will be biased towards the top left as you start at (0,0), which I am not sure is what you actually want.

android capture portion of camera view

I come up to you because I have a big problem.
I would like to capture only a portion of the camera view.
I basically want something like this :
I tried to do the same with AndroidStudio, by adding a picture on the screen, here is the result :
It's ugly but it was just to try.
You basically need to put your face into the transparent area, and then click on "take picture". I would like the camera to not take the black part.
How is it possible ? Is it possible to create a kind of mask for the camera ?
I have in mind that in this case, I'll need to take the entire picture and scale it correctly by software. But then, I have to do it differently for every screen sizes. It looks incredibly difficult.
Instead, is it possible to directly modify the cameraView (SurfaceView, or SurfaceHolder) so that I won't need the image, and the picture will automatically fit the "mask" because it's actually the only thing the camera see.
Have you got some clues ?
thank you very much !
The camera sees whatever enters its lens. You cannot "make" it see only a certain shape out of its normal rectangle (unless you tape some black paper over it :)). The cropping is always done after the picture is taken. The SurfaceView and SurfaceHolder are just tools to help you present the picture (which can be already modified, if you want it to) on the phone screen.
No you cannot mask camera capture. You can apply the mask to result. Note that often you get still picture at a resolution much higher than what you use for preview. And preview resolution is not the same as screen resolution (which you should use to put the black mask on the screen). In such case, your postprocessing mask should match the picture resolution and not the preview resolution.

Distorted images when using GLSurfaceView for camera preview

I've got the problem, that when I try to use a GLSurfaceView to display the camera preview, the preview image ends up being totally distorted, but only if I choose a preview size of >= 1280x720. At lower resolutions (e.g. 960x720) everything is fine. Here is what the preview image looks like:
I noticed this while I was trying to record a video stream from the camera, which works fine appart from the distorted images. So I tried to just show the camera preview using this code -> http://maninara.blogspot.co.at/2012/09/render-camera-preview-using-opengl-es.html
I noticed that as soon as the width gets larger than 1000px, the image ends up distorted, and also tried to use another image format, but that didn't help either. I'm running this on a Google Glass API 19.
Kindest regards,
Wolfram

Camera preview on small surfaceView not showing the whole camera output

My app is showing the camera preview on one of my activities. I would like to show the camera preview but not taking the full screen, just inside a small box.
The problem is that right now the image that i get from the camera is smaller that I want. For example, when I open it I can only see there my eyes and nose, but I would like to see my whole face.
It's like my surfeceView is acting like a window in a house that only allows me to see part of the views outside. The idea is that in the surfaceView (that does not take the whole screen space) I get the whole camera output but resized in order to fit the surfaceView.
Is that a problem with the camera preview size? Or should I somehow resize the camera output?
Thanks in advance
You should give us some code to find the problem, at least the layout.xml file and the class that uses SurfaceView and SurfaceHolder.Callbacks. For now, you can try the following if you didn't.
In the onSurfaceChanged method use the two int parameters width and height to calculate the best size for your preview. You do this with setPreviewSize and getSupportedPreviewSize.
If you can't fix the issue in an easy way you can also try to use an ImageView or a TextureView and paint on them the image that you are given from the onPreviewFrame callback.

How is the camera preview connected with the final image output?

I've always been under the impression that the preview and the final output are not connected in any way; meaning that I can set the preview to be some arbitrary dimension and that the final JPG will be whatever specific resolution I set in to be in the params, but I just ran into a very odd situation where the image data coming back in the byte[] that's in the jpg callback is different, depending on what dimensions I set my preview to.
Can someone enlighten me on what actual relationship the preview has on the final JPG? (or point me to documentation on said relationship).
TIA
[Edit]
As per ravi's answer, this was my assumption as well, however, I see no alternative but to surmise that they are, in fact, directly connected based on the evidence. I'll post code if necessary (though there's a lot of it) but here's what I'm doing.
I have a preview screen where the user takes a photo of themselves. I then display the picture captured (from the jpg callback bitmap data) in a subsequent draw view and allow them to trace a shape over their photo. I then pass the points of their polygon into a class that cuts that shape out of the original image, and gives back the cut image.
All of this works, BUT depending on how I present the PREVIEW, the polygon cutting class crashes on an array out of bounds index as it tries to access pixels on the final image that simply don't exist. This effect is produced EXCLUSIVELY by altering the shape of the preview View's dimensions. I'm not altering ANYTHING else in the code, and yet, just by mis-shaping my preview view, I can reproduce this error 100% of the time.
I can't see an explanation other than that the preview and the final are directly connected somehow, since I'm never operating on the preview's data, I only display it in a SurfaceView and then move on to deal exclusively with the data from the JPG callback following the user having taken their photo.
There is no relation between the preview resolution and the final image that is captured.
They are completely independent (at least for the still image capture). The preview resolution and the aspect ratio are not interrelated with the final image resolution and the aspect ratio in anyway.
In the camera application that I have written, the preview is always VGA but the image I capture varies from 5M to VGA (depending on the device capability)
Perhaps if you can explain the situation it would be more helpful.
We are currently developing a camera application and face very similiar problems. In our case, we want to display a 16:9 preview, while capturing a 4:3 picture. On most devices this works without any problems, but on some (e.g. Galaxy Nexus, LG Optimus 3D), the output camera picture depends on the preview you've chosen. In our case the outcoming pictures on that devices are distorted when the preview ratio is different from the picture ratio.
We tried to fix this, by changing the preview resolution to a better one just before capturing the image. But this does not work on some devices and occure error while starting the preview again after capturing is finished.
We also tried to fix this, by enlarging the SurfaceView to fullscreen-width and "over fullscreen"-height to make a 16:9 preview out of a 4:3 preview. But this does not work, because SurfaceViews can not be higher then screenheight.
So there IS any connection on SOME devices, and we really want to know, how to fix/workaround this.

Categories

Resources