详解免费开源的DotNet二维码操作组件ThoughtWorks.(2)

/// <summary> /// 生成二维码 /// </summary> /// <param>带生成二维码的字符串</param> /// <param>路径</param> /// <returns></returns> public static string CreatehoughtWorksQrCode(string content, string path) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } var qrCodeEncoder = new QRCodeEncoder { QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeScale = 4, QRCodeVersion = 8, QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M }; Image image = qrCodeEncoder.Encode(content); var filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg"; var filepath = string.Format("{0}{1}", path, filename); FileStream fs = null; try { fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (IOException ex) { throw new IOException(ex.Message); } finally { if (fs != null) fs.Close(); image.Dispose(); } return CodeDecoder(filepath); }

2.选择生成二维码的相关类型。

/// <summary> /// 选择生成二维码的相关类型 /// <param>要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param> /// <param>三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param> /// <param>大小:L M Q H</param> /// <param>版本:如 8</param> /// <param>比例:如 4</param> /// <returns></returns> /// </summary> public void CreateCode_Choose(string strData, string qrEncoding, string level, int version, int scale) { if (string.IsNullOrEmpty(strData)) { throw new ArgumentNullException(strData); } if (string.IsNullOrEmpty(qrEncoding)) { throw new ArgumentNullException(qrEncoding); } if (string.IsNullOrEmpty(level)) { throw new ArgumentNullException(level); } var qrCodeEncoder = new QRCodeEncoder(); var encoding = qrEncoding; switch (encoding) { case "Byte": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; case "AlphaNumeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; break; case "Numeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; break; default: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; } qrCodeEncoder.QRCodeScale = scale; qrCodeEncoder.QRCodeVersion = version; switch (level) { case "L": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; break; case "M": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; break; case "Q": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; break; default: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; break; } Image image = null; FileStream fs = null; try { //文字生成图片 image = qrCodeEncoder.Encode(strData); var filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg"; var filepath = HttpContext.Current.Server.MapPath(@"~\Upload") + "\\" + filename; fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (fs != null) fs.Close(); if (image != null) image.Dispose(); } }

3.二维码解码。

/// <summary> /// 二维码解码 /// </summary> /// <param>图片路径</param> /// <returns></returns> public static string CodeDecoder(string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(filePath); } try { if (!File.Exists(filePath)) return null; var myBitmap = new Bitmap(Image.FromFile(filePath)); var decoder = new QRCodeDecoder(); var decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap)); return decodedString; } catch (Exception ex) { throw new Exception(ex.Message); } }

四.总结:

跟以前介绍组件一样,首先是组件的概述,组件的核心类,组件的使用方法,这些在这个组件时,找改组件的相关概述时,花了不少时间,也不知道为何,这个组件没有找到相关的资料,甚至连作者都是以某某某代替,但是互联网就是如此,我们不需要知道是谁制造的,只要用起来方便就可以。在生成二维码的组件和js插件中,我个人还是喜欢这个组件的,感觉很不错,任何组件和方法都是有个人偏好和使用环境,读者可以自行根据情况选择。

由于开发者提供了一个demo,可以直接进入上面的链接中查看下载,在这里就不做一个示例介绍。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjywwz.html