我想在二维码中嵌入一些原始文本和网址。 例如数据可以是: ABCD http://www.example.com XYZ 现在扫描 http://www.example.com 应弹出,其余数据应保持完整。 意味着我将在我的二维码中编码(ABCD http://www.example.com XYZ),但我希望弹出(http://www.example.com)。
我已经使用zxing尝试过 但在扫描时会显示外部字符串
public static void main(String[] args) {
// Define the data, including a properly formatted URL
String data = "ABCD https://www.google.com fgh";
// Define QR code properties
int width = 300;
int height = 300;
String fileType = "png"; // or "jpg", "gif", etc.
// Create hints for QR code generation
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
// Generate QR code
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
// Create a BufferedImage from the BitMatrix
BufferedImage qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
qrImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
// Save the QR code to a file
File qrFile = new File("clickable_url_qrcode." + fileType);
ImageIO.write(qrImage, fileType, qrFile);
System.out.println("Clickable URL QR code generated successfully!");
} catch (Exception e) {
System.err.println("Error generating QR code: " + e.getMessage());
}
}
输出: ABCD https://www.google.com fgh 扫描二维码
我必须使用这种格式 因为这只是一个示例,但我正在使用正确指定的 emvco 规范 并且链接前面和后面都有空格
试试这个
//after scanning
String data = "ABCD https://www.google.com fgh";
String[] urls = data.split("\\s+");
for (String url : urls) {
if (url.startsWith("http://") || url.startsWith("https://")) {
System.out.println(url);
//it will print "https://www.google.com";
}
}