Search code examples
androidcameraorientationqr-codezxing

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!


Solution

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