How To Read A Barcode From An Image In Java
Create a command line sample program for reading different types of barcodes, including damaged barcodes and low quality images.
- Barcode Xpress Java SDK (download free trial here)
- Oracle Java SE 5.0 and up
- Oracle Java JDK 1.8 or above
- Development IDE (in this example, Eclipse Java Oxygen is used)
Java Intermediate Level
Need to recognize barcodes from an image file in your Java application? In this project, we'll walk through how to do this with a powerful barcode library for Java, Barcode Xpress.
If you’re already an experienced Java developer and know how to setup your development environment, feel free to skip this section.
Start by downloading a trial version of the Accusoft Barcode Xpress Java SDK. Within your Accusoft Barcode Xpress Java SDK will be the file barsdk5.jar. Expand that either with your favorite unzip program or if your java environment is already set up, just run:
Along with some code samples, there will also be the file tasbar5.jar. This contains the libraries that are needed to run this sample.
Depending on your development environment, you’ll want to add this jar file to your Java Build Path. Since Eclipse is being used for this sample, right click on your Java project and select Properties, then “Java Build Path”, and the “Libraries” tab.
It’s useful to add in the jar files into a common library, so in this case we'll add a folder called “lib” into the development directory and place the tasbar5.jar file there so it can be included with whatever code repository system is being used. Then to reference this file, click “Add External JARs”, navigate to the lib directory, and select tasbar5.jar. Now the various Java objects we’ll need for our code can be accessed.
Here is the source code for the application we're demoing in this article:
Scanning the First Barcode
For our code to work, let's go over how to import an image file into Java, how to set the options for what kind of barcodes to scan for, and then how to scan the file itself.
Here's a sample bar code to be read.
As we can see – it's not perfect. Looks like the paper was a little off on one side, there's some pixelation in the scanning. Maybe the scanner was nudged during the process. sam Our SimpleBarCodeScanner program works via the command line. The first argument will be the type of barcode we're scanning – we'll pick Code39 as our default barcode to scan for. So a typical execution of this program will look like this (assuming that your tasbar5.jar is in the /lib directory, and you execute the program from your compiled /bin directory):
Let's run it, see what the results are, and then step through our program code to explain what's going on.
Looking At the Process
We'll skip over the items in the Main function – all we're really doing there is accepting the arguments, and as long as they're valid, we pass it onto the ScanBarCodeFile function via the following command:
This function takes an integer as the barcodeType. Barcode Xpress can handle several different types of barcodes (for a list of supported barcodes, please review the documentation at http://help.accusoft.com/BarcodeXpress/v12.0/dotnet/webframe.html#Supported_Barcode_Types.html ), but for this example we're just going to focus on one dimensional barcodes of the Code39, Code128, and CodeEAN13 standard.
A complete copy of the source code is available above – we'll just highlight some items to go through what it's going.
When we start, our function is going to read in the image file we've specified. The Java object BufferedImage can input JPEG, PNG, GIF, and BMP formats, so we'll stick with png files in our example.
Once the BufferedImage object has been loaded, let's load it into the Barcode Xpress object BarReader:
There are plenty of options to set in our BarReader object, and to do that, we'll load them up into a Readoptions object, then set it to read the barcodes from left to right:
Since we just want to scan in specific types of barcodes as we run the function, we can specify that by setting that in our options object:
In this case, we select 1 as our barcode type, so it defaulted to the Code39 standard. To actually scan the image based on the options we've set up, just set our Barcode Xpress object to read them from the BufferedImage object, and store the results in an array of Barcode objects. This way if there's more than one barcode in a document, we'll find them all:
And here's our value displayed:
Only Scanning A Specific Type Of Barcode
That works just fine for a simple demonstration – one barcode inside of one file. Let's increase the complexity a bit. What if we have an image with different kinds of barcodes within it, but we only care about one of them?
Not a problem – Barcode Xpress detects the barcode we're interested, and ignores the other.
Scanning Multiple & Misaligned Barcodes
Because of the way our code is situated, we can scan multiple barcodes in one shot, like in this image:
Barcode Xpress is capable of scanning 1,000 documents a minute – so one image file with a few barcodes in it isn't a problem.
Also notice that the orientation of the image isn't an issue – some of them are at an angle, so we don't have to worry about whether the image is perfectly vertical or that everything is lined up. Just scan, get the data, and move on.
Switching Types Of Barcodes
We can also shift between different kinds of barcodes on the fly. In an image that has both EAN-13 and Code39 standards, we can flip between scanning for one and the other by changing the command line switch:
We'll start with Code39 by setting the first command line argument to "1", then flip right back into EAN-13 by setting the command line argument to "3"
Rather than worrying that you'll get more information than you need – you can pick and choose for your needs.
Integrating Into Your Program
If you're looking to implement this code in your project, the following function will provide the data you require. GetScanBarCodeFile works the same as our ScanBarCodeFile above – only instead of displaying the barcode values on the console, it returns an array of BarCode objects so you can extract the data yourself. The function can be called by:
Here's a copy of the GetScanBarCodeFile function:
Ready to try it out? Check out Accusoft Barcode Xpress for more details or to download an evaluation copy.