我需要实现一个没有源代码的旧系统。 系统打印带有以下二维码输出的标签:(QR version 6 41x41, alignment patterns only 1)
我尝试使用 Zxing 库用 Java 编写程序。 但是,生成的输出与之前的输出不同:(QR 版本 >= 7,alignment patterns = 6)
即使两个读取扫描仪将产生相同的输出和字符长度(154@char)
"H 4A91K BB7998 M11AU10406 CBU 4A91471 H 000160 69H 69578 00009Y000 @N "
可能吧,好像和qr低版本有关。 我试图制作一个较低的 qr.version 但出现以下错误:
com.google.zxing.WriterException: Data too big for requested version
at com.google.zxing.qrcode.encoder.Encoder.encode(Encoder.java:123)
at com.google.zxing.qrcode.QRCodeWriter.encode(QRCodeWriter.java:78)
任何人都可以帮助我如何使 qrcode 输出与版本 1 相同(对齐模式 只有 1)?
谢谢大家的支持
二维码内置了一些冗余来纠正损坏的二维码。创建二维码时,您可以选择纠错级别。级别越高,QR 码中必须包含的信息越多以实现冗余,因此 QR 码必须越大。您参考中的纠错级别似乎低于您创建的二维码。您可能想看看这个 StackOverflow 答案,看看如何在 Z-Xing 中指定它。
如果您需要生成具有特定设置的二维码,例如只有一种对齐模式的二维码版本6(41x41),使用Java中的Zxing库,您可以尝试以下方法:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import com.google.zxing.qrcode.encoder.Encoder;
import com.google.zxing.qrcode.encoder.QRCode;
import java.util.EnumMap;
import java.util.Map;
public class QRCodeGenerator {
public static void main(String[] args) {
String data = "Your data goes here";
int qrCodeVersion = 6;
int qrCodeSize = 41;
try {
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 0);
QRCodeWriter writer = new QRCodeWriter();
QRCode qrCode = Encoder.encode(data, ErrorCorrectionLevel.H, hints);
ByteMatrix matrix = qrCode.getMatrix();
int matrixWidth = matrix.getWidth();
int matrixHeight = matrix.getHeight();
int paddingX = (qrCodeSize - matrixWidth) / 2;
int paddingY = (qrCodeSize - matrixHeight) / 2;
for (int y = 0; y < matrixHeight; y++) {
for (int x = 0; x < matrixWidth; x++) {
if (matrix.get(x, y) == 1) {
// Adjust the position of the generated QR code based on the padding
int xPos = paddingX + x;
int yPos = paddingY + y;
System.out.print(xPos + "," + yPos + " "); // Print the QR code position
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在此示例中,将
Your data goes here
替换为您要编码到QR码中的实际数据。调整 qrCodeVersion
和 qrCodeSize
变量以满足您的要求(在您的情况下版本 6 和尺寸 41x41)。
代码使用Zxing库生成二维码,然后遍历二维码矩阵,识别每个黑色模块的位置(对齐图案)并打印坐标。您可以修改此代码以满足您的特定需求,例如保存位置以生成标签。
请注意,使用非标准设置生成二维码可能会导致与现有二维码阅读器或扫描仪的兼容性问题。必须使用各种扫描设备对生成的二维码进行彻底测试,以确保它们可以被正确读取。