Generate and Read QR Code in Java

Alexander Stock
5 min readJan 17, 2024

--

Generating and reading QR codes has become increasingly popular in today’s digital age. QR (Quick Response) codes are two-dimensional barcodes that can store a wide range of information, such as website URLs, contact details, or product information. They provide a convenient way to quickly access and share data using smartphones or other devices equipped with a camera.

In this article, we will explore the process of generating QR codes programmatically and reading them using the Spire.Barcode for Java library.

Install Spire.Barcode for Java

Spire.Barcode for Java is a tiny yet robust library that empowers developers to effortlessly create and decode more than 38 widely-used 1D or 2D barcodes within Java applications.

E-iceblue Inc, the provider of this component, offers both a free version and a commercial version of the product. While the free version allows for QR code generation, it lacks the capability to read them. Therefore, this article utilizes the commercial version to illustrate the process of generating and reading QR codes in Java.

To install the library from the Maven repository, simply add the following configuration to your project’s pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.barcode</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies>

Alternatively, you can download Spire.Barcode for Java and manually import the jar file as dependency in your Java application.

Get a Free Trial License

The commercial version comes with certain limitations on specific features, which can be eliminated by acquiring a license. You can reach out to their sales team to request a 30-day free trial license. And the license can be applied by key in your program through this line of code.

LicenseProvider.setLicenseKey("Your License Key");

Generate QR Code with Custom Appearance in Java

The Spire.Barcode for Java library includes the BarcodeSettings class, which is utilized to customize the properties of the generated barcode. For instance, you can set the barcode type using the setType() method and specify the data for the barcode using the setData() method. With these settings in place, a BarCodeGenerator object is created, offering the generateImage() method to generate the barcode image.

The steps to generate QR code using Spire.Barcode for Java are as follows:

  • Create a BarcodeSettings object to configure the properties of the generated barcode.
  • Set the barcode type to QR_Code using setType() method under the object.
  • Set the other attributes of the QR code using the methods under the BarcodeSettings object.
  • Create a BarCodeGenerator object based on the configured settings.
  • Call generateImage() method to generate the QR code, and return a BufferedImage object.
  • Save the QR code image to a PNG format file.

The following code demonstrates how to generate a QR code with custom settings using the Spire.Barcode library and save it to an image file.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import com.spire.barcode.QRCodeECL;
import com.spire.barcode.license.LicenseProvider;

public class GenerateQRCode {

public static void main(String[] args) throws IOException {

// Apply a temporary license
LicenseProvider.setLicenseKey("Your License Key");

// Create a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();

// Set barcode type as QR code
settings.setType(BarCodeType.QR_Code);

// Set barcode data
settings.setData("https://medium.com/@alexaae9");

// Do not display text on QR code
settings.setShowText(false);

// Set border to none
settings.hasBorder(false);

// Set width of the barcode module
settings.setX(2);

// Set error correction level
settings.setQRCodeECL(QRCodeECL.M);

// Set fore color
settings.setForeColor(Color.blue);

// Create BarCodeGenerator object based on settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

// Generate image data and store in BufferedImage
BufferedImage bufferedImage = barCodeGenerator.generateImage();

// save to image
ImageIO.write(bufferedImage,"png",new File("MyQRCode.png"));
}
}

output:

An QR code with a core color or purple.
Figure 1. QR Code with Custom Appearance

Generate QR Code with a Logo at Center in Java

Traditionally, QR codes are composed of black squares arranged on a white background. However, incorporating a logo at the center of a QR code adds a touch of branding and personalization.

To include an image at the center, use the setQRCodeLogoImage() method of the BarcodeSettings object.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import com.spire.barcode.QRCodeECL;
import com.spire.barcode.license.LicenseProvider;

public class GenerateQRCode {

public static void main(String[] args) throws IOException {

// Apply a temporary license
LicenseProvider.setLicenseKey("Your License Key");

// Create a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();

// Set barcode type as QR code
settings.setType(BarCodeType.QR_Code);

// Set barcode data
settings.setData("https://medium.com/@alexaae9");

// Do not display text on QR code
settings.setShowText(false);

// Set border to none
settings.hasBorder(false);

// Set width of the barcode module
settings.setX(2);

// Set error correction level
settings.setQRCodeECL(QRCodeECL.M);

// Set fore color
settings.setForeColor(Color.black);

// Add a logo at the center
BufferedImage image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\MyLogo.png"));
settings.setQRCodeLogoImage(image);

// Create BarCodeGenerator object based on settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

// Generate image data and store in BufferedImage
BufferedImage bufferedImage = barCodeGenerator.generateImage();

// save to image
ImageIO.write(bufferedImage,"png",new File("MyQRCodeWithLogo.png"));
}
}

output:

An QR code with a logo at the center.
Figure 2. QR Code with A Logo at Center

Read QR Code in Java

By utilizing the BarcodeScanner.scanOne() method from Spire.Barcode, users can easily retrieve data from a QR code image. Here is an example.

import com.spire.barcode.BarcodeScanner;
import com.spire.barcode.license.LicenseProvider;

public class ReadQRCode {

public static void main(String[] args) throws Exception {

// Apply a temporary license
LicenseProvider.setLicenseKey("Your License Key");

// Scan a QR code image
String data = BarcodeScanner.scanOne("C:\\Users\\Administrator\\Desktop\\MyQRCode.png");
System.out.print(data);
}
}

output:

A screenshot shows the data read from an QR code image.
Figure 3. Read QR Code

Conclusion

In this article, we’ve learned how to create QR code with custom settings in Java, as well as how to retrieve data from a QR code image, with the help of Spire.Barocde for Java. As an advanced library, it supports the generation and scanning of many more barcode types such as Code 25, Code 39, Code 128, EAN 8, EAN 13, POST NET, PDF 417, and Data Matrix. If you’re interested in creating or reading them, do not hesitate to give the library a try.

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.