32

Currently zxing library supports only on landscape mode.For my app i need to use in portrait mode

Kiran Babu
  • 1,893
  • 2
  • 13
  • 7
  • 2
    Here's what you can do with zxing 2.1. http://stackoverflow.com/questions/16252791/how-to-show-zxing-camera-in-portrait-mode-on-android/16252917#16252917 – Roy Lee Apr 27 '13 at 16:37
  • 1
    As of Zing 2.2.0 there is property you can set for orientation, refer this [answer](https://stackoverflow.com/a/44284764/2584794) – Anup May 31 '17 at 12:27

10 Answers10

39

Here is the Solution for Portrait mode Scanning

first declare these two lines in your app level gradle file

implementation 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
implementation 'com.google.zxing:core:3.2.0'

Define a button in your xml file and in Onclick Listener of button write below code in MainActivity java file

IntentIntegrator integrator = new IntentIntegrator(this);
    integrator.setPrompt("Scan a barcode");
    integrator.setCameraId(0);  // Use a specific camera of the device
    integrator.setOrientationLocked(true);
    integrator.setBeepEnabled(true);
    integrator.setCaptureActivity(CaptureActivityPortrait.class);
    integrator.initiateScan();

Write below code in your MainActivity java file after onCreate() method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
    if(result.getContents() == null) {
        Log.d("MainActivity", "Cancelled scan");
        Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    } else {
        Log.d("MainActivity", "Scanned");
        st_scanned_result = result.getContents();
        Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();

    }
  }

}

then create a class with name CaptureActivityPortrait that extends CaptureActivity. The class looks like below

  package soAndSo(Your PackageName);

  import com.journeyapps.barcodescanner.CaptureActivity;

  public class CaptureActivityPortrait extends CaptureActivity {
  }

And Most Important declare your CaptureActivityPortrait in manifest file like below

<activity android:name=".CaptureActivityPortrait"
        android:screenOrientation="sensorPortrait"
        android:stateNotNeeded="true"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden"></activity>
Tara
  • 2,598
  • 1
  • 21
  • 30
21

I wanted to use the Barcode reader in portrait mode. I found the solution here as mentioned in a comment posted earlier in this thread. I thought of putting this as an answer so it is easier to find the solution for the people having the same problem.

To use the scanner in portrait mode, you need to add the following activity in your AndroidManifest.xml.

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

And that's it.

See the link for more details.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
18

Just check out issue for Use Zxing in portrait mode.

dodgy_coder
  • 12,407
  • 10
  • 54
  • 67
Uttam
  • 12,361
  • 3
  • 33
  • 30
  • 3
    This link is obsolete. – Dusan Kovacevic Apr 22 '15 at 12:35
  • 2
    The link leads to "QR Code doesn't support unicode" issue... Check this https://github.com/journeyapps/zxing-android-embedded/issues/16 instead – kristyna Nov 05 '15 at 15:04
  • 2
    If you are using version 3.x.x, declare orientation in AndroidManifest.xml. For more read https://github.com/journeyapps/zxing-android-embedded/blob/master/README.md#changing-the-orientation – kristyna Nov 05 '15 at 15:29
7

Please check as following (Official Documentation)

Add this activity to manifest file

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

Set integrator OrientationLocked to false

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();

Hope this helps

6

setDisplayOrientation(int) does not affect the order of byte array passed in PreviewCallback.onPreviewFrame. (Refer to JavaDoc for additional info)

It means that you need to rotate the data return from previewCallback, but this is yuv data, you need to convert to rgb data then rotate them. it is possible to scan barcode in portrait mode but it will take longer because it need more time to process data from yuv to rgb and rotate rgb data. so the matter here is how can we get yuv data from previewcallback for portrait mode? when we set setPreviewSize for camera parameters it will get exception if previewsize is not supported. Here is the matter of this issue. If the camera driver does not support previewSize for portrait mode (height > width) you cannot get yuv data in portrait mode. And the rest depends on you, you can scan barcode in portrait mode but it take time longer or you have to change orient of screen to landscape to get result faster.

duggu
  • 37,851
  • 12
  • 116
  • 113
MichaelP
  • 2,761
  • 5
  • 31
  • 36
4
  1. In order to make screen work in portrait, set portrait orientation for the activity (e.g. in manifest) and then config the camera: Use camera.setDisplayOrientation(90) in CameraConfigurationManager.setDesiredCameraParameters(Camera camera). But be aware that:

    • setDisplayOrientation(int) requires Android 2.2
    • setDisplayOrientation(int) does not affect the order of byte array passed in PreviewCallback.onPreviewFrame. (Refer to JavaDoc for additional info)
  2. Because preview frames are always in "landscape", we need to rotate them. I used clockwise rotation offered by comment #11. Do not forget to swap width and height parameters after rotation. DecodeHandler.java, rotate data before buildLuminanceSource in decode(byte[] data, int width, int height)

    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;
    
  3. I also modified CameraManager.java, getFramingRectInPreview(), as recommended by #c11:

    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. I did not modify getCameraResolution(). That's the second difference to #c11.

As a result, I've got UPC and other 1D codes scanning work in portrait.

P.S. Also you may adjust the size of FramingRect (that's the rectangle visible on the screen during scanning), and FramingRectInPreview will be adjusted automatically.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Thomas
  • 41
  • 1
4

Just add these code in the AndroidManifest.xml of your project

<activity android:name="com.journeyapps.barcodescanner.CaptureActivity"
        android:screenOrientation="sensorPortrait"
        android:stateNotNeeded="true"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden"
        tools:replace="android:screenOrientation" />
fklappan
  • 3,259
  • 2
  • 17
  • 18
Mei Lie
  • 91
  • 1
  • 11
1

Use this android library https://github.com/SudarAbisheck/ZXing-Orient

It supports both portrait and landscape orientation.

  • 2
    Please check out [Take a tour](http://stackoverflow.com/tour) and [Your answer is in another castle: When is an answer not an answer](http://meta.stackexchange.com/questions/225370) – Drew Mar 01 '16 at 05:16
  • 1
    @Drew in my view it is decent answer ... the OP wants a version of ZXing that supports portrait more, and this answer delivers exactly that. It helped me with the same requirement. – dodgy_coder May 30 '16 at 03:33
  • I am sure it did. Years ago, resource requests were fine on this site. The guidelines changed. And when new answers come in with link-only answers, you often get the comments that I gave you. – Drew May 30 '16 at 04:02
0

add this android:screenOrientation="sensorPortrait" to your manifest

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

Add this code to AndroidManifest.xml file inside application tag this will definitely work.

<activity
        android:name="com.journeyapps.barcodescanner.CaptureActivity"
        android:screenOrientation="fullSensor"
        tools:replace="screenOrientation" />
  • Adding this might cause manifest merge problem. Can you provide a solution that does not cause the manifest merge issue – Prasanna Anbazhagan Mar 02 '23 at 14:10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 02 '23 at 14:11