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生成二维码的详细内容,更多关于c#生成二维码的资料请关注我们其它相关文章!
赞 (0)