项目引用引用ThoughtWorks.QRCode.dll 类
新建类QrCodeHelper
引用
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
创建生成二维码的方法CreateEQcoder
1 ///2 /// 生成二维码 3 /// 4 /// 尺寸4-15 5 /// 复杂级别3-12 6 /// 容错量"H","L","M","Q" 7 /// 二维码信息 8 /// 用户ID(唯一标识) 9 ///10 public static string CreateEQcoder(int qrCodeScale, int qrCodeVersion, string qrCodeErrorCorrect, string url, string uId)11 {12 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();13 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;14 qrCodeEncoder.QRCodeScale = qrCodeScale;15 qrCodeEncoder.QRCodeVersion = qrCodeVersion;16 switch (qrCodeErrorCorrect)17 {18 case "H":19 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;20 break;21 case "L":22 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;23 break;24 case "M":25 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;26 break;27 case "Q":28 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;29 break;30 }31 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;32 Bitmap bmPhoto = qrCodeEncoder.Encode(url, System.Text.Encoding.GetEncoding("UTF-8"));33 string fileName = DateTime.Now.ToString("yyyy") + ".jpg";34 string savePath = "/Tempuploads/" + uId + "/QRCode/";35 36 string dirPath = System.Web.HttpContext.Current.Server.MapPath(savePath);37 38 try39 {40 if (!Directory.Exists(dirPath))41 {42 Directory.CreateDirectory(dirPath);43 }44 bmPhoto.Save(System.Web.HttpContext.Current.Server.MapPath("/") + savePath + fileName, ImageFormat.Jpeg);45 bmPhoto.Dispose();46 return savePath + fileName;47 }48 catch (Exception)49 {50 return "";51 }52 finally53 {54 bmPhoto.Dispose();55 }56 }
解析二维码
QRCodeDecoder decoder = new QRCodeDecoder(); String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(bmPhoto))); string Text = decodedString;