Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change QR Scanner orientation with ZXING in Android Studio

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!

like image 593
Matías Gaete Avatar asked Jan 25 '16 00:01

Matías Gaete


3 Answers

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

like image 192
Ravi Tripathi Avatar answered Nov 10 '22 07:11

Ravi Tripathi


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>
like image 22
NOT_A_PROGRAMMER Avatar answered Nov 10 '22 08:11

NOT_A_PROGRAMMER


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);
like image 1
Lokesh Bajracharya Avatar answered Nov 10 '22 09:11

Lokesh Bajracharya