我刚刚将ZXing.Net从版本0.16.6更新到0.16.9,目前,我遇到以下两个错误:
使用泛型类型“BarcodeWriter
找不到类型或命名空间名称“BitmapRenderer”(您是否缺少 using 指令或程序集引用?)
using System;
using System.Drawing;
using System.IO;
using ZXing;
using ZXing.QrCode.Internal;
using ZXing.Rendering;
namespace BarCode.Commons
{
public class BarCodeUtil
{
public static Bitmap GenerateBar(string text, int margin = 0, int width = 1000, int height = 500)
{
var bw = new BarcodeWriter(); // The first error is here
var encOptions = new ZXing.Common.EncodingOptions
{
Width = width + ( 2 * margin),
Height = height + ( 2 * margin),
Margin = margin,
PureBarcode = true
};
encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
bw.Renderer = new BitmapRenderer(); // The second error is here
bw.Options = encOptions;
bw.Format = BarcodeFormat.CODE_128;
Bitmap bm = bw.Write(text);
return bm;
}
}
}
对于第一个错误,看来我需要指定一种输出类型,我将其修改为以下命令:
var bw = new BarcodeWriter<Bitmap>();
但是,我不知道如何解决第二个错误。
我假设您使用的是.Net Core 或.Net 5.0 或更高版本。 在这种情况下,基本 nuget 包 ZXing.Net 不包含特定位图实现的 BarcodeWriter 实现。 默认情况下,只有旧的经典 .Net Framework 4.x 包含 Bitmap 类。
如果我的假设是正确的,您必须选择绑定包之一:https://www.nuget.org/packages?q=Zxing.Net.Binding 也许这个特定的应该适合您的需求: https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility (ZXing.Windows.Compatibility.BarcodeWriter) 将您的代码更改为
var bw = new ZXing.Windows.Compatibility.BarcodeWriter();
或在文件顶部添加以下“using”语句
using ZXing.Windows.Compatibility;