Detect and Extract Barcodes - Android Java

This tutorial shows how to detect and read barcodes using the LEADTOOLS SDK in an Android Java application.

Overview  
Summary This tutorial covers how to extract barcodes in an Android application.
Completion Time 30 minutes
Android Studio Project Download tutorial project (10 MB)
Platform Android (Java)
IDE Android Studio
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorials, before working on the Detect and Extract Barcodes - Android Java tutorial.

Saving images is not necessary to this tutorial, so the saving portion can be commented out.

Create the Project and Add LEADTOOLS References

In Android Studio, create a new Android (Java) project, and add the below necessary LEADTOOLS references.

The references needed depend upon the purpose of the project. This tutorial requires the following .JAR and .SO files:

The .JAR files can be found at: <INSTALL_DIR>\Bin\Java

The .SO files can be found at: <INSTALL_DIR>\Bin\Android

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Set the Application Layout

In the Project Explorer window, open the activity_main.xml file found in the app/src/main/res/layout directory. Below the RasterImageViewer XML code add a new Read barcodes button.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" 
    android:background="@android:color/holo_blue_dark"> 
 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Select Image From Gallery" 
        android:onClick="onSelectImage"/> 
 
    <leadtools.controls.RasterImageViewer 
        android:id="@+id/rasterimageviewer" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight=".96" 
        android:background="@android:color/white"/> 
 
    <Button 
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Read barcodes" 
        android:onClick="readBarcodes"/> 
</LinearLayout> 

Add the Extract Barcode Information Code

In MainActivity.java, add the following import statements before the MainActivity class:

Java
import leadtools.LeadRect; 
import leadtools.barcode.BarcodeData; 
import leadtools.barcode.BarcodeEngine; 

Add the following member variables to the MainActivity class:

Java
    private BarcodeEngine engine; 
    private StringBuilder sb; 

Update the onCreate() function as shown below:

Java
  @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        // Load LEADTOOLS native libraries 
        try{ 
            Platform.setLibPath(sharedLibsPath); 
            Platform.loadLibrary(LTLibrary.LEADTOOLS); 
            Platform.loadLibrary(LTLibrary.CODECS); 
            Platform.loadLibrary(LTLibrary.BARCODE); 
        } 
        catch(Exception ex) { 
            Log.d(TAG,"Failed to load LEADTOOLS Native libraries" ); 
        } 
 
        engine = new BarcodeEngine(); 
        sb = new StringBuilder(); 
    } 

Create a readBarcodes() function to allow reading of barcodes. Add the below code:

Java
    public void readBarcodes(View v ) 
    { 
        RasterImage image = mViewer.getImage(); 
        if(image != null) 
        { 
            BarcodeData[] barcodes = engine.getReader().readBarcodes(image, LeadRect.getEmpty(),0,null); 
 
            sb.append("Total Barcodes found: " + barcodes.length); 
            sb.append("\n"); 
 
            for(int i = 0; i < barcodes.length; i++) 
            { 
                BarcodeData data = barcodes[i]; 
                sb.append("Symbology: " + data.getSymbology().toString()+ ", Location: " + data.getBounds().toString() + ", Data: " + data.getValue()); 
                sb.append("\n"); 
            } 
            if(barcodes.length != 0) 
            { 
                Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show(); 
 
            } 
            else 
            { 
                Toast.makeText(this, "Did not recognize any Barcodes", Toast.LENGTH_LONG).show(); 
            } 
        } 
        else 
        { 
            Toast.makeText(this, "No Image is loaded", Toast.LENGTH_LONG).show(); 
        } 
    } 

Run the Project

Press Shift + F10 to run the application. Follow the steps below to test the application.

Wrap-up

This tutorial showed how to run barcode recognition on an image. Also, it covered how to use the BarcodeEngine, BarcodeReader, and BarcodeData classes.

See Also

Help Version 23.0.2024.3.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.