【问题标题】:How to generate QR code with some text using JAVA?如何使用JAVA生成带有一些文本的二维码?
【发布时间】:2023-01-20 01:41:50
【问题描述】:

I want to generate QR code with some text using JAVA like this. please check this image. This is how I want to generate my QR code. (带有用户名和事件名称文本)

这是我的代码,它只生成 (QR) 代码,(没有任何附加文本)。如果有人知道如何生成带有文本的 QR 码,请帮助我。

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);
        }
    }
}

【问题讨论】:

  • 你是什​​么意思“有一些文字”?
  • 你能检查一下我附上的图片吗?它显示了我想如何生成二维码。对不起,我不能方便地发布它。

标签: java text qr-code


【解决方案1】:

要用Java生成二维码,我们需要用到第三方库ZXing(Zebra Crossing)。这是一个流行的 API,允许我们使用 QR 码进行处理。借助该库,我们可以轻松生成并读取二维码。在转向 Java 程序之前,我们需要将 ZXing 库添加到项目中。我们可以从官网下载。

  • zxing core-3.3.0.jar
  • zxing javase-3.3.0.jar

下载后,将其添加到类路径中。或者在 pom.xml 文件中添加以下依赖项。

<dependencies>  
<dependency>  
<groupId>com.google.zxing</groupId>  
<artifactId>core</artifactId>  
<version>3.3.0</version>  
</dependency>  
<dependency>  
<groupId>com.google.zxing</groupId>  
<artifactId>javase</artifactId>  
<version>3.3.0</version>  
</dependency>  
</dependencies>  

让我们创建一个生成二维码的 Java 程序。

生成二维码.java

import java.io.File;  
import java.io.IOException;  
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.NotFoundException;  
import com.google.zxing.WriterException;  
import com.google.zxing.client.j2se.MatrixToImageWriter;  
import com.google.zxing.common.BitMatrix;  
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
public class GenerateQRCode  
{  
//static function that creates QR Code  
public static void generateQRcode(String data, String path, String charset, Map map, int h, int w) throws WriterException, IOException  
{  
//the BitMatrix class represents the 2D matrix of bits  
//MultiFormatWriter is a factory class that finds the appropriate Writer subclass for the BarcodeFormat requested and encodes the barcode with the supplied contents.  
BitMatrix matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, w, h);  
MatrixToImageWriter.writeToFile(matrix, path.substring(path.lastIndexOf('.') + 1), new File(path));  
}  
//main() method  
public static void main(String args[]) throws WriterException, IOException, NotFoundException  
{  
//data that we want to store in the QR code  
String str= "THE HABIT OF PERSISTENCE IS THE HABIT OF VICTORY.";  
//path where we want to get QR Code  
String path = "C:\Users\Anubhav\Desktop\QRDemo\Quote.png";  
//Encoding charset to be used  
String charset = "UTF-8";  
Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();  
//generates QR code with Low level(L) error correction capability  
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  
//invoking the user-defined method that creates the QR code  
generateQRcode(str, path, charset, hashMap, 200, 200);//increase or decrease height and width accodingly   
//prints if the QR code is generated   
System.out.println("QR Code created successfully.");  
}  
}  

【讨论】:

  • 不要只发布链接作为答案,至少要在此处复制相关信息。这将避免用户必须通过链接,并确保一旦链接断开,答案将变为无效。
  • 从我从问题中看到的情况来看,已经生成了二维码。
猜你喜欢
  • 1970-01-01
  • 2016-05-08
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多