Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Landscape preview distorted / zoomed #73

Closed
mjf009 opened this issue Apr 21, 2017 · 10 comments
Closed

Landscape preview distorted / zoomed #73

mjf009 opened this issue Apr 21, 2017 · 10 comments

Comments

@mjf009
Copy link

mjf009 commented Apr 21, 2017

Tested on Samsung tab and phone and I have a problem with when rotating to landscape the camera image looks zoomed in. It is fine on portrait mode and the captured preview is displaying ok on both landscape and portrait. Is there a fix for this.

@kemsky
Copy link

kemsky commented May 2, 2017

Galaxy Tab 2 10.1, the same story.

@rogerdcarvalho
Copy link

Anyone figured out how to fix this?

@austinkettner
Copy link
Member

@getupcast Would you be willing to submit a Pull Request for this?

@rogerdcarvalho
Copy link

I've been able to resolve this. In Camera1.Java, line 14:

this.mDeviceOrientation = deviceOrientation

Is not always set correctly. The device arbitrarily sets it to 0, even if the device is rotated. I decided to create a new boolean to store whether or not the device is in landscape or not, and base this on displayOrientation, which seems to be set consistently:

   //Device orientation is not reliable. Create a separate boolean to keep track of real orientation
        if (mDisplayOrientation == 90 || mDisplayOrientation == 270) {
            isLandscape = true;
        }

Later in line 473, the previewSize is inverted based on the device orientation:

 boolean invertPreviewSizes = (mCameraInfo.orientation + mDeviceOrientation) % 180 == 90;
        if (mPreviewSize != null && invertPreviewSizes) {
            return new Size(mPreviewSize.getHeight(), mPreviewSize.getWidth());
        }

I switched this to my new boolean:

boolean invertPreviewSizes = !isLandscape;
        if (mPreviewSize != null && invertPreviewSizes) {
            return new Size(mPreviewSize.getHeight(), mPreviewSize.getWidth());
        }

Secondly, on line 616, the mPreview previewParameters are also set based on the deviceOrientation, but on top of that, invertPreviewSizes is used to flip height and width:

 if (mDeviceOrientation == 0 || mDeviceOrientation == 180) {
                mPreview.setPreviewParameters(
                        getPreviewResolution().getWidth(),
                        getPreviewResolution().getHeight(),
                        mCameraParameters.getPreviewFormat()
                );
            } else {
                mPreview.setPreviewParameters(
                        getPreviewResolution().getHeight(),
                        getPreviewResolution().getWidth(),
                        mCameraParameters.getPreviewFormat()
                );
            }

            mCameraParameters.setPreviewSize(
                    invertPreviewSizes ? getPreviewResolution().getHeight() : getPreviewResolution().getWidth(),
                    invertPreviewSizes ? getPreviewResolution().getWidth() : getPreviewResolution().getHeight()
            );

Here I also switched to my new boolean instead of mDeviceOrientation to check what is required, but I only use this information to determine whether to invert height and weight. I don't think it makes sense to first set the preview on width/height or vice/versa, but then invert again. So the new code ended up being:

boolean invertPreviewSizes = !isLandscape;

if (getPreviewResolution() != null) {

            mPreview.setPreviewParameters(
                    getPreviewResolution().getWidth(),
                    getPreviewResolution().getHeight(),
                    mCameraParameters.getPreviewFormat()
            );

            mCameraParameters.setPreviewSize(
                    invertPreviewSizes ? getPreviewResolution().getHeight() : getPreviewResolution().getWidth(),
                    invertPreviewSizes ? getPreviewResolution().getWidth() : getPreviewResolution().getHeight()
            );

Tested on Samsung S8 Android 7.0 and preview shows correctly in both Landscape and Portrait. Hope this helps to resolve this across the board for all devices.

@rogerdcarvalho
Copy link

@austinkettner sorry was logged in under corporate account. I can't really do a pull request because I made many other changes specific to my implementation that may muddy the waters....

@austinkettner
Copy link
Member

Understood @rogerdcarvalho we will get this in soon =)

@harishvishwakarma
Copy link

Is this in the code yet? I am getting the issue on Samsung

@austinkettner
Copy link
Member

@harishvishwakarma Can you test this in the 1.0.0 PR? #318 - should be resolved, but if not let me know!

@harishvishwakarma
Copy link

@austinkettner : It is resolved in the mentioned PR. I am very much impressed at how much easy this makes Camera implementation. Thanks for the library.
When are you planning to put this to master branch? I am planning to use it for a production application.
Methods have changed in this one though and cannot really find documentation for that.

  1. How to turn on autofocus?
  2. How to turn on auto-crop to view bounds?
  3. How to turn on pinch to zoom?

@austinkettner
Copy link
Member

Glad it's working much better in the 1.0.0! Can you please comment in the PR with 1-3, we will add documentation for this shortly.

Documentation as a whole for 1.0.0 is still coming, so we plan on having a fully dedicated documentation site ahead of merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants