Android Zxing changes portrait orientation

I am trying to rotate the Zxing display after reading a few questions and reports about this problem. After following the instructions, the display turned, but the rectangle of the scanner (as can be seen in the attached image).

This is what I did:

  • in CameraConfigurationManager:

    camera.setDisplayOrientation(90); 
  • in DecodeHandler.java

     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; 
  • 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; 

enter image description here

+43
android zxing
Apr 18 '12 at 19:52
source share
7 answers

After many problems, I found the problem, and I hope this helps someone in the future.

The initFromCameraParameters method in initFromCameraParameters has the assumption that the scan is ALWAYS in landscape mode , and there is a fix for this when width < height . If you follow the steps in the question and remove this check, it works fine.

+30
Apr 18 2018-12-18T00:
source share

Thanks for your reply! it really helped me, one thing I noticed is that at least on zxing 2.1 you need to pass "rotatedData" to buildLuminanceSource instead of "data", the line will look like this:

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

Hope this helps someone else!

+13
Jan 25 '13 at 19:37
source share

Well, I made a small change in ProjectLibrary (xzing project) and was able to change the landscape orientation to portrait

setDesiredCameraParameters method of class CameraConfigurationManager

camera.setDisplayOrientation(90);

.. in my AndroidManifest.xml project source file. I set screenOrientation = portrait and its work on my ICS 4.0.3

  <activity android:name="com.google.zxing.client.android.CaptureActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" > <intent-filter> <action android:name="com.phonegap.plugins.barcodescanner.SCAN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 
+8
Nov 16 '12 at 23:27
source share
  • In CameraConfigurationManager :

     camera.setDisplayOrientation(90); 
  • In DecodeHandler.java :

     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; 
  • 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; 
  • In CameraConfigurationManager :

     if (width > height) { Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect"); int temp = width; width = height; height = temp; } 
  • Change android:screenOrientation="portrait" to CaptureActivity in the manifest.

+5
Oct 11 '14 at 5:26
source share

I am a barcode scanner developer. Yes, scanning in portrait mode requires much more. You must โ€œrotateโ€ the image data and take into account the orientation of the device, its default orientation and the orientation of the sensor.

The barcode scanner + scans in portrait mode, and you can integrate with it through Intent just like you integrate with a barcode scanner . (However, this application is for payment.)

+4
Apr 18 '12 at 20:23
source share

According to zxing library: 2.2.0 support for orientation changes is inherent

Add / Modify the following in the manifest:

 <activity android:name="com.journeyapps.barcodescanner.CaptureActivity" android:screenOrientation="fullSensor" tools:replace="screenOrientation" /> 

Set an additional property when calling the scanner:

 IntentIntegrator integrator = new IntentIntegrator(this); //allows portrait/landscape mode integrator.setOrientationLocked(false);//"additional property" integrator.initiateScan(); 

Link Link: https://github.com/journeyapps/zxing-android-embedded#changing-the-orientation

+3
May 31 '17 at 12:24
source share

I tried various fixes suggested in other answers, but barcode recognition remained unreliable.

I highly recommend using the repository below in portrait mode. Try it, itโ€™s fast and stable. I used it in my hybrid application.

https://github.com/Dbuggerx/BarcodeScanner

0
Jan 30 '16 at 5:51 on
source share



All Articles