Change QR Scanner orientation with ZXING in Android Studio

27,380

Solution 1

I am using

compile 'com.journeyapps:zxing-android-embedded:3.1.0@aar'

It is different version, so I don't know if this will work for you, but this is working for me.

More about my setup, I only compile

'com.journeyapps:zxing-android-embedded:3.1.0@aar'

'com.google.zxing:core:3.0.1'

and I did not compile

'com.journeyapps:zxing-android-integration:2.0.1@aar'

First I created an activity extend from the CaptureActivity

or click this link to view the class https://gist.github.com/TheGratefulDev/21a557c9a96333ec037c

public class CaptureActivityPortrait extends CaptureActivity {
//Nothing in side.
}

Second, add this

integrator.setCaptureActivity(CaptureActivityPortait.class);

into your integrator code.

This is how mine looks like:

CustomIntegrator integrator = new CustomIntegrator(activity);
            integrator.setDesiredBarcodeFormats(CustomIntegrator.PDF_417);
            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();

Finally, at the AndroidMaifest add

   <activity
        android:name=".custom.CaptureActivityPortrait"
        android:screenOrientation="portrait" <---this is the most important line
        android:stateNotNeeded="true"
        android:theme="@style/zxing_CaptureTheme"
        android:windowSoftInputMode="stateAlwaysHidden">
    </activity>

Solution 2

Instead of extending a class, just add this to the manifest:

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

Works like a charm

Solution 3

I just found the easiest way. We should create another CaptureActivity.java class and write this code inside onclick listener:

IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setPrompt("Scan a barcode");
integrator.setDesiredBarcodeFormats(integrator.ALL_CODE_TYPES);
integrator.setCameraId(0);  
integrator.setOrientationLocked(false);

// Replace with your own java class location here
integrator.setCaptureActivity(com.share.ants.hotelmenu.CaptureActivity.class);
integrator.setBeepEnabled(true);
Share:
27,380
Matías Gaete
Author by

Matías Gaete

Updated on July 09, 2022

Comments

  • Matías Gaete
    Matías Gaete almost 2 years

    I hope you can help me with this. Im using the Zxing Embedded Library in order to use the QR scanner, the problem is that is on Landscape mode and I would like to change it to Portrait.

    I have this on the dependencies of my Graddle

    compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
    compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
    compile 'com.google.zxing:core:3.0.1'
    

    and I have this in my java class to activate the scanner with a button...

    public void scanQR(View view){
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setResultDisplayDuration(0);//Text..
        integrator.setPrompt(" Scan a QR Code");
        integrator.setScanningRectangle(450, 450);//size
        integrator.setCameraId(0);  // Use a specific camera of the device
        integrator.initiateScan();
    
    }
    

    Thanks for the help!