Barcode Xpress for Java v13.9 - Updated October 4, 2022
Create Your First Project
Getting Started > Create Your First Project

Introduction

Reading your first barcode with Barcode Xpress for Java takes only a few minutes. There are two easy ways to get started.

Use our GitHub sample

We have an easy to use sample for getting started on GitHub. Just clone the repo and get started in no time.

Create your own project using Maven

Creating a new project with Maven is simple. We have to run one command to create the project, and then copy and paste a few things to add Barcode Xpress to our app.

  1. Run the command below to create your project:

    mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
    
    
  2. You can copy and paste the full POM below over your existing POM file, or you can take these parts we added and paste them into your POM file (please use actual version numbers instead of MAJOR.MINOR):

  1. We need to edit our App.java. Again, you can copy and paste the whole file below over your existing App.java, or you can take what you need

    package com.mycompany.app;
    
    import com.accusoft.barcodexpress.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    public class App 
    {
     public static void main( String[] args )
     {
         BarcodeXpress barcodeXpress = new BarcodeXpress();
         BarcodeReader barcodeReader = barcodeXpress.getReader();
    
         BarcodeType barcodeType = BarcodeType.CODE39;
         barcodeReader.setBarcodeTypes(new BarcodeType[] { barcodeType });
    
         try {
             File inputFile = new File("test-barcodes.bmp");
             BufferedImage bufferedImage = ImageIO.read(inputFile);
    
                    Result[] results = barcodeReader.analyze(bufferedImage);
    
             for (int i = 0; i < results.length; i++) {
                 System.out.println(results[i].getValue() + "'\n");
             }
         }
         catch (BarcodeException bEx) {
             System.out.println(bEx.getMessage());
         }
         catch (Exception ex) {
             System.out.println(ex.getMessage());
         }
     }
    }
    
    
  2. Download our test image and place it in the top level of your app's directory. https://github.com/Accusoft/hello-barcode-xpress-for-java/blob/master/test-barcodes.bmp

  3. Run the app!

    To build and run the app, use this command:

    mvn package exec:java
    
    

    If you just want to build you app, just run this command:

    mvn package
    
    

    To run your app, use this command:

    mvn exec:java
    
    

Now that our app is complete, we'll go over some of the code in more detail

Barcode Xpress recognizes more than 30 different types of barcodes. In order to save processing time, it is best to only search for the types of barcodes you want to find. Here we setup the barcode reader to read only CODE39 barcodes. If you want to search for other types, see the supported barcode types and use the type that suits your requirements.

BarcodeType barcodeType = BarcodeType.CODE39;
barcodeReader.setBarcodeTypes(new BarcodeType[] { barcodeType});

Barcode Xpress throws a BarcodeException if it encounters an exception specific to Barcode Xpress. Before calling the analyze method, use a try-catch block (the analyze method does throw exceptions):

try {
…
}
catch (BarcodeException bEx) {
   System.out.println(bEx.getMessage());
}

The most important method for Barcode Xpress is analyze. Here we call analyze and print the resulting information:

```
Result[] results = barcodeReader.analyze(bufferedImage);
System.out.println(results.length + " barcodes found. \n");   
for (int i = 0; i < results.length; i++) {
    System.out.println("#" + (i+1));
    System.out.println("Barcode value = '" + results[i].getValue() + "'");
    System.out.println("Barcode type = '" + results[i].getType() + "'\n");
}
```

Is this page helpful?
Yes No
Thanks for your feedback.