1、创建项目,并引入QRCoder包

2、输出一个简单的二维码到控制台:.
QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);AsciiQRCode qrCode = new AsciiQRCode(qrCodeData);string qrCodeAsAsciiArt = qrCode.GetGraphic(1);Console.WriteLine(qrCodeAsAsciiArt);
3、扫码测试,使用微信扫码,也可以扫出hello,world的结果

手机扫码效果:

3、一般情况下,可能需要保存为图片,接下来换个写法,保存到本地图片:
static void GenerateQRCodeAndSave(string data, string outputPath){QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);PngByteQRCode qrCode = new PngByteQRCode(qrCodeData);byte[] qrCodeAsPngByteArr = qrCode.GetGraphic(20);using (MemoryStream stream = new MemoryStream(qrCodeAsPngByteArr)){var bitmap = new Bitmap(stream);bitmap.Save(outputPath);}}
在根目录下就有了一个二维码图像文件:

也可以用微信扫码扫出Hello World
4、使用XZING识别二维码信息
引入ZXING.net包,然后建一个BitmapLuminanceSource类(如果新的包没有这个类的话):
public class BitmapLuminanceSource : BaseLuminanceSource{public BitmapLuminanceSource(Bitmap bitmap) : base(bitmap.Width, bitmap.Height){var lockData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);var stride = Math.Abs(lockData.Stride);var pixelCount = stride * Height;var pixels = new byte[pixelCount];System.Runtime.InteropServices.Marshal.Copy(lockData.Scan0, pixels, 0, pixelCount);bitmap.UnlockBits(lockData);for (int y = 0; y < Height; y++){var offset = y * stride;for (int x = 0; x < Width; x++){var idx = offset + x * 4;var luminance = (byte)(pixels[idx + 2] * 0.3 + pixels[idx + 1] * 0.59 + pixels[idx] * 0.11);luminances[y * Width + x] = luminance;}}}protected override LuminanceSource CreateLuminanceSource(byte[] newLuminances, int width, int height){throw new NotImplementedException();}}
再新建一个使用XZING.NET识别二维码的方法做测试:
static string DecodeQRCodeByZXing(string imagePath){var barcodeReader = new BarcodeReader<Bitmap>(bitmap => new BitmapLuminanceSource(bitmap));var result = barcodeReader.Decode(new Bitmap(imagePath));if (result != null)return result.Text;return "没得码.";}
把上面QRCoder生成的图片,重命名为hello,然后运行程序测试一下:

5、使用OpenCV库进行识别
引入OpenCV的两个包,Emgu.CV 和 Emgu.CV.runtime.windows

编写基于OpenCV的识别方式的方法:
public static string DecodeQRCodeByOpenCv(string imagePath){using (Image<Bgr, byte> img = new Image<Bgr, byte>(imagePath)){using (QRCodeDetector qrdetector = new QRCodeDetector()){string decodedInfo = "";Mat points = new Mat();if (qrdetector.Detect(img, points)){Mat straightQrCode = new Mat();decodedInfo = qrdetector.Decode(img, points, straightQrCode);straightQrCode.Dispose();}points.Dispose();return decodedInfo;}}}
运行并测试:

6、基于ZBar的方式
引入ZBar包

编写基于zbar的方法代码
public static string DecodeQRCodeByZbar(string imagePath){string res = string.Empty;using (var scanner = new ZBar.ImageScanner()){var bitmap = (Bitmap)Image.FromFile(imagePath);var symbols = scanner.Scan(bitmap);foreach (var symbol in symbols){res += symbol.Data;}}return res;}
运行并测试:

以上就是该文章的全部内容。