【发布时间】:2021-08-30 10:29:21
【问题描述】:
我找到了下一个使用 google zxing 库从字符串生成 QRCODE 的代码
String textToCodify = "1234567891234";
QRCodeWriter barcodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = barcodeWriter.encode(textToCodify, BarcodeFormat.EAN_13, 200, 200);
File outputfile = new File("image.jpg");
BufferedImage code = MatrixToImageWriter.toBufferedImage(bitMatrix);;
ImageIO.write(code, "jpg", outputfile);
但是当我尝试生成 UPC 或 EAN 代码时,出现了一些异常。
java.lang.IllegalArgumentException: Can only encode QR_CODE, but got EAN_13
at com.google.zxing.qrcode.QRCodeWriter.encode(QRCodeWriter.java:59)
at com.google.zxing.qrcode.QRCodeWriter.encode(QRCodeWriter.java:44)
at workWithBarCode.Test.generateQRCodeImage(Test.java:43)
at workWithBarCode.Test.main(Test.java:23)
有人可以帮忙吗?
【问题讨论】: