使用 ItextSharp 的条形码 i25
Barcode i25 using ItextSharp
我正在使用 BarcodeInter25 class 制作条形码。我能做到,但它只是模糊,如何才能变得更清晰??
它的背景白色也不是完全白色
我的代码:
BarcodeInter25 code25 = new BarcodeInter25();
Rectangle r = new iTextSharp.text.Rectangle(38, 152);
code25.ChecksumText = false;
code25.Code = "some digits";
code25.BarHeight = 2
System.Drawing.Image i = code25.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
MemoryStream ms = new MemoryStream();
i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Image img = Image.GetInstance(ms.ToArray());
ms.Dispose();
您应该避免以任何方式调整大小。输出很可能是像素完美的,但是当你缩放它时 up/down 双线性过滤器会平滑它使其变得模糊。
我在 PDF 中嵌入 QRC 代码时遇到了完全相同的问题,并通过避免调整大小解决了这个问题。
如果您确实需要不同的尺寸,请使用正确的 interpolation algorithm.
在代码中以编程方式应用它
看看你的代码,条形码模糊的原因应该很明显了。您将其转换为 System.Drawing.Image
(使其成为光栅图像),然后将其转换为 iTextSharp.text.Image
(但此时图像已经模糊)。
实现所需内容的正确方法是直接从条形码创建 iTextSharp.text.Image
(不要通过 System.Drawing.Image
)。可以这样做:
BarcodeInter25 code25 = new BarcodeInter25();
Rectangle r = new iTextSharp.text.Rectangle(38, 152);
code25.ChecksumText = false;
code25.Code = "some digits";
code25.BarHeight = 2;
PdfContentByte cb = writer.DirectContent;
Image img = code25.CreateImageWithBarcode(cb, null, null);
现在 Image
对象将不是光栅图像(带有使线条模糊的像素),而是真正的矢量图像(没有像素,但是 moveTo()
、lineTo()
和 stroke()
)。矢量数据的优点是它与分辨率无关:你可以随意放大和缩小,它总是很清晰。
这在 Chapter 10 of my book where you'll find the Barcodes example. In that chapter, you'll also discover the setFont()
中有解释(或在 iTextSharp 中 Font
属性)。我引用 API 文档:
public void setFont(BaseFont font)
Sets the text font.
Parameters:
font
- the text font. Set to null
to suppress any text
所以如果你不想看到任何文字,你可以在上面的代码片段中添加以下行:
code25.Font = null;
我正在使用 BarcodeInter25 class 制作条形码。我能做到,但它只是模糊,如何才能变得更清晰?? 它的背景白色也不是完全白色 我的代码:
BarcodeInter25 code25 = new BarcodeInter25();
Rectangle r = new iTextSharp.text.Rectangle(38, 152);
code25.ChecksumText = false;
code25.Code = "some digits";
code25.BarHeight = 2
System.Drawing.Image i = code25.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
MemoryStream ms = new MemoryStream();
i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Image img = Image.GetInstance(ms.ToArray());
ms.Dispose();
您应该避免以任何方式调整大小。输出很可能是像素完美的,但是当你缩放它时 up/down 双线性过滤器会平滑它使其变得模糊。
我在 PDF 中嵌入 QRC 代码时遇到了完全相同的问题,并通过避免调整大小解决了这个问题。
如果您确实需要不同的尺寸,请使用正确的 interpolation algorithm.
在代码中以编程方式应用它看看你的代码,条形码模糊的原因应该很明显了。您将其转换为 System.Drawing.Image
(使其成为光栅图像),然后将其转换为 iTextSharp.text.Image
(但此时图像已经模糊)。
实现所需内容的正确方法是直接从条形码创建 iTextSharp.text.Image
(不要通过 System.Drawing.Image
)。可以这样做:
BarcodeInter25 code25 = new BarcodeInter25();
Rectangle r = new iTextSharp.text.Rectangle(38, 152);
code25.ChecksumText = false;
code25.Code = "some digits";
code25.BarHeight = 2;
PdfContentByte cb = writer.DirectContent;
Image img = code25.CreateImageWithBarcode(cb, null, null);
现在 Image
对象将不是光栅图像(带有使线条模糊的像素),而是真正的矢量图像(没有像素,但是 moveTo()
、lineTo()
和 stroke()
)。矢量数据的优点是它与分辨率无关:你可以随意放大和缩小,它总是很清晰。
这在 Chapter 10 of my book where you'll find the Barcodes example. In that chapter, you'll also discover the setFont()
中有解释(或在 iTextSharp 中 Font
属性)。我引用 API 文档:
public void setFont(BaseFont font)
Sets the text font.
Parameters:
font
- the text font. Set tonull
to suppress any text
所以如果你不想看到任何文字,你可以在上面的代码片段中添加以下行:
code25.Font = null;