Java 中的二维条码生成问题

2D barcode generation issue in Java

我正在使用 iText 生成条形码 API,当它是线性条形码时,一切看起来都很好,当它是二维条形码时,条形码作为图像放入 pdf 文档中,因此降低了低分辨率条形码的质量打印机,无法扫描条码。下面是代码

BarcodePDF417 pdf417 = new BarcodePDF417();
    String text = "BarcodePDF417 barcode";
    pdf417.setText(text);
    Image img = pdf417.getImage();        
    document.add(img);

现在我正在寻找绘制条形码的替代方法,我发现 palceBarcode 方法可能有利于需求。

我在 itext 源代码中看到了 BarcodePDF417 class 中的以下代码,但无法找到使用方法

public void placeBarcode(PdfContentByte cb, BaseColor foreground, float moduleHeight, float moduleWidth) {
    paintCode();
    int stride = (bitColumns + 7) / 8;
    cb.setColorFill(foreground);
    for (int k = 0; k < codeRows; ++k) {
        int p = k * stride;
        for (int j = 0; j < bitColumns; ++j) {
            int b = outBits[p + j / 8] & 0xff;
            b <<= j % 8;
            if ((b & 0x80) != 0) {
                cb.rectangle(j * moduleWidth, (codeRows - k - 1) * moduleHeight, moduleWidth, moduleHeight);
            }
        }
    }
    cb.fill();
}

谁能推荐一下上述方法的使用方法?

我写了如下代码,但整个页面变黑了。

Rectangle pageSize = new Rectangle(w * 72, h * 72);
    Document doc = new Document(pageSize, 1f, 1f, 1f, 1f);
    PdfWriter writer = PdfWriter.getInstance(doc, getOutputStream());
    doc.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodePDF417 pf = new BarcodePDF417();
    pf.setText("BarcodePDF417 barcode");
    pf.getImage();
    Rectangle rc = pf.getBarcodeSize();
    pf.placeBarcode(cb, BaseColor.BLACK, rc.getHeight(), rc.getWidth());
    doc.close();

请查看 BarcodePlacement 示例。在这个例子中,我们创建了三个 PDF417 条形码:

PdfContentByte cb = writer.getDirectContent();
Image img = createBarcode(cb, "This is a 2D barcode", 1, 1);
document.add(new Paragraph(
    String.format("This barcode measures %s by %s user units",
       img.getScaledWidth(), img.getScaledHeight())));
document.add(img);
img = createBarcode(cb, "This is NOT a raster image", 3, 3);
document.add(new Paragraph(
    String.format("This barcode measures %s by %s user units",
        img.getScaledWidth(), img.getScaledHeight())));
document.add(img);
img = createBarcode(cb, "This is vector data drawn on a PDF page", 1, 3);
document.add(new Paragraph(
    String.format("This barcode measures %s by %s user units",
        img.getScaledWidth(), img.getScaledHeight())));

结果在外面看起来像这样:

一个特定的条形码在内部看起来像这样:

我添加此 inside 视图以显示二维条形码未添加为光栅图像(与您尝试的初始方法一样)。它是由一系列小矩形组成的矢量图像。您可以通过查看 barcode_placement.pdf 文件自行检查。

请不要混淆,因为我使用了 Image 对象。如果您查看 createBarcode() 方法,您会发现 Image 实际上是一个矢量图像:

public Image createBarcode(PdfContentByte cb, String text,
    float mh, float mw) throws BadElementException {
    BarcodePDF417 pf = new BarcodePDF417();
    pf.setText("BarcodePDF417 barcode");
    Rectangle size = pf.getBarcodeSize();
    PdfTemplate template = cb.createTemplate(
        mw * size.getWidth(), mh * size.getHeight());
    pf.placeBarcode(template, BaseColor.BLACK, mh, mw);
    return Image.getInstance(template);
}

传给placeBarcode()方法的高度和宽度,定义绘制的小矩形的高度和宽度。如果查看 inside 视图,您可以看到例如:

0 21 3 1 re

这是一个 x = 0,y = 21,宽 3,高 1 的矩形。

当您询问条形码的大小时,您会得到将要绘制的矩形的数量。因此条形码的尺寸是:

Rectangle size = pf.getBarcodeSize();
float width = mw * size.getWidth();
float height = mh * size.getHeight();

只有当 mwmh 等于 1 时,您假设 size 是用户单位的大小才是正确的。

我使用这些值创建一个 PdfTemplate 实例,并将条形码绘制到此 Form XObject。大多数时候,使用 Image class 比使用 PdfTemplate 更容易,所以我将 PdfTemplate 包装在 Image.[=32 中=]

然后我可以像添加任何其他图像一样将此 Image 添加到文档中。与普通图像的主要区别在于此图像是矢量图像。