Zxing Scanner Android — Implementing in Activities & Fragment| Custom colors

dev.jeevanyohan
2 min readJun 11, 2020

--

Photo by Christina Morillo from Pexels

Zxing is one of the most popular libraries used for barcode scanning and QR code scanning in android. So not much intro is required I guess. First things first. In build.gradle file

implementation 'com.journeyapps:zxing-android-embedded:<version>'

Check the latest version here : https://github.com/journeyapps/zxing-android-embedded/releases

In this example we will be scanning QR codes. For scanning QR code we need the camera permisison. In manifest.xml

<uses-permission android:name="android.permission.CAMERA" />

The easiest way to implement QR code scanning is to use IntentIntegrator. In MainActivity.java

IntentIntegrator integrator = new IntentIntegrator(this);


integrator.setOrientationLocked(false);
integrator.setPrompt("Scan QR code");
integrator.setBeepEnabled(false);//Use this to set whether you need a beep sound when the QR code is scanned

integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);


integrator.initiateScan();

This is the case when you use IntentIntegrator in an activity. If you are using it in a fragment use this code: In FragmentQRScan.java

IntentIntegrator integrator = IntentIntegrator.forSupportFragment(FragmentQRScan.this);

integrator.setOrientationLocked(false);
integrator.setPrompt("Scan QR code");
integrator.setBeepEnabled(false);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);


integrator.initiateScan();

It is important to use IntentIntegrator.forSupportFragment() instead of IntentIntegrator(getActivity()) because if you use the latter, the results will be available only in the activity page.

Getting results

Now let’s fetch the data. In MainActivity.java or FragmentQRScan.java

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
} else {

Toast.makeText(getContext(), "Scanned : " + result.getContents(), Toast.LENGTH_LONG).show();



}
}
}

If you are using Fragment, then in the parent activity page use this, so that the activity page passes the result to the fragment.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

Customising the scanner

Using the IntentIntegrator there are limited customisations. However you can use this library with a custom layout which will be discussed in another article. Now let’s do some customisations with IntentIntegrator.

Changing the laser color:

In your colors.xml page

<color name="zxing_viewfinder_laser">your-color-here</color>

Changing possible result points color :

<color name="zxing_possible_result_points">your-color-here</color>

Other colors :


<color name="zxing_result_view">#b0000000</color>
<color name="zxing_status_text">#ffffffff</color>
<color name="zxing_transparent">#00000000</color>
<color name="zxing_viewfinder_mask">#60000000</color>

Setting the orientation of scanning page

In manifest.xml add the following activity within the<application> tag and set your desired orientation

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

--

--