Java Barcode Reader Developer Guide

How to scan, read linear, 2d barcode images in Java web application

How to Scan & Read Linear & 2D BarCodes in Java applications using Java Barcode Reader Library



Java Barcode Reader is a Java barcode scanning library which scan and read 1D (linear) and 2D (matrix) barcodes from image files. Besides Java Barcode Reader library, OnBarcode also provides Java Barcode Generator for generating linear and 2D barcodes in the Java program.

This article provides detailed developer guide for Java Barcode Reader library. To use this article, you need first download the trial version of Java Barcode Reader from our site.


How to scan, read barcodes from image using Java?

  1. Download Java Barcode Reader Library
  2. Install Java jar SDK to read barcodes from images in Java web and desktop applications.
  3. Step by Step Tutorial








 

Java Barcode Reader Install & Requirement

Top

Requirement

You need Java JDK 5.0 or later version.


Install

Put OnBarcode.BarcodeReader.jar into your Java project reference.

To read Tiff and bitmap image, you need two extra jars from Sun Microsystem, see how to read barcodes from tiff and bitmap images.


Install Licensed Jar

  1. Remove the trial version jar from your Java project. You need clean your project cached content also.
  2. Add the downloaded licensed jar file OnBarcode.BarcodeReader.jar to your project reference.




Compare Barcode library Trail and Licensed jar

Top
The free trial jar includes exactly the same features as the licensed jar.

There is only one limit in the trial version of barcode jar. The first character or digit from returned barcode data is randomly generated. In the licensed sdk, we have removed the limit.




Barcode Reader Library all Supported Barcode Formats

Top
The OnBarcode Java Barcode Reader library supports decoding the following barcode types:

public enum OnBarcode.Barcode.BarcodeScanner.BarcodeType
{
    AustraliaPost,        EAN8,                 ITF14,          RM4SCC,    
    Codabar,              EAN13,                Leitcode,       UPCA,   
    Code39,               Identcode,            PatchCode,      UPCE,    
    Code39Extension,      IntelligentMail,      PDF417,         All
    Code93,               Interleaved2of5,      Planet,                   
    Code128,              ISBN,                 Postnet,
    DataMatrix,           ISSN,                 QRCode,                               
}




How to read barcodes using Java Barcode Reader?

Top
Read barcodes from image is a simple task with barcode reader for java library. Use method BarcodeScanner.Scan() to quickly scan and decode the barcodes from the image file in Java application.

Here is the sample Java code to decode a Code 128 barcode data from image file.

String[] datas = BarcodeScanner.Scan("code128-barcode.png", BarcodeType.Code128);


Pass your barcode image file, and barcode type to BarcodeReader, and it will return barcode datas.



Scan barcodes from Java image object

You can also scan barcode data from Java image object RenderedImage in memory.

java.awt.image.RenderedImage renderedImage = ImageIO.read(
    new File("code128-barcode.png"));
String[] datas = BarcodeScanner.Scan(renderedImage, BarcodeType.Code128); 






Class ScanOptions and BarcodeDetail in Java

Top
Using Java Barcode Reader library, you can customize the barcode scanning options through ScanOptions object, and read, explore scanned barcode information (including text message, barcode coordinates, rotation angle) in BarcodeDetail object.


ScanOptions

Using ScanOptions in Java Barcode Scanner library, you can easily customize the barcode reading options to enhance reading speed, improve recognition rate.

List of options in ScanOptions
  • setScanSingle(): enable to scan maximum one barcode per image file. It will improve the reading speed, if there is only one barcode in one image file. The barcode library will stop scanning the remaining image area, once it has detected a valid barcode.
  • SetScanRegions(): enable to scan specified regions inside the image file. It will speed up the reading speed, and improve the reading accuracy.
  • SetMaxMultiThreadCount(): enable to scan barcodes using multi-threading in Java application.
  • setEnableGS1(): enable to read and parse GS1 data elements from GS1 compatible barcodes, such as QR Code, Data Matrix, GS1-128, GS1 DataBar. More details, view here: How to read and parse GS1 barcode image using Java?
  • setScanDirection(): Specify the barcode library image scanning direction to speed up the barcode reading speed.
  • setStepInterval(): When defining the barcode search area, the scanning spacing is used, which indicates how many lines are scanned at a time.
  • setZoomRatio(): Valid range: 0.2F - 5.0F. Default is 1.0F. Apply scanned image zooming.
  • setCode39WithCheckCharacter(): For Code 39 barcode only. Set it to true, if the Code 39 barcode contains check character.




BarcodeDetail

You can quickly get the scanned barcode data in string using the Java codes below.

String[] datas = BarcodeScanner.Scan("code128-barcode.png", BarcodeType.Code128); 


Sometimes you need know and explore more information about scanned barcode, such as barcode location on the image, barcode rotation angle.

We have included the Java class BarcodeDetail in Java Barcode Reader library to help read and explore more information about the scanned barcode data. In class BarcodeDetail, we have provided useful properties and methods for each scanned barcodes.

  • getRotation(): find the rotation angle about the scanned barcode.

  • X1, Y1, X2, Y2, X3, Y3, X4, Y4: 8 get methods to return the detected barcode position on the image file.

  • isStructuredAppend(): It is for QR Code and Data Matrix barcodes only. To identify whether the QR Code or Data Matrix barcode is in Structure Append mode.

  • IsGS1Compitable(): It is for QR Code, Data Matrix and Code 128 barcodes only. To identify whether the found barcode is GS1 compatible.

  • Method isMacroPDF417(): It is for PDF417 barcodes only.

  • Method getDataBytes(). You can get the read barcode data in byte array.

  • Method getASCIITextMessage(). You can get the returned barcode text in ASCII characters.

  • Method getMessage(). You can get the scanned barcode data using default UTF8 encoding or your specified encoding format.


Java code to scan and get more barcode information from BarcodeDetail object

ScanOptions options = new ScanOptions(BarcodeType.DataMatrix);
options.setScanSingle(true);
BarcodeDetail[] datas = BarcodeScanner.Scan(
    "data-matrix-sample-image.png", options);
for (BarcodeDetail obj : datas) {
    System.out.println("Message: '" + obj.getMessage() + "'");
    //  Show index and file path of the source image in the input array.
    //  getSourceFilePath() returns empty if the input is file streams or Bitmap objects.
    System.out.println(" Source: Index=" + obj.getSourceIndex() 
        + "; Source Path: " + obj.getSourceFilePath());
}




How to improve barcode reading performance using Java Barcode Reader library?

Top

Quick to scan and read a single barcode in Java



You can choose scan only one barcode from the image file, which will improve the reading speed. The barcode library will stop scanning once it has recognized one valid barcode from the image in Java project.

String[] datas = BarcodeScanner.ScanSingleBarcode(
    "sample-code128-barcode.png", BarcodeType.Code128); 




Multiple barcodes scanning in Java

By default, the Java barcode reader library will detect and recognize all barcodes with specified format in the image file.

String[] datas = BarcodeScanner.Scan("qrcode-barcode.png", BarcodeType.QRCode);
BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails(
    "qrcode-barcode.png", BarcodeType.QRCode);


You can scan multiple barcode formats at one time in the Java web and desktop applications, and the barcode library will return all found barcodes.

The Java source code below will explain how to scan and decode QR Code and Code 128 barcodes from the image file in Java application.

ArrayList<BarcodeType> formats = new ArrayList<BarcodeType>();
formats.add(BarcodeType.QRCode);
formats.add(BarcodeType.Code128);	
formats.add(BarcodeType.EAN13);		
BarcodeDetail[] datas = BarcodeScanner.ScanInDetails("multiple-barcodes.png", formats);

The barcode image below contains three barcodes with different barcode types: QR Code, Code 128 (GS1-128), EAN-13.





Scan and read barcodes from specified image regions in Java

To improve the image scanning speed, the barcode reader library also supports scanning the barcodes from specified rectangle areas inside the barcode image in Java program.

ArrayList<SRegion> regions = new ArrayList<SRegion>();
regions.add(new SRegion(0, 0, 50, 60));
String[] barcodes = BarcodeScanner.ScanRegions("qrcode-barcodes.png", 
    BarcodeType.QRCode, regions);




Scan barcodes with specified directions in Java

ScanOptions ops = new ScanOptions(BarcodeType.Code128);
ops.setScanDirection(
    ScanOptions.ScanDirection_LeftToRight |
    ScanOptions.ScanDirection_TopToBottom);


You can choose one or multiple directions combined to scan the barcodes in the Java application.

public static final int ScanDirection_Undefined = 0;
public static final int ScanDirection_LeftToRight = 1;
public static final int ScanDirection_TopToBottom = 2;
public static final int ScanDirection_RightToLeft = 4;
public static final int ScanDirection_BottomToTop = 8;
public static final int ScanDirection_Horizontal = 5;
public static final int ScanDirection_Vertical = 10;
public static final int ScanDirection_All = 15;




Scan barcodes with customized step interval in Java

When the Java Barcode Reader library scans barcodes from an image file, it will load the whole image content, scan image pixels one line by one line. You can use method setStepInterval() in class ScanOptions to customize the scanning step interval.

The method input parameter specifies the scanning spacing, which indicates how many lines are scanned at a time.
The minimum value is 1, and every row will be scanned.
Default value: 5 (scan once every 5 lines)

The Java program below, shows how to customize the barcode image scanning step intervals to improve the reading speed.

ScanOptions ops = new ScanOptions(BarcodeType.Code128);
ops.ScanDirection = ScanDirectionType.LeftToRight;
ops.StepInterval = 10;




Process Complex Barcode Text Message in Java

Top
Java Barcode Reader library support scanning and decoding barcode with complex text message. You can easily read and decode digits, ASCII characters, Unicode text, GS1 text message from barcodes. View more details here:




Read, Scan barcode image in special forms in Java

Top

Read rotated barcode

Using Java Barcode Reader library, you will automatically scan, read and recognize all rotated barcodes without any extra codes. The barcode library will automatically find and detect any rotated barcodes during scanning.





Read colored barcode

The java barcode scanner sdk will handle QR Code and other barcodes with different bar and space colors rendered.





Read styled QR Code with logo image

Java Barcode Reader library supports QR Code with logo image scanning in Java application.





Read Rectangle Data Matrix 2d barcode

Barcode scanner library supports Data Matrix reading in both square and rectangle shapes.



 

Java Barcode Reader FAQ

Top



How to read barcode from a specified area (rectangle) in the image?

To improve scanning speed, you can specify a rectangle area in the image for scanning barcode.

ScanOption option = new ScanOption();
/**
* specify barcode area in the scanning image
*
* x1, y1, x2, y2, valid values are from 0 to 1, inclusive.
*
* @param x1  barcode area left top point X value, % of image width
* @param y1  barcode area left top point Y value, % of image height
* @param x2  barcode area right bottom point X value, % of image width
* @param y2  barcode area right bottom point Y value, % of image height
*/
// scan top 20% of the image
option.setScanDirection(ScanOption.SCAN_FROM_TOP_TO_BOTTOM);
option.setBarcodeArea(0, 0, 1, 0.2);
String[] datas = BarcodeReader.read(new File("C:/YourBarcodeImage.gif"), BarcodeReader.EAN8, option);
  • Pass your barcode image file, and barcode type, and ScanOption object to BarcodeReader, and it will return barcode datas in the specified area from the image.




TIFF/Bitmap Image Reader


For TIFF or Bitmap image reading only, developers need include two Java Advanced Imaging jars from Sun Microsystems, jai_codec.jar, jai_core.jar. Both are included under lib folder.




How to get supplement barcode data?



For barcode EAN 8, EAN 13, UPC-A, UPC-E, their barcode image may contain 2 or 5 digits supplement barcode, Java Barcode Reader will add supplement barcode data to the main barcode data.

For example, Java barcode reader will return data as "123450512345" for above sample image. The First 7 digits "1234505" is UPCE data, and the last 5 digits "12345" is UPCE supplement data.




About UPC-E data



For UPC-E barcode, usually there are 8 digits printed on the barcode label. The first digit is UPC-E number system, valid values are 0 or 1. The next 6 digits are UPC-E data, and the last one is checksum.

Our library will return 6 digits data and the last one checksum. Developer should use the first 6 digits as UPC-E data, according to UPC-E specification.

For the above barcode image, 0 is the number system, 123450 is the UPC-E data, and 5 is the checksum. Our library will return 1234505, plus suppliment barcode 12345. So the final output will be 123450512345.




Support Multi-page Tiff File



For multi-page tiff file, Java barcode reader will scan every page in the file, and return all barcodes in all pages.



Java Barcode Reader Downloaded Package Contents

Top
  • OnBarcode.BarcodeReader.jar for Java Barcode Reader library file.

  • lib contains three jars for TIFF and bitmap image.

  • Software_License_Agreement.pdf for barcode library license information.

  • Developer Guide.pdf developer guide.




































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.