Read Barcode from Image using Java

Barcodes play a vital role in the accurate tracking of inventory and delivery of products from manufacturing to point of sale. In certain cases, we may need to read barcodes from images programmatically. We can easily detect, recognize and, read various types of barcodes and QR codes in Java applications. It helps to develop barcode reader or scanner applications. In this article, we will learn how to read a barcode from an image using Java.

The following topics shall be covered in this article:

What is Barcode

A barcode is a machine-readable code that represents encoded data in the form of numbers and a pattern of parallel lines. The barcode image shows a series of parallel black and white bars varying the widths, spacing, and sizes. Barcode readers or scanners scan the barcode images and decode the information.

Java API to Read Barcode from Image

In order to read the barcodes from images within the Java applications, we will be using the Aspose.BarCode for Java. It is a feature-rich API that lets you generate, scan, and read a wide range of barcode symbologies. The BarCodeReader class of the API allows reading the barcodes. The readBarCodes() method of this class returns an array of recognized barcodes. The API allows reading barcodes of the specified DecodeType. The BarCodeResult class of the API stores the recognized barcode data such as barcode type, code text, region, and other parameters.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-barcode</artifactId>
    <version>22.5</version>
</dependency>

Read Barcode from an Image in Java

We can read barcodes from an image by following the steps given below:

  • Firstly, load an image using the BarCodeReader class.
  • Get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from an image using Java.

Read Barcode from an Image.

Read barcode from an image in Java.

Recognize Barcode of Specific Type in Java

We can read a barcode of a specific type from an image by following the steps given below:

  • Firstly, read buffer data of an image file using the ImageIO.read() method.
  • Next, create an instance of the BarCodeReader class with a buffered image and Decode Type.
  • Then, set a barcode decode type to read the barcode of a specific type, such as CODE_39_STANDARD.
  • After that, get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode of a specific type from an image using Java.

Recognize Barcode of Specific Type.

Recognize the barcode of a specific type in Java.

Read Multiple Barcodes from Image in Java

We can also read all the available barcodes from an image by following the steps given below:

  • Firstly, load an image using the BarCodeReader class.
  • Next, set a barcode decode type as ALL_SUPPORTED_TYPES.
  • After that, get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read multiple barcodes from an image using Java.

Read Multiple Barcodes from Image.

Read multiple barcodes from an image.

Get X and Y Coordinates of Barcode using Java

We can read the X and Y coordinates of the detected barcode from an image by following the steps given below:

  • Firstly, load an image using the BarCodeReader class.
  • Next, get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Then, loop through the results and check if the getRegion() is not null.
  • After that, get the Points array bounding the barcode region using the getPoints().
  • Finally, show the barcode’s X and Y points.

The following code sample shows how to get the X and Y coordinate points of a barcode from an image using Java.

Get X and Y Coordinates of Barcode.

Get X and Y coordinates of a barcode from an image.

Read Barcode From Specific Region of Image in Java

We can read a barcode from a specific region or an area of an image by following the steps given below:

  • Firstly, read buffer data of an image file using the ImageIO.read() method.
  • Next, create an instance of the BarCodeReader class. It takes a buffered image, a rectangle area, and a Decode Type as arguments.
  • After that, get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Finally, loop through the results and show the barcode’s type and text.

The following code sample shows how to read a barcode from a specific region of an image using Java.

Read Barcode From Specific Region of Image.

Read barcodes from a specific region of an image in Java.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • Load barcode image programmatically;
  • recognize barcodes of different types from an image;
  • read barcode coordinates in Java;
  • detect and read multiple barcodes from a single image;
  • detect and read barcodes from a specific area of an image.

Besides, you can learn more about Aspose.BarCode for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also