您好,登錄后才能下訂單哦!
在現在的項目中,較多的使用到二維碼,現在介紹一個第三方組件,用于生成二維碼。
生成二維碼:
/// <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 p_w_picpath = 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); p_w_picpath.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (IOException ex) { throw new IOException(ex.Message); } finally { if (fs != null) fs.Close(); p_w_picpath.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 p_w_picpath = null; FileStream fs = null; try { //文字生成圖片 p_w_picpath = 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); p_w_picpath.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (IOException ex) { throw new IOException(ex.Message); } finally { if (fs != null) fs.Close(); if (p_w_picpath != null) p_w_picpath.Dispose(); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。