Change the orientation of a QR scanner using ZXING in Android Studio

I hope you can help me with this. I use the built-in Zxing library to use a QR scanner, the problem is that it is in landscape mode, and I would like to change it to a portrait.

I have it 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!

+5
source share
4 answers

I use

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

This is a different version, so I donโ€™t know if this will work for you, but it works for me.

,

'com.journeyapps: ZXing- : 3.1.0@aar'

'com.google.zxing: : 3.0.1

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

, CaptureActivity

, https://gist.github.com/TheGratefulDev/21a557c9a96333ec037c

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

-,

integrator.setCaptureActivity(CaptureActivityPortait.class);

.

:

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();

, AndroidMaifest

   <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>
+15

:

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

+17

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

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);
+1
source

It works for me:

IntentIntegrator integrator = new IntentIntegrator(YourActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt(getResources().getString(R.string.scan_a_barcode));
integrator.setCameraId(0);
// Use a specific camera of the device
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
0
source

Source: https://habr.com/ru/post/1625759/


All Articles