1

I create a project in Android Studio, and added zxing library 2.3. I want to scan and decode only in portrait mode. i have read these manual Zxing Camera in Portrait mode on Android , but now i have portrait mode, and cant decode any barcode . i also tried to solve this problem on another way by adding portrait mode in manifest and this code to CameraConfigurationManager.java:

parameters.set("orientation", "portrait");
  parameters.setRotation(90);
  if (camera != null)
      try {
          camera.setDisplayOrientation(90);
      } catch (NoSuchMethodError ex) {
      }

Program starts in portrait mode, but didnt decode any barcode,when i rotate my phone on 90 degrees, program decode barcode ,for qr code image when captured and rotated on 90 degrees,when i rotate my phone , program decode barcode.

Community
  • 1
  • 1
Kirill
  • 169
  • 1
  • 9

1 Answers1

0

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

You can remove all conditions, If you want to work only with portrait mode.

Hope this will help you....

compyutech
  • 578
  • 3
  • 8
  • 26