Nexus 5X Reverse Landscape Sensor Fix in a Android Camera Preview App

Reverse image on Nexus 5x in portrait

That's a known issue, which is reported on the tracker.

Status: Won't Fix (Intended Behavior)

The main camera of the Nexus 5X has an unusual orientation - by Android compatibility requirements the sensor long edge has to align with the long edge of the device, which means the sensor is oriented either landscape or reverse-landscape. Most Android devices have a landscape-oriented sensor, but the 5X is reverse-landscape.

Since most devices are identical, many apps do not correctly check for the sensor orientation and apply the right adjustments. If you more or less copy-paste the sample code here:

http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

for the old camera API, it should set the correct orientation for all types of devices (phones and tablets), sensor orientations, and camera facings (front or back).

As you've noted, the JPEG orientation also has to be set, but this has always been a requirement, so fewer apps get this wrong (since phones are often held at random orientations even if the UI is forced-landscape).

The camera2 API is intentionally more user-friendly here - if you use a SurfaceView, the API ensures the preview is correctly oriented. We can't unfortunately fix the old API to do this for you.

Basically, if you use Camera2 API you shouldn't see that behavior.

Is the setDisplayOrientation sample code correct?

The snippet you've quoted, which I used and applied in my project, is no problem in my circumstances.

For all the devices (Galaxy Note, Galaxy S III, Galaxy Nexus, Galaxy Ace II, Nexus S) I used for test, info.Orientation all return 270 on front camera, and 90 on back camera.

After a few discuss with the question raiser, I found I misunderstood the questions, so I divide the answer in two parts.

For the wrong orientation in camera preview, please refer to this solution:

Solution for wrong orientation in camera preview:

First please ensure info.Orientation will return 270 on front camera, 90 on back camera.
Then please try set your camera preview activity (or similar class that handles the preview) orientation to landscape.

Thus, when you go through the code, you'll find:

degree = 90 for screen orientation, info.Orientation = 270 for the front camera. Then you'll get result = (270 - 90 + 360) % 360, result = 180, which means it will rotate clock wise 180 for your front camera view that will correct the front camera upside-down issue.

Solution for wrong orientation in camera photo results:

If this info.Orientation applies to you, then the problem may be:

  1. For some Samsung (or other) devices (Like Galaxy Note, Galaxy SIII), the camera will only write the Orientation Tag instead of rotate the real pixels.
  2. If you're using the front camera and using the code above, it will display the preview with correct orientation, but will show u the "upside down" picture if you take the shot.

Solution:

/**
*
* Get the orientation from EXIF
 * @param filepath
 * @return orientation
 */
public int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e("EXIF info", "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
// We only recognize a subset of orientation tag values.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
} else {
degree = 1;
}
Log.i("EXIF info", "Orientation degrees: " + degree);
return degree;
}

Then

if (isFromCamera) {

if (fromFrontCam) {
// try change here if the orientation still wrong, -90 means rotate counter-clockwise 90 degrees.
matrix.preRotate(-90);
} else {
matrix.preRotate(90);
}
} else {
// read EXIF data
getExifOrientation(path)
// don't forget to handle the situation that some devices won't write exif data
matrix.preRotate(degree);
}

How to set camera orientation in Android?

 Camera.Parameters params= mCamera.getParameters();
params.set("rotation", 90);
mCamera.setParameters(params);

Android - Emulator in landscape mode, screen does not rotate

It is a bug with the 2.3 and 4.4 emulators.

http://code.google.com/p/android/issues/detail?id=13189 [v2.3]
https://code.google.com/p/android/issues/detail?id=61671 [v4.4]



Related Topics



Leave a reply



Submit