gpt4 book ai didi

java - Java中的二维条码生成问题

转载 作者:行者123 更新时间:2023-11-30 08:13:01 25 4
gpt4 key购买 nike

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

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

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

我在 itext 源代码的 BarcodePDF417 类中看到了下面的代码,但无法找到如何使用它的方法

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())));

结果在外面看起来是这样的:

enter image description here

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

enter image description here

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

请不要混淆,因为我使用的是 Image目的。如果您查看 createBarcode()方法,你可以看到Image实际上是一个 vector 图像:

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() 的高度和宽度方法,定义绘制的小矩形的高度和宽度。如果您查看 内部 View ,您可以看到例如:

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();

您的假设 size是用户单位的大小只有在 mw 时才是正确的和 mh等于1 .

我使用这些值创建一个 PdfTemplate实例,然后我将条形码绘制到此 Form XObject。大多数时候,使用 Image 更容易类比使用 PdfTemplate ,所以我包装了 PdfTemplateImage 里面.

然后我可以添加这个 Image就像任何其他图像一样添加到文档中。与普通图像的主要区别在于,该图像是 vector 图像。

关于java - Java中的二维条码生成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30186774/

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