12

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!

Matías Gaete
  • 133
  • 1
  • 1
  • 7

4 Answers4

54

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

Ravi Tripathi
  • 778
  • 1
  • 5
  • 12
22

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>
NOT_A_PROGRAMMER
  • 1,794
  • 2
  • 20
  • 31
  • how you are extending captureactivity? i am getting error as can't inherit from final CaptureActivity – Shadow Mar 16 '16 at 09:53
  • @Shadow I just checked my answer, I forgot zxing:core3.2.0 check the build.glade make sure you have this two libraries added { 'com.journeyapps:zxing-android-embedded:3.1.0@aar' and 'com.google.zxing:core:3.2.0' } – NOT_A_PROGRAMMER Mar 16 '16 at 23:19
  • @Shadow this is that class that extend from CaptureActivity https://gist.github.com/KaLingCode/21a557c9a96333ec037c – NOT_A_PROGRAMMER Mar 16 '16 at 23:28
  • this is also worked for version 3.2.0 'com.journeyapps:zxing-android-embedded:3.2.0@aar' @NOT_A_PROGRAMMER – saulyasar Jan 03 '17 at 08:48
  • I had android:screenOrientation="fullSensor" which meant my activity would switch orientation to landscape (which wasn't desirable for QR code reading). Setting it to portrait fixed it. – Nick Wright Apr 19 '17 at 11:14
  • I tested and work in ``com.journeyapps:zxing-android-embedded:3.6.0``. Just following step by step – Aldan Aug 16 '18 at 02:53
  • Instead of this android:screenOrientation="portrait" use android:name="com.journeyapps.barcodescanner.CaptureActivity" android:screenOrientation="portrait" tools:replace="screenOrientation" setting this configuration saved my day . :) – Rajesh Prasad Yadav Oct 22 '18 at 19:48
1

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);
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Lokesh Bajracharya
  • 457
  • 2
  • 8
  • 19
0

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();
Laerte
  • 7,013
  • 3
  • 32
  • 50
Muthu
  • 11
  • 4