How to take a portrait photo when the device is in the landscape

I am making a rather special camera application for a device that will always physically be stored in landscape orientation. Part of the specification is that it can optionally be viewed and photographed in portrait orientation - see Configuration “A” below. enter image description here

(Now, before anyone votes to close this question, suggesting it to repeat this other SO question , let me indicate that another question was simply about whether it was possible or not, to which the stated answer is yes. This the question is how to do this.)

I already set up TextureView for preview and called camera.setPreviewTexture (), so I see the camera image on the screen. I also have the code and it works, which makes the landscape version of what I need, see Configuration "B" below.

enter image description here To configure camera settings, I first read my own camera settings using getParameters (), and then change only the ones I want to change. Therefore, in the case of configuration "B" it is just a matter of executing the following code:

camera_parameters = camera.getParameters(); camera_parameters.setPreviewSize(640,480); camera_parameters.setPictureSize(640,480); camera.setParameters(camera_parameters); 

( EDIT: I already checked that 640x480 is available for both preview and image). Then the application works as I expected.

The problem occurs when you try to configure configuration "A". This time I set the texture look wider than tall, and execute the following code:

 camera_parameters = camera.getParameters(); camera_parameters.set("orientation", "portrait"); camera_parameters.setPreviewSize(640,480); camera_parameters.setPictureSize(640,480); camera.setParameters(camera_parameters); 

Unfortunately, this does not quite do the job. What I get when the application is running looks more like: enter image description here As you can see, the image displayed in this preview is essentially an image that is wider than it is tall, which was deformed into a rectangle that is larger than its width. The resulting saved jpeg image file is also wider than tall.

I thought that maybe I needed to change the code to the following:

 camera_parameters = camera.getParameters(); camera_parameters.set("orientation", "portrait"); camera_parameters.setPreviewSize(480,640); camera_parameters.setPictureSize(480,640); camera.setParameters(camera_parameters); 

But this throws a setParameters failed exception.

So my question is: what parameters should be set for which values, between camera.getParameters(); and setParameters(camera_parameters); ?

+5
source share
1 answer

I work a lot with the camera and photography to implement an application that looks like a face in a hole. In this case, the problem is that you are trying to set the size with an absolute value.

  camera_parameters.setPreviewSize(480,640); camera_parameters.setPictureSize(480,640); 

This may cause an exception on your phone / emulator, and in another it may work well. The problem is that you have to read the allowed size (output to the camera) and create an algorithm for choosing between them. Functions:

Here you can find a very simple example:

  mCamera = Camera.open(); Camera.Parameters params = mCamera.getParameters(); List<Size> sizes = params.getSupportedPictureSizes(); // See which sizes the camera supports and choose one of those mSize = sizes.get(0); params.setPictureSize(mSize.width, mSize.height); mCamera.setParameters(params); 

With these tips, you can find the solution that best suits your expectations.

0
source

Source: https://habr.com/ru/post/1201411/


All Articles