通过颜色为条形码位图着色

时间:2019-11-26 10:45:35

标签: c# zxing zxing.net

我正在使用ZXing.NET使用此代码生成条形码

time = Time.parse("2019-11-26T12:20:00.655+05:30")
pacific_time = time.in_time_zone("Pacific Time (US & Canada)")

# and then UTC if you want
pacific_time.utc

因此,当前每个条形码(以及带有文本的条形码)仅为黑色。有没有一种方法可以传递BarcodeWriter barcodeWriter = new BarcodeWriter { Format = BarcodeFormat, Options = new EncodingOptions { Width = barCodeWidth, Height = barCodeHeight, PureBarcode = !GenerateBarCodeWithText } }; Bitmap barCodeBitmap = barcodeWriter.Write(content); 对象来使条形码和文本变成红色?我尝试了这种骇人听闻的解决方案,以获取当前的像素颜色,检查它是否为白色,如果不是,则将其着色为指定的字体颜色。

Color

不幸的是,每个像素都会变色。

1 个答案:

答案 0 :(得分:1)

要为整个代码(包括文本)着色,可以使用以下代码段:

BarcodeWriter barcodeWriter = new BarcodeWriter
{
   Format = BarcodeFormat,
   Options = new EncodingOptions
   {
      Width = barCodeWidth,
      Height = barCodeHeight,
      PureBarcode = !GenerateBarCodeWithText
   },
   Renderer = new BitmapRenderer
   {
      Foreground = Color.Red
   }
};

Bitmap barCodeBitmap = barcodeWriter.Write(content);

如果您希望条形码和文本使用不同的颜色,则必须编写自己的渲染器实现,并使用它代替BitmapRenderer。 您可以查看BitmapRenderer的来源并将其用作自己的实现的模板:

https://github.com/micjahn/ZXing.Net/blob/master/Source/lib/renderer/BitmapRenderer.cs

例如添加一个新的属性“ TextColor”,并在行中使用它

var brush = new SolidBrush(Foreground);

更改为

var brush = new SolidBrush(TextColor);