您好,登錄后才能下訂單哦!
小編給大家分享一下C#用ThoughtWorks生成二維碼的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在現(xiàn)在的項目中,較多的使用到二維碼,前面介紹過一篇使用Gma生成二維碼的操作,現(xiàn)在介紹一個第三方組件,主要介紹生成二維碼,二維碼的解析,以及對二維碼的相關信息的選擇,現(xiàn)在介紹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.選擇生成的二維碼參數(shù):
/// <summary> /// 選擇生成二維碼的相關類型 /// <param name="strData">要生成的文字或者數(shù)字,支持中文。如: "4408810820 深圳-廣州" 或者:4444444444</param> /// <param name="qrEncoding">三種尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param> /// <param name="level">大?。篖 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生成二維碼的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。