【发布时间】: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);
}
}
}
【问题讨论】:
-
你是什么意思“有一些文字”?
-
你能检查一下我附上的图片吗?它显示了我想如何生成二维码。对不起,我不能方便地发布它。