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.
-
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
-
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):
-
We added two properties:
<bx.jar.version>MAJOR.MINOR</bx.jar.version> <bx.repository.url>https://mvn.accusoft.com</bx.repository.url>
-
Next, we added these two dependencies:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.accusoft.barcodexpress</groupId> <artifactId>barcodexpressjava</artifactId> <version>${bx.jar.version}</version> </dependency>
-
Then, we added the Accusoft Maven repository:
<repositories> <repository> <id>accusoft-mvn-pub</id> <name>Accusoft Public Maven Releases</name> <url>${bx.repository.url}</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
-
Finally, we added the Exec Maven plugin and configured it to run our main class. This allows to run build and run our program with single command:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.mycompany.app.App</mainClass> </configuration> </plugin>
Here's the entire POM file:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <bx.jar.version>MAJOR.MINOR</bx.jar.version> <bx.repository.url>https://mvn.accusoft.com</bx.repository.url> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.accusoft.barcodexpress</groupId> <artifactId>barcodexpressjava</artifactId> <version>${bx.jar.version}</version> </dependency> </dependencies> <repositories> <repository> <id>accusoft-mvn-pub</id> <name>Accusoft Public Maven Releases</name> <url>${bx.repository.url}</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.mycompany.app.App</mainClass> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
-
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()); } } }
-
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
-
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");
}
```