小编给大家分享一下C#用ThoughtWorks生成二维码的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
在现在的项目中,较多的使用到二维码,前面介绍过一篇使用Gma生成二维码的操作,现在介绍一个第三方组件,主要介绍生成二维码,二维码的解析,以及对二维码的相关信息的选择,现在介绍ThoughtWorks用于生成二维码,本随笔采用.net4.5和C#6.0语法,也可以进行查看ThoughtWorks的底层源码。
1.生成二维码:
/// <summary> /// 生成二维码 /// </summary> /// <param name="content">带生成二维码的字符串</param> /// <param name="path">路径</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> /// 二维码解码 /// </summary> /// <param name="filePath">图片路径</param> /// <returns></returns> public static string CodeDecoder(string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(filePath); } try { if (!System.IO.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); } }
3.选择生成的二维码参数:
/// <summary> /// 选择生成二维码的相关类型 /// <param name="strData">要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param> /// <param name="qrEncoding">三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param> /// <param name="level">大小:L M Q H</param> /// <param name="version">版本:如 8</param> /// <param name="scale">比例:如 4</param> /// <returns></returns> /// </summary> public void CreateCode_Choose(string strData, string qrEncoding, string level, int version, int scale) { 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 ex) { throw new IOException(ex.Message); } finally { if (fs != null) fs.Close(); if (image != null) image.Dispose(); } }
以上是C#用ThoughtWorks生成二维码的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。