C# 生成二维码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 生成二维码相关的知识,希望对你有一定的参考价值。
项目引用引用ThoughtWorks.QRCode.dll 类
新建类QrCodeHelper
引用
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
创建生成二维码的方法CreateEQcoder
1 /// <summary> 2 /// 生成二维码 3 /// </summary> 4 /// <param name="qrCodeScale">尺寸4-15</param> 5 /// <param name="qrCodeVersion">复杂级别3-12</param> 6 /// <param name="qrCodeErrorCorrect">容错量"H","L","M","Q"</param> 7 /// <param name="url">二维码信息</param> 8 /// <param name="uId">用户ID(唯一标识)</param> 9 /// <returns></returns> 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 try 39 { 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 finally 53 { 54 bmPhoto.Dispose(); 55 } 56 }
解析二维码
QRCodeDecoder decoder = new QRCodeDecoder(); String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(bmPhoto))); string Text = decodedString;
以上是关于C# 生成二维码的主要内容,如果未能解决你的问题,请参考以下文章