Not able to convert Zxing into portrait mode in an

2019-06-01 20:45发布

问题:

I have followed the following answer to do this. https://stackoverflow.com/a/16252917/2747591

But I am not getting what i want to do.

The image captured by camera is rotated by 90 degree while i am trying to scan. Like if you are clicking a photo of a person using the camera, then in my phone screen it is showing the preview rotated by 90 degree. But that is not what i want as it is making bar code scanning difficult to use. I want preview as it should be. Any ideas?

Here are my changes in the code

Step 1

In DecodeHandler.java I have added the following code just before buildLuminanceSource

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; // Here we are swapping, that's the difference to #11
width = height;
height = tmp;
data = rotatedData;
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);

Step 2

Modified getFramingRectInPreview() in CameraManager.java

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;

Step 3:

Disable the check for Landscape Mode in initFromCameraParameters(...) in CameraConfigurationManager.java

The instructions is to Remove

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

But I didn't find this code in my Cameraconfiguration file. so it should not matter anyways

Step 4

Added following line to rotate camera in setDesiredCameraParameters(...) in CameraConfigurationManager.java just after defining parametres

camera.setDisplayOrientation(90);

Step 5

Changed the CaptureActivity orientation from landscape to portrait in my app's manifest file like this

<activity
           android:name="com.google.zxing.client.android.CaptureActivity"
           android:screenOrientation="portrait"
           android:configChanges="orientation|keyboardHidden"
           android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           android:windowSoftInputMode="stateAlwaysHidden">
           <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
           <intent-filter>
              <action android:name="com.google.zxing.client.android.SCAN"/>
              <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
    </activity>

回答1:

I have used zxing zxing 2.3 and below solution worked for me.

1 In CameraConfigurationManager class, setDesiredCameraParameters Method add below code below pointed line

-> Camera.Parameters parameters = camera.getParameters();

 if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        camera.setDisplayOrientation(90);
 }

2 In CameraManager class, getFramingRect Method replace code as below

int width = MIN_FRAME_WIDTH; int height = MIN_FRAME_HEIGHT;
if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
   int tmp = 7 * screenResolution.x / 8; 
   width = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : (tmp);                   
   tmp = 1 * screenResolution.y / 3;
   height = (tmp) < MIN_FRAME_WIDTH ? MIN_FRAME_WIDTH : ((tmp) > MAX_FRAME_HEIGHT ?  MAX_FRAME_HEIGHT : (tmp));
}else{
   // Original Code
   width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, > MAX_FRAME_WIDTH);
   height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT,  MAX_FRAME_HEIGHT); 
}

3 In CameraManager class, getFramingRectInPreview Method replace code as below

if (context.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
   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;
}else{
   // Original code commented
   rect.left = rect.left * cameraResolution.x / screenResolution.x;
   rect.right = rect.right * cameraResolution.x / screenResolution.x;
   rect.top = rect.top * cameraResolution.y / screenResolution.y;
   rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
}

4 In DecodeHandler class, decode Method add below code below pointed line

-> Result rawResult = null;

 if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        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];
        }
        data = rotatedData;
        int tmp = width;
        width = height;
        height = tmp;

  }

Please find my working code

http://www.compyutech.co.in/repo/zxing-dynamic.zip

Hope this will help you....