二维码图像之间的不同模式

问题描述 投票:0回答:3

我需要实现一个没有源代码的旧系统。 系统打印带有以下二维码输出的标签:(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)?

谢谢大家的支持

java qr-code zxing
3个回答
1
投票

如果我检查了你预期的 QR 码有大小为 41x41 的模块,它使用的是版本 6。 版本 6 只有一种对齐模式。您的数据包含 1244 位,而版本 6 的最大大小仅为 1,088 位,用于具有较低 (L) 纠错的混合数据位。因此,出现异常是因为您的数据大于以下版本 6 中 QR 码的最大容量。 您的数据最可能的版本是版本 7,它具有 1,248 位的容量和较低的纠错能力。但是这个版本有不止一种对齐模式。 我建议您尝试压缩数据以降低数据大小。 Version 6 Size Description source: https://www.qrcode.com/en/about/version.html


0
投票

二维码内置了一些冗余来纠正损坏的二维码。创建二维码时,您可以选择纠错级别。级别越高,QR 码中必须包含的信息越多以实现冗余,因此 QR 码必须越大。您参考中的纠错级别似乎低于您创建的二维码。您可能想看看这个 StackOverflow 答案,看看如何在 Z-Xing 中指定它。


0
投票

如果您需要生成具有特定设置的二维码,例如只有一种对齐模式的二维码版本6(41x41),使用Java中的Zxing库,您可以尝试以下方法:

  1. 在您的 Java 项目中包含 Zxing 库。您可以从 Zxing GitHub 存储库下载该库,或将其作为 Maven 或 Gradle 依赖项包含在您的项目配置中。
  2. 使用Zxing库生成二维码。这是一个使用 Zxing 库生成具有指定设置的 QR 码的示例:

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库生成二维码,然后遍历二维码矩阵,识别每个黑色模块的位置(对齐图案)并打印坐标。您可以修改此代码以满足您的特定需求,例如保存位置以生成标签。

请注意,使用非标准设置生成二维码可能会导致与现有二维码阅读器或扫描仪的兼容性问题。必须使用各种扫描设备对生成的二维码进行彻底测试,以确保它们可以被正确读取。

© www.soinside.com 2019 - 2024. All rights reserved.