c#中生成和解析条形码和二维码的组件有很多,这里推荐使用ZXing组件,都可以实现。今天就来实现这个功能,界面如下所示。
调用代码如下,比较简单,就不过多描述了
public partial class FormGeneQRCode : FormBase { public FormGeneQRCode() { InitializeComponent(); } public override void LoadData() { //设置页面标题 this.SetFormTitle(this.Text); ////加载工具栏 //List<ToolItemModel> toolItems = new List<ToolItemModel>(); //toolItems.Add(new ToolItemModel() { ButtonID = "Close", ButtonText = "关闭", ButtonParentID = "Root", EditType = ToolItemEditType.Button.ToString(), ImageKey = IconName.ToolItemClose.ToString(), HAlign = HAlignment.Right.ToString() }); //FormUIHelper.CreateToolBar(this, this.toolBar, toolItems, this.ImageList16, false); //绑定事件 this.fmImage.ButtonClick += this.OnDevButtonEditButtonClickEventHandler; this.btnGeneCode.Click += this.OnDevButtonClickEventHandler; this.btnDecodeCode.Click += this.OnDevButtonClickEventHandler; } private void FormGeneQRCode_Load(object sender, EventArgs e) { this.LoadData(); } public override void OnDevButtonEditButtonClickEventHandler(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { ButtonEdit btnEdit = (ButtonEdit)sender; if (btnEdit.Name == this.fmImage.Name) { string imageFileName = FormUIHelper.OpenFileDialog(false, FileExpandName.jpeg.ToString()); this.fmImage.Text = imageFileName; } } public override void OnDevButtonClickEventHandler(object sender, EventArgs e) { SimpleButton btn = (SimpleButton)sender; if (btn.Name == this.btnGeneCode.Name) { //获取生成编码类型 string geneCodeType = FormUIHelper.GetRadioButtonListSelectedValue(this.fmGeneCodeType); if (geneCodeType == "BarCode") { string content = this.fmWord.Text.Trim(); Bitmap bitmap = QRCodeUtil.GeneBarCode(content); this.pictureEdit1.Image = bitmap; } else if (geneCodeType == "QRCode") { string content = this.fmWord.Text.Trim(); if (string.IsNullOrEmpty(content)) return; Bitmap qrCode = QRCodeUtil.GeneQRCode(content); this.pictureEdit1.Image = qrCode; } else if (geneCodeType == "QRCodeWithImage") { string content = this.fmWord.Text.Trim(); if (string.IsNullOrEmpty(content)) return; string imageFileName = this.fmImage.Text; if (string.IsNullOrEmpty(imageFileName)) return; Image image = Image.FromFile(imageFileName); Bitmap qrCodeWithImage = QRCodeUtil.GeneQRCodeWithImage(content, image); this.pictureEdit1.Image = qrCodeWithImage; } } else if (btn.Name == this.btnDecodeCode.Name) { string geneCodeType = FormUIHelper.GetRadioButtonListSelectedValue(this.fmGeneCodeType); if (geneCodeType == "BarCode") return; string content = QRCodeUtil.DecodeQRCode((Bitmap)this.pictureEdit1.Image); FormUIHelper.ShowMessage(content); } } public override void OnToolItemButtonClickEventHandler(object sender, EventArgs e) { //参数检测 if (sender == null) return; ToolStripItem tsi = (ToolStripItem)sender; //首先执行父级事件.若执行过则子级不再执行 base.OnToolItemButtonClickEventHandler(sender, e); if (this.ActionIsExecuted == true) return; object tag = tsi.Tag; if (StringHelper.ObjectIsNullOrEmpty(tag)) tag = ""; string s = tag.ToString().Trim().ToLower(); if (s == "close") { this.Close(); } } }
用到的QRCodeUtil类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.QrCode.Internal; namespace Tutorial.Core { public class QRCodeUtil { /// <summary> /// 生成条形码 /// </summary> /// <returns></returns> public static Bitmap GeneBarCode(string content) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException("content"); } EncodingOptions options = new EncodingOptions() { Width = 300, Height = 300 }; BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.EAN_13; writer.Options = options; Bitmap bitmap = writer.Write(content); return bitmap; } /// <summary> /// 生成二维码图片 /// 参考地址:http://www.cnblogs.com/Interkey/p/qrcode.html /// </summary> /// <param name="contents">要生成二维码包含的信息</param> /// <param name="width">生成的二维码宽度(默认300像素)</param> /// <param name="height">生成的二维码高度(默认300像素)</param> /// <returns>二维码图片</returns> public static Bitmap GeneQRCode(string contents, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return null; } EncodingOptions options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = width, Height = height, ErrorCorrection = ErrorCorrectionLevel.H, }; BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write(contents); return bitmap; } /// <summary> /// 解析条形码 /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static string DecodeBarCode(Bitmap bitmap) { BarcodeReader reader = new BarcodeReader(); reader.Options.CharacterSet = "UTF-8"; Result result = reader.Decode(new Bitmap(bitmap)); string barCode = result.ToString(); return barCode; } /// <summary> /// 生成中间带有图片的二维码图片 /// 参考地址:http://www.cnblogs.com/Interkey/p/qrcode.html /// </summary> /// <param name="contents">要生成二维码包含的信息</param> /// <param name="middleImg">要生成到二维码中间的图片</param> /// <param name="width">生成的二维码宽度(默认300像素)</param> /// <param name="height">生成的二维码高度(默认300像素)</param> /// <returns>中间带有图片的二维码</returns> public static Bitmap GeneQRCodeWithImage(string contents, Image middleImg, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return null; } if (middleImg == null) { //return null; return GeneQRCode(contents); } //本文地址:http://www.cnblogs.com/Interkey/p/qrcode.html //构造二维码写码器 MultiFormatWriter mutiWriter = new MultiFormatWriter(); Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, 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; } /// <summary> /// 解析二维码 /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static string DecodeQRCode(Bitmap bitmap) { IBarcodeReader reader = new BarcodeReader(); Result result = reader.Decode(bitmap); string codeType = result.BarcodeFormat.ToString(); string codeContent = result.Text; return codeContent; } } }
相关阅读
文章评论