引用ThoughtWorks.QRCode.dll (源代码里有)
1、简单二维码生成及解码代码:
//生成二维码方法一 private void CreateCode_Simple(string nr) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 8; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //System.Drawing.Image image = qrCodeEncoder.Encode("4408810820 深圳-广州 小江"); System.Drawing.Image image = qrCodeEncoder.Encode(nr); string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg"; string filepath = Server.MapPath(@"~\Upload") + "\\" + filename; System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); //二维码解码 var codeDecoder = CodeDecoder(filepath); } ////// 二维码解码 /// /// 图片路径 ///public string CodeDecoder(string filePath) { if (!System.IO.File.Exists(filePath)) return null; Bitmap myBitmap = new Bitmap(Image.FromFile(filePath)); QRCodeDecoder decoder = new QRCodeDecoder(); string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap)); return decodedString; }
2、源代码演示,相对复杂的生成方式:
界面效果:
前端代码:
@{ Layout = null;}Code 要生成的文字(支持中文):
Encoding:
Correction Level:
Version:
scale:
后台:
/// 生成二维码 /// /// 要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444 /// 三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC /// 大小:L M Q H /// 版本:如 8 /// 比例:如 4 ///public ActionResult CreateCode_Choose(string strData, string qrEncoding, string level, int version, int scale) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); string 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 = qrCodeEncoder.Encode(strData); string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg"; string filepath = Server.MapPath(@"~\Upload") + "\\" + filename; //如果文件夹不存在,则创建 //if (!Directory.Exists(filepath)) // Directory.CreateDirectory(filepath); System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); return Content(@"/Upload/" + filename); }
源代码演示下载: