About QR Code
QR Code, short for Quick Response Code, is a two-dimensional barcode that stores information as a series of pixels in a square grid. A QR code can contain a lot of data, but when it is scanned, the user can immediately access the information, which is why it is called a quick response code.
The QR code system was first invented in 1994 by a Japanese company and now it has become the most commonly used two-dimensional barcode in the world.
Java Library to Generate or Scan QR Code
To generate or scan QR code using Java, this article uses a third-party library called Spire.Barcode for Java.
If you are using maven, you can install Spire.Barcode for Java from maven by adding the following configurations to your project’s pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.barcode</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
If you are not using maven, you can download the latest version of Spire.Barcode for Java from the official website, extract the package and then import Spire.Barcode.jar in the lib folder into your project as a dependency.
Generate QR Code using Java
The following are the main steps to generate a QR Code using Java:
- Instantiate a BarcodeSettings object.
- Specify the barcode type, data, and other properties such as error correction level and barcode module width using BarcodeSettings.setType(), BarcodeSetting.setData(), BarcodeSettings.setQRCodeECL(), and BarcodeSettings.setX() methods.
- Instantiate a BarCodeGenerator object.
- Generate QR code image using BarCodeGenerator.generateImage() method.
- Save the image to a .png file using ImageIO.write() method.
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import com.spire.barcode.QRCodeECL;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class GenerateQRCode {
public static void main(String[] args) throws IOException {
//Apply a license key if you have one
com.spire.license.LicenseProvider.setLicenseKey("License Key");
//Instantiate a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();
//Set barcode type
settings.setType(BarCodeType.QR_Code);
String data = "https://google.com";
//Set barcode data
settings.setData(data);
//Set display text
settings.setData2D(data);
//Set text invisible
settings.setShowText(false);
//Set border invisible
settings.hasBorder(false);
//Set width for the barcode module
settings.setX(2);
//Set the error correction level
settings.setQRCodeECL(QRCodeECL.M);
//Instantiate a BarCodeGenerator object based on the specific settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
//Generate QR code image
BufferedImage bufferedImage = barCodeGenerator.generateImage();
//save the image to a .png file
ImageIO.write(bufferedImage,"png",new File("QR_Code.png"));
}
}
The generated QR code image:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/09/1-3.png?w=209)
Scan QR Code using Java
The following are the main steps to scan a QR Code using Java:
- Scan the QR Codes within an image using BarcodeScanner.scan(String fileName, BarCodeType barcodeType) method, and save the results into a String array.
- Loop through the array and print out the data.
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeScanner;
public class ScanQRCode {
public static void main(String[] args) throws Exception {
//Apply a license key if you have one
com.spire.license.LicenseProvider.setLicenseKey("License Key");
//Scan the QR codes within an image and store the results into a String array
String[] datas = BarcodeScanner.scan("QR_Code.png", BarCodeType.QR_Code);
//Loop through the array
for(String data : datas){
//Print out the result
System.out.print(data);
}
}
}
Output:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/09/2-5.png?w=456)
Get a Free Trial License
The library will generate an evaluation watermark on the generated QR code image, if you would like to remove the watermark or to scan a QR code image, you can request a free 30 days trial license.
Besides, it also provides a free version called Free Spire.Barcode for Java, which supports generating QR code without any watermark. But note that it doesn’t include the ability to scan QR code.