Nexus 5x 纵向反转图像

Reverse image on Nexus 5x in portrait

我一直在处理一个旧项目,该项目的 Zxing 库版本已过时,需要更新以修复 reverse image bug on the Nexus 5x. I managed to update the library but only portrait mode 应该得到支持。

if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    source = activity.getCameraManager().buildLuminanceSource(data,
                width, height);
} else {
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++)
            rotatedData[x * height + height - y - 1] = data[x + y
                    * width];
    }
    int tmp = width;
    width = height;
    height = tmp;
    data = rotatedData;
    source = activity.getCameraManager().buildLuminanceSource(data,
                width, height);
}

我现在在解码 Nexus 5X 设备上的条码时遇到问题,我们的 EAN-13 条码似乎旋转了,如下图所示。

连结 5X

Android 6.0 设备

这是一个已知问题,即 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.

基本上,如果您使用 Camera2 API,您不应该看到这种行为。