javatextqr-code

How to generate QR code with some text using Java?


I want to generate a QR code with some text (user name and event name) using Java like this:

QR code with text on top and below

This is my code and this generates only the QR code, without any additional text.

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class Create_QR {
    public static void main(String[] args) {
        try {
            String qrCodeData = "This is the text";
            String filePath = "C:\\Users\\Nirmalw\\Desktop\\Projects\\QR\\test\\test_img\\my_QR.png";
            String charset = "UTF-8"; // or "ISO-8859-1"

            Map < EncodeHintType, ErrorCorrectionLevel > hintMap = new HashMap < EncodeHintType, ErrorCorrectionLevel > ();

            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

            BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset),
                    BarcodeFormat.QR_CODE, 500, 500, hintMap);

            MatrixToImageWriter.writeToFile (matrix, filePath.substring(filePath.lastIndexOf('.') + 1), new File(filePath));

            System.out.println("QR Code created successfully!");
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

If anyone knows how to generate a QR code with text above and below the code, please help me.


Solution

  • You can generate a QR code with text in Java using Free Spire.Barcode for Java API. First, download the API's jar from this link or install it from Maven Repository:

        <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.free</artifactId>
            <version>5.1.1</version>
        </dependency>
    </dependencies>
    

    Next, refer to the following code sample:

    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 {
            //Instantiate a BarcodeSettings object
            BarcodeSettings settings = new BarcodeSettings();
            //Set barcode type
            settings.setType(BarCodeType.QR_Code);
            //Set barcode data
            String data = "https://stackoverflow.com/";
            settings.setData(data);
            //Set barcode module width
            settings.setX(2);
            //Set error correction level
            settings.setQRCodeECL(QRCodeECL.M);
    
            //Set top text
            settings.setTopText("User Name");
            //Set bottom text
            settings.setBottomText("Event Name");
    
            //Set text visibility
            settings.setShowText(false);
            settings.setShowTopText(true);
            settings.setShowBottomText(true);
    
            //Set border visibility
            settings.hasBorder(false);
    
            //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 following is the generated QR code image with text:

    Generate QR Code with Text in Java