This tutorial shows how to write barcodes in a Java Android application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to write barcodes in an Java Android application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (104 KB) |
Platform | Java (Android) |
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 Write 1D and 2D Barcodes to an Image - Java Android tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.
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
leadtools.barcode.jar
The .SO
files can be found at: <INSTALL_DIR>\Bin\Android
libleadtools.barcode.so
libleadtools.barcode.lead2dwrite.so
libleadtools.barcode.oned.so
libleadtools.barcode.pdfwrite.so
libleadtools.barcode.qrwrite.so
libleadtools.barcode.datamatrixwrite.so
libleadtools.imageprocessing.core.so
libleadtools.imageprocessing.utilities.so
libleadtools.drawing.so
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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 Write 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/darker_gray">
<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="Write barcodes"
android:onClick="writeBarcodes"/>
</LinearLayout>
In MainActivity.java
, add the following import statements before the MainActivity
class:
import leadtools.barcode.BarcodeAlignment;
import leadtools.barcode.BarcodeData;
import leadtools.barcode.BarcodeEngine;
import leadtools.barcode.BarcodeOutputTextPosition;
import leadtools.barcode.BarcodeSymbology;
import leadtools.barcode.OneDBarcodeWriteOptions;
import leadtools.barcode.QRBarcodeData;
import leadtools.barcode.QRBarcodeECCLevel;
import leadtools.barcode.QRBarcodeSymbolModel;
import leadtools.barcode.QRBarcodeWriteOptions;
Add the following member variables to the MainActivity
class:
private BarcodeEngine engine;
private BarcodeData data;
private QRBarcodeData qrData;
private QRBarcodeWriteOptions qrOptions;
Update the onCreate()
function as shown below:
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();
data = new BarcodeData();
qrData = new QRBarcodeData();
qrOptions = new QRBarcodeWriteOptions();
}
Create a writeBarcodes()
function to write a UPCA barcode and QR barcode to the given image.
public void writeBarcodes(View v )
{
RasterImage image = mViewer.getImage();
if(image != null)
{
if(count % 2 == 0) {
// Write 1D barcode
data.setSymbology(BarcodeSymbology.UPC_A);
data.setValue("01234567890");
data.setBounds(new LeadRect(10, 10, 600, 200));
OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions();
options.setEnableErrorCheck(true);
options.setTextPosition(BarcodeOutputTextPosition.DEFAULT);
try
{
engine.getWriter().writeBarcode(image, data, options);
}
catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
else
{
//Write 2D barcode
qrData.setSymbolModel(QRBarcodeSymbolModel.MODEL1_AUTOSIZE);
qrData.setSymbology(BarcodeSymbology.QR);
qrData.setValue("QR Data");
qrData.setBounds(new LeadRect(10,300,600,200));
qrOptions.setGroupNumber(0);
qrOptions.setGroupTotal(0);
qrOptions.setXModule(30);
qrOptions.setECCLevel(QRBarcodeECCLevel.LEVEL_L);
qrOptions.setHorizontalAlignment(BarcodeAlignment.NEAR);
qrOptions.setVerticalAlignment(BarcodeAlignment.NEAR);
try
{
engine.getWriter().writeBarcode(image, qrData, qrOptions);
File file = new File(this.getFilesDir(), "output.png");
String path = file.getAbsolutePath();
codecs.save(image, LeadStreamFactory.create(path), RasterImageFormat.PNG, 0);
Toast.makeText(this, path, Toast.LENGTH_LONG).show();
}
catch(Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
count++;
}
}
Press Shift + F10 to run the application. Follow the steps below to test the application.
SELECT IMAGE FROM GALLERY
to load in the image to write the barcode to.Select the WRITE BARCODES
button to write the two barcodes to the image and save it.
This tutorial shows how to write 1D and 2D barcodes to an image.