Skip to content

FX205

Step 1: Import the zxing library

This library will allow you scan different formats (qrcode, barcode). The list of supported formats can be found at this GitHub link.

To import a library into your project, add it to the Dependencies of the project.
To do this, add the following to your build.gradle file:

repositories {
    jcenter()
}

dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:4.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
}

android {
    buildToolsVersion '28.0.3' // Older versions may give compile errors
}

Step 2: Add the necessary permissions

For the application to scan codes, the camera must be enabled.

Open the manifest file and add the following:

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

Step 3: Hardware acceleration

Hardware acceleration is required since TextureView is used.

Make sure it is enabled in your manifest file:

<application android:hardwareAccelerated="true" ... >

Step 4: Create the view

Here we will add a simple button to launch the scan and two text fields to display the results (format detected and content).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button android:id="@+id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Scan" />

    <TextView
        android:id="@+id/result_format"
        android:layout_below="@id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/result_content"
        android:layout_below="@id/result_format"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
         />
</RelativeLayout>

Step 5: Put into action

This last step links the previous ones together

  • Inject the view elements into the code
 @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scanButton = (Button) findViewById(R.id.scan_button);
        result_format = (TextView) findViewById(R.id.result_format);
        result_content = (TextView) findViewById(R.id.result_content);
    }
  • Define the button click action
scanButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new IntentIntegrator(MainActivity.this).initiateScan();
    }
});

If you use from a Fragment :

IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment

// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.
  • The result of this action is retrieved and displayed in the two dedicated text fields
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        result_format.setText("FORMAT: " + scanningResult.getFormatName());
        result_content.setText("CONTENT: " + scanningResult.getContents());
    }
    else{
        Toast.makeText(getApplicationContext(), "No data received", Toast.LENGTH_SHORT).show();
    }

}

Customize options :

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0);  // Use a specific camera of the device
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();