gpt4 book ai didi

java - 为什么ZXing解码这么差?

转载 作者:行者123 更新时间:2023-11-30 10:23:35 24 4
gpt4 key购买 nike

下面的程序使用ZXing(和PDFBox)将字节数组(长度N=1,2,3,...)编码为二维码,然后嵌入到PDF文档中,渲染,提取为BufferedImage,并解码。比较解码和编码的字节数组。首先尝试不使用解码选项 PURE_BARCODE 进行解码,如果失败,则尝试使用 PURE_BARCODE 选项进行解码。

解码在 N=20 左右开始失败(对于示例随机种子,N=28)。请注意,在解码失败之前,字节数组会被正确解码

当解码失败时,会在对话框中显示失败的二维码,并将有效负载字符串打印到控制台。

但是,如果我将手机指向此对话框,我的手机没有问题解码此二维码图像,并且解码后的字符串与控制台上的一致。

我犯了什么错误?

package zxing;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Map;
import java.util.PrimitiveIterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

public class Problem {

public static void main(String[] args) throws IOException,
WriterException,
NotFoundException,
ChecksumException,
FormatException {
Random r = new Random(12345);

PrimitiveIterator.OfInt rb = r.ints(-128, 127).iterator();

for (int N = 1; N < 100; N += 1) { // encode and decode random byte arrays of size N
System.out.println(N);
for (int numRuns = 0; numRuns < 10; numRuns++) { // number of tries at size N

byte[] payload = new byte[N]; // payload to be encoded

for (int i = 0; i < N; i++) { // payload random initialization
payload[i] = rb.next().byteValue();
}
final String payloadString = new String(payload, STRING_ENCODING); // encode as string

// encode using zxing
final BufferedImage qr = MatrixToImageWriter.toBufferedImage(new QRCodeWriter().encode(payloadString,
BarcodeFormat.QR_CODE, 256, 256, ENCODING_HINTS));

// insert into PDF
PDDocument pdDocument = new PDDocument();
PDPage page = new PDPage();
pdDocument.addPage(page);
PDPageContentStream pageContent = new PDPageContentStream(pdDocument, page);
pageContent.drawImage(JPEGFactory.createFromImage(pdDocument, qr), 0, 0, 200, 200);
pageContent.close();
// render pdf and extract qr code image
BufferedImage pageImage = new PDFRenderer(pdDocument).renderImage(0);
BufferedImage qrcodeImage = pageImage.getSubimage(0, pageImage.getHeight() - 200, 200, 200);
pdDocument.close();

byte[] resultPayload;
try { // try zxing decode qrcodeImage *not* in PURE_BARCODE mode
resultPayload = new QRCodeReader().decode(
new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(qrcodeImage.getWidth(),
qrcodeImage.getHeight(),
qrcodeImage.getRGB(0, 0, qrcodeImage.getWidth(), qrcodeImage.getHeight(), null, 0,
qrcodeImage.getWidth())))), DECODING_HINTS_IMPURE).getText().getBytes(
STRING_ENCODING);
} catch (Throwable ex) {
try { // failed so try zxing decode qrcodeImage in PURE_BARCODE mode
resultPayload = new QRCodeReader().decode(
new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(qrcodeImage.getWidth(),
qrcodeImage.getHeight(),
qrcodeImage.getRGB(0, 0, qrcodeImage.getWidth(), qrcodeImage.getHeight(), null,
0,
qrcodeImage.getWidth())))), DECODING_HINTS_PURE).getText().getBytes(
STRING_ENCODING);
} catch (Throwable ex2) { // both decodings failed, display image
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(qrcodeImage)),
"N=" + Integer.toString(N),
JOptionPane.PLAIN_MESSAGE, null);
System.out.println("Encoded=" + payloadString);
throw ex2;
}
}

if (!Arrays.equals(payload, resultPayload)) {
throw new RuntimeException("Payload mismatch.");
}


}
}

}
private final static String STRING_ENCODING = "ISO-8859-1";
final private static Map<EncodeHintType, Object> ENCODING_HINTS = new EnumMap<EncodeHintType, Object>(
EncodeHintType.class);

static {
ENCODING_HINTS.put(EncodeHintType.CHARACTER_SET, STRING_ENCODING);
ENCODING_HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
}

final private static Map<DecodeHintType, Object> DECODING_HINTS_IMPURE = new EnumMap<DecodeHintType, Object>(
DecodeHintType.class);

static {
DECODING_HINTS_IMPURE.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
DECODING_HINTS_IMPURE.put(DecodeHintType.CHARACTER_SET, STRING_ENCODING);
}

final private static Map<DecodeHintType, Object> DECODING_HINTS_PURE = new EnumMap<DecodeHintType, Object>(
DecodeHintType.class);

static {
DECODING_HINTS_PURE.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
DECODING_HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
DECODING_HINTS_PURE.put(DecodeHintType.CHARACTER_SET, STRING_ENCODING);
}

}

[... ZXing 抛出一个 FormatException ...]

[...ZXing手机应用程序也可以读取对话框二维码...]

最佳答案

以下更改将精度提高到大约 N=64 的高质量纠错和 N=100 没有纠错(这足以满足我的需求)。

需要进行三处更改。

首先,按照@Tilman Hausherr 的建议,在将二维码图像插入 PDF 时使用无损工厂。虽然这对 OP 中提供的代码没有任何影响,但它是其他更改生效的必要共同条件。

此外,以二进制而不是 rgb 格式呈现 PDF,并以更高的比例。默认比例 (1.0) 仅为 72dpi。

pageContent.drawImage(LosslessFactory.createFromImage(pdDocument, qr), 0, 0, 200, 200);
...
BufferedImage pageImage = new PDFRenderer(pdDocument)
.renderImage(0,3.0f,ImageType.BINARY);

关于java - 为什么ZXing解码这么差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com