Search code examples
androideclipsezxinglandscape-portraitportrait

Zxing (v3.2.0) Camera fixed portrait mode using eclipse


I follow the instruction step by step on Zxing Camera in Portrait mode on Android to display portrait while user's using zxing camera.

But it won't work. The scanner is still appeared in the landscape mode. I think it's because i am using the latest version (v3.2.0) of Zxing and the instruction is deprecated.

How can this be done in v3.2.0 Zxing?


Anyway, Here are the steps I've tried:

  1. Modify buildLuminanceSource(..) DecodeHandler.java

Code:

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;

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);

  1. Modify getFramingRectInPreview() in CameraManager.java

Code:

rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

  1. Modify initFromCameraParameters(...) in CameraConfigurationManager.java

    In Zxing (v3.2.0), I don't find the following code

Code:

//remove the following
if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}

  1. Add following line to rotate camera in setDesiredCameraParameters(...) in CameraConfigurationManager.java

Code:

camera.setDisplayOrientation(90);

  1. In my project, modify AndroidManifest.xml

Code:

android:screenOrientation="portrait"

Solution

  • After doing trials and errors for couple days, here's my working solution:

    It needs extra work, let's say Step 6. Always set the portrait orientation no matter what. And it worked.


    Step 6. Modify onResume in CaptureActivity.java

    if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
      //setRequestedOrientation(getCurrentOrientation()); // mark this line
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
      //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);  // mark this line
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }