我将使用ZXing.Net库(0.16.4版)来编码和解码QR码。我从这里获得了如何解码QR码的参考:C# with ZXing.Net: Decoding the QR code
代码:
Bitmap image = (Bitmap)Bitmap.FromFile(@"file.png");
byte[] bytes = File.ReadAllBytes(@"file.png");
try
{
using (image)
{
LuminanceSource source;
source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
//... code found
var data = result.Text.Split(Environment.NewLine);
}
else
{
//... no code found
}
}
}
catch (Exception exception)
{
throw new Exception("Cannot decode the QR code " + exception.Message);
}
这里的代码在BitmapLuminanceSource上抛出了编译时错误。
类型或命名空间名称“BitmapLuminanceSource”找不到(您是否缺少using指令或程序集引用?
我已经在这里安装了ZXing.Net包,但无法理解为什么这个类引用在此处不起作用。
为使代码正常工作,我从以下位置复制了此类: https://github.com/micjahn/ZXing.Net/blob/master/Source/lib/BitmapLuminanceSource.cs
var bitMap = (System.DrawingCore.Bitmap)System.DrawingCore.Bitmap.FromStream(stream); var reader = new ZXing.ZKWeb.BarcodeReader(); var result = reader.Decode(bitMap);
- Michael