.NET使用ZXing.NET生成中间带图片和不带图片的二维码
基于开源的 ZXing.NET 组件,代码如下:
1、先添加对ZXing.NET的引用,然后添加相应的命名空间:
using System; using System.Collections.Generic; using System.Drawing; using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.QrCode.Internal;
2、生成二维码方法类:
public class QrCodeHelper { #region 方法一是生成中间不带图片的二维码图片 ////// 生成二维码图片 /// /// 要生成二维码包含的信息 /// 生成的二维码宽度(默认300像素) /// 生成的二维码高度(默认300像素) /// 二维码图片 public static Bitmap GeneratorQrImage(string contents, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return null; } //本文地址:http://www.cnblogs.com/Interkey/p/qrcode.html EncodingOptions options = null; BarcodeWriter writer = null; options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = width, Height = height, ErrorCorrection = ErrorCorrectionLevel.H, }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write(contents); return bitmap; } #endregion #region 方法二生成中间带图片的二维码图片 /// /// 生成中间带有图片的二维码图片 /// /// 要生成二维码包含的信息 /// 要生成到二维码中间的图片 /// 生成的二维码宽度(默认300像素) /// 生成的二维码高度(默认300像素) /// 中间带有图片的二维码 public static Bitmap GeneratorQrImage(string contents, Image middleImg, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return null; } if (middleImg == null) { //return null; return GeneratorQrImage(contents); } //构造二维码写码器 MultiFormatWriter mutiWriter = new MultiFormatWriter(); Dictionary object> hint = new Dictionary object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap bitmap = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width); int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height); int middleImgL = (bitmap.Width - middleImgW) / 2; int middleImgT = (bitmap.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bitmap, 0, 0); } //在二维码中插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH); return bmpimg; } #endregion }
3、简单方法调用实例:
#region 生成二维码 public ActionResult QrCode() { String url = "https://www.cnblogs.com/sharing1986687846"; Bitmap barcodeBitmap = (Bitmap)Image.FromFile(@"G:\MyWeb\Web学习\Windows\LiveProject\LiveProject\Images\panda.png"); Bitmap noImgCrCodeBmp= QrCodeHelper.GeneratorQrImage(url); //生成中间不带图的二维码 Bitmap imgCrCodeBmp= QrCodeHelper.GeneratorQrImage(url, barcodeBitmap); //生成中间带图的二维码 MemoryStream ms=new MemoryStream(); String imgStr = String.Empty; imgStr += String.Format("
二维码对应内容:{0}\" />", url); noImgCrCodeBmp.Save(ms, ImageFormat.Png); string base64 = Convert.ToBase64String(ms.ToArray()); imgStr += String.Format("
中间不带图:
", base64); MemoryStream ms1 = new MemoryStream(); imgCrCodeBmp.Save(ms1, ImageFormat.Png); base64 = Convert.ToBase64String(ms1.ToArray()); imgStr += String.Format("
中间带图:
", base64); return Content(imgStr); } #endregion
效果图如下:
4、中间有图片与没有图片的生成的不一样,原因不清楚,初步以为中间有图片的是因为容错率的原故。
对应的解析代码也很简单:
// create a barcode reader instance IBarcodeReader reader = new BarcodeReader(); // load a bitmap Bitmap barcodeBitmap = (Bitmap)Image.FromFile("C:\\sample-barcode-image.png"); // detect and decode the barcode inside the bitmap Result result = reader.Decode(barcodeBitmap); // do something with the result if (result != null) { this.txtDecoderType.Text = result.BarcodeFormat.ToString(); this.txtDecoderContent.Text = result.Text; }
结语:
其他常用可生成二维码的组件有:QrCode.Net(开源)、ZXing(开源)、ThoughtWorks.QRCode等。
了解了二维码的数据存储后,可以基于这些技术生成富有创意的二维码。
posted @
2019-01-18 10:41 波霸38 阅读(
...) 评论(
...) 编辑 收藏