49

I'm trying to rotate Zxing display after reading a few questions and posts about the issue. After following the instructions, the display did rotate, but the rectangle of the scanner is not positioned as it should (as can be seen on the image attached).

This is what I have done:

  1. in CameraConfigurationManager:

    camera.setDisplayOrientation(90);
    
  2. 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;
    
  3. 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

Rohit
  • 3,401
  • 4
  • 33
  • 60
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • 2
    possible duplicate of [How to use Zxing in portrait mode?](http://stackoverflow.com/questions/8007194/how-to-use-zxing-in-portrait-mode) – Sean Owen Apr 18 '12 at 20:21

9 Answers9

30

After a lot of struggling, I found the problem, and I hope it will help someone in the future.

On initFromCameraParameters method in CameraConfigurationManager there is an assumption that the scan is ALWAYS in landscape mode, and therefor a fix when width < height. If You follow the steps in the question and remove this check, it works fine.

duggu
  • 37,851
  • 12
  • 116
  • 113
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • 1
    Hi, I have implemented steps 1 and 3 and now I can view in portrait. I cannot find a place to implement 2 and this is preventing me from reading the code in portrait. Where should I put the code from step 2? I cant find a reference to rotatedData anywhere. Thanks. – jim Sep 27 '12 at 21:40
  • Which version of zxing are you using? – SuperFrog Sep 27 '12 at 22:27
  • I'm using v2.0. I actually found the location for the code in this link. http://code.google.com/p/zxing/issues/detail?id=178 – jim Sep 30 '12 at 14:12
  • @conor : Can u please answer to your question – Code_Life Dec 15 '12 at 06:50
  • @MohitSharma I notice you tried to edit question original question. I'd suggest you just leave comment regarding your proposed edit, instead. You can edit for clarity (e.g. clean up indentation/formatting if question is hard to read/understand), but I would not suggest changing the code included in a question. – Rob Dec 15 '12 at 07:08
  • 3
    @ROb : Okies....... Friends .. second point has been added in decode method with byte[] rotatedData .... – Code_Life Dec 15 '12 at 07:34
  • im using the zxing 2.1 version, i'm exactly have the same problem i tried this http://code.google.com/p/zxing/issues/detail?id=178#c46, now it's look same as attached image in this question, where i have to change as you suggest in zxing 2.1, please give some help? – Sam Feb 01 '13 at 00:19
  • 1
    @Sam - You need to disable the check in CameraConfigurationManager Class which is in com.google.zxing.client.android.camera. Search for "We're landscape-only" and disable this check. – SuperFrog Feb 01 '13 at 12:19
  • now view finder showing in potrait mode, but it's not decoding any qr code, i'm checking on Samsung GALAXY S III, any thing i missed out? using zxing 2.1 – Sam Feb 01 '13 at 13:59
  • Prior to disabling the check in CameraConfigurationManager, did the scan work? – SuperFrog Feb 01 '13 at 14:06
  • Anyone used that solution in Delphi Xe5 for mobile? – Sebastian Xawery Wiśniowiecki Mar 25 '14 at 14:19
17

As of zxing library:2.2.0 support for orientation change is inherent

Add/edit the following in manifest:

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

Set additional property at call to scanner :

IntentIntegrator integrator = new IntentIntegrator(this);

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

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

Anup
  • 4,024
  • 1
  • 18
  • 27
14

Thank you for your answer!! it really helped me, one thing that I noticed is that at least on zxing 2.1 you need to pass "rotatedData" to buildLuminanceSource instead of just "data", the line end up like this:

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

Hopefully this helps someone else!

Roberto
  • 446
  • 5
  • 11
  • Alright on top above the answer I have added yours too :) thanks it works! – Roy Lee Apr 25 '13 at 12:11
  • I feel so dumb for not changing this. Thanks, that's a very good addition to the information given in the question and accepted answer! – chuky Jul 14 '14 at 14:27
8

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

In setDesiredCameraParameters method of class CameraConfigurationManager added

camera.setDisplayOrientation(90);

.. in my original project's AndroidManifest.xml file. I set screenOrientation = portrait and Its working fine 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> 
duggu
  • 37,851
  • 12
  • 116
  • 113
Yousuf Qureshi
  • 498
  • 2
  • 7
  • 21
5
  1. In CameraConfigurationManager:

    camera.setDisplayOrientation(90);
    
  2. 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;
    
  3. 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;
    
  4. In CameraConfigurationManager:

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Dayanand Waghmare
  • 2,979
  • 1
  • 22
  • 27
  • This has worked for me. It would be nice to get an edit from @Dayanand indicating the methods to make these changes in. The only additional change I needed to make was to comment out the explicit `setRequestedOrientation` at `CaptureActivity#onResume` – MikeNereson Mar 06 '15 at 17:01
  • is that editing the source code of zxing library? – gumuruh Dec 19 '20 at 03:29
4

I'm a developer of Barcode Scanner. Yes, it takes a lot more than this to make it scan in portrait mode. You have to "rotate" the image data, and account for the orientation of the device, its default orientation, and its sensor's orientation.

Barcode Scanner+ scans in portrait mode, and you can integrate with it via Intent in exactly the same way that you integrate with Barcode Scanner. (However it's a for-pay app.)

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • It does rotate and scan, the only problem is the location of the rectangle. – SuperFrog Apr 18 '12 at 20:34
  • I think you flipped your x and y in the code above. That's how the display is acting at least. x should go with x. – Sean Owen Apr 18 '12 at 20:49
  • The flip is intentional as I have seen on an example here: http://code.google.com/p/zxing/issues/detail?id=178#c46 – SuperFrog Apr 18 '12 at 21:29
  • Something is definitely flipped, as you can see from your image. It's acting like it's in landscape. Are you sure `cameraResolution` and `screenResolution` are what you think? – Sean Owen Apr 19 '12 at 00:18
1

I've tried various patches, suggested in other answers, but barcode recognition remained unreliable.

I highly recommend using repository below in portrait mode. Try it, it's fast and stable. I used it in my hybrid app.

https://github.com/Dbuggerx/BarcodeScanner

user1643723
  • 4,109
  • 1
  • 24
  • 48
Loi Nguyen
  • 11
  • 2
1

try this : add android:screenOrientation="sensorPortrait"

<activity android:name=".CaptureActivity"
              android:screenOrientation="sensorPortrait"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:theme="@style/CaptureTheme"
              android:windowSoftInputMode="stateAlwaysHidden"
rizujikeda
  • 47
  • 6
0

In your library go to manifest file change the below line under activity tag

android:screenOrientation="portrait"
Syed Danish Haider
  • 1,334
  • 11
  • 15