Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera display doesn't maintain aspect ratio/seems skewed

Tags:

android

I'm running into an issue with camera input in Android. After searching over the internet for half a day I'm not getting any further.

Wat I want to achieve at this stage is correctly displaying the current camera input. Nothing more and nothing less for now. When holding the device in landscape mode, all seems fine. However as soon as I switch the device to portrait the display seems to be horizontally scaled in order to match the width of the screen on the device.

In order to clarify this issue a bit more, I've attached two photographs:

  • Landscape (looks correct)
  • Portrait (looks incorrect)

It seems like this issue also occurs with the camera example from the API examples, but it doesn't seem to occur when I use the camera application from my device. So clearly something else is happening there.

The code that I'm using is based on examples that can be found over the internet and has no modifications:

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
}

The API examples make use of a method "getOptimalPreviewSize(...)" which sounds like the method I need, but that does not change anything. I left it out here in order to be as brief as possible.

Am I missing something obvious or is this the expected behavior?

Hopefully someone can point me in the correct direction.

Thanks in advance!

Paul

like image 842
PaulT Avatar asked Nov 14 '22 20:11

PaulT


1 Answers

Okay, I just found the answer to my own question. The way I solved it was by going fullscreen with my application:

requestWindowFeature(Window.FEATURE_NO_TITLE);

This makes a little bit of sense, as I guess we can not use just any resolution as the preview size on the camera. I guess it will internally snap to the closest allowed resolution/aspect ratio and it doesn't do any smart things like cropping the image to match the requested resolution while maintaining aspect ratio. In my case I was displaying a title bar while the source image was fullscreen and then downsized to fit on the screen.

I think it is kind of stupid that the aspect ratio is not maintained and images are not cropped by default,

like image 121
PaulT Avatar answered Jan 08 '23 17:01

PaulT