In the previous post, we have demonstrated how to use Spire.Barcode for Java to create barcode images in Java applications. This tutorial provides code sample for generating QR code in Java.
package GenerateBarcode; 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.sun.javafx.print.Units; public class QR_Code { public static void main(String[] args) throws IOException { //create an instance of BarcodeSetteings BarcodeSettings settings = new BarcodeSettings(); //set barcode type settings.setType(BarCodeType.QR_CODE); //set barcode data settings.setData("ABC 123456789"); //set dispaly text settings.setData2D("ABC 123456789"); //show text on bottom settings.setShowTextOnBottom(true); //set the border invisible settings.hasBorder(false); //set width of the barcode module settings.setX(2); //set the error correction level settings.setQRCodeECL(QRCodeECL.M); //create BarCodeGenerator object based on settings BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings); //generate image data and store in BufferedImage instance BufferedImage bufferedImage = barCodeGenerator.generateImage(); //save to image ImageIO.write(bufferedImage,"png",new File("QR_CODE.png")); System.out.println("Complete!"); } }
Output: