Zxing portrait mode and landscape on Android

I already integrated the zxing library in my application. Now I want the zxing camera to work both in landscape mode and in portrait mode. Now it works in only one mode. Can someone help me how to do this?

+4
android eclipse portrait barcode-scanner zxing
May 01 '14 at 7:20 a.m.
source share
2 answers

I used the compyutech answer, but some things were missing. So, I put everything you need here.

1. CameraConfigurationManager: inside the initFromCameraParameters method:

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

also remove this code from the same method:

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

2. CameraManager: class variables

 private static final int MIN_FRAME_WIDTH = 340; private static final int MIN_FRAME_HEIGHT = 240; private static final int MAX_FRAME_WIDTH = 1200; private static final int MAX_FRAME_HEIGHT = 675; 

getFramingRect method:

 public synchronized Rect getFramingRect() { if (framingRect == null) { if (camera == null) { return null; } Point screenResolution = configManager.getScreenResolution(); if (screenResolution == null) { // Called early, before init even finished return null; } // Code added to enable portrait mode 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)); Log.d(TAG, "Customized code for portrait mode in getFramingRect executed (Piyush Merja) "); }else{ // Original Code width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH); height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT); } // End int leftOffset = (screenResolution.x - width) / 2; int topOffset = (screenResolution.y - height) / 2; framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); Log.d(TAG, "Calculated framing rect: " + framingRect); } return framingRect; } private static int findDesiredDimensionInRange(int resolution, int hardMin, int hardMax) { int dim = 5 * resolution / 8; // Target 5/8 of each dimension if (dim < hardMin) { return hardMin; } if (dim > hardMax) { return hardMax; } return dim; } 

getFramingRectInPreview :

 public synchronized Rect getFramingRectInPreview() { if (framingRectInPreview == null) { Rect framingRect = getFramingRect(); if (framingRect == null) { return null; } Rect rect = new Rect(framingRect); Point cameraResolution = configManager.getCameraResolution(); Point screenResolution = configManager.getScreenResolution(); if (cameraResolution == null || screenResolution == null) { // Called early, before init even finished return null; } // Code added to enable portrait mode 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; Log.d(TAG, "Customized code for portrait mode in getFramingRectInPreview executed (Piyush Merja) "); }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; } // End framingRectInPreview = rect;//this was missing in compyutech answer } return framingRectInPreview; } 

3. DecodeHandler:

inside decode method:

  long start = System.currentTimeMillis(); Result rawResult = null; // Code added to enable portrait mode 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; Log.d(TAG, "Customized code for portrait mode in decode executed (Piyush Merja) "); }//end PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height); 

4. AndroidManifest.xml:

Remove orientation settings for CaptureActivity .

+4
Apr 08 '15 at 0:55
source share

Google this question.

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

1 In the CameraConfigurationManager class, the setDesiredCameraParameters method adds the code below under the sharpened line

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

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

2 In the CameraManager class, the getFramingRect method will replace the code 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 the CameraManager class, the getFramingRectInPreview method will replace the code 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 the DecodeHandler class, decode method add below code below a 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 helps you ....

+2
Aug 29 '14 at 10:29
source share



All Articles