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 (4 MB) |
Platform | Android (Java) |
IDE | Android Studio |
Runtime License | Download LEADTOOLS |
Try it in another language |
|
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.
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>\LEADTOOLS21\Bin\Java
leadtools.barcode.jar
leadtools.imageprocessing.core.jar
The .SO
files can be found at: <INSTALL_DIR>\LEADTOOLS21\Bin\Android
libleadtools.barcode.so
libleadtools.barcode.lead2dread.so
libleadtools.barcode.oned.so
libleadtools.barcode.pdfread.so
libleadtools.barcode.qrread.so
libleadtools.barcode.datamatrixread.so
libleadtools.imageprocessing.core.so
libleadtools.imageprocessing.utilities.so
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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.
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>
In MainActivity.java
, add the following import statements before the MainActivity class:
import leadtools.LeadRect;
import leadtools.barcode.BarcodeData;
import leadtools.barcode.BarcodeEngine;
Add the following member variables to the MainActivity class:
private BarcodeEngine engine;
private StringBuilder sb;
Update the onCreate()
function as shown below:
@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:
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();
}
}
Press Shift + F10 to run the application. Follow the steps below to test the application.
SELECT IMAGE FROM GALLERY
to load in an image.Select the READ BARCODES
button to read the barcodes.
This tutorial showed how to run barcode recognition on an image. Also, it covered how to use the BarcodeEngine
, BarcodeReader
, and BarcodeData
classes.