方法1、jquery
/// <summary> /// 將信息轉化爲二維碼位圖 /// </summary> /// <param name="barcodeText">轉化內容</param> /// <returns>轉化後的二維碼位圖</returns> public void BarcodeImage(String barcodeText) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeVersion = 0; Bitmap img = qrCodeEncoder.Encode(barcodeText, Encoding.UTF8);//指定utf-8編碼, 支持中文 string httpFileName = @"C:\Users\Administrator\Desktop\ConsoleApplication1\MvcApplication2\"; string fileName = @"\Content\image\" + 123456 + ".jpg"; img.Save(httpFileName + fileName);//保存位圖 } /// <summary> /// 解析二維碼 /// </summary> /// <param name="bitmap">要解析的二維碼位圖</param> /// <returns>解析後的字符串</returns> public string QRCodeDecoderUtil(Bitmap bitmap) { QRCodeDecoder decoder = new QRCodeDecoder(); string decodedString = decoder.decode(new QRCodeBitmapImage(bitmap), Encoding.UTF8) ;//指定utf-8編碼, 支持中文 return decodedString; }
方法2、編碼
後臺:spa
public FileContentResult BarcodeImage(String barcodeText) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeVersion = 0; Bitmap img = qrCodeEncoder.Encode(barcodeText, Encoding.UTF8);//指定utf-8編碼, 支持中文 //string httpFileName = @"C:\Users\Administrator\Desktop\ConsoleApplication1\MvcApplication2\"; //string fileName = @"\Content\image\" + 123456 + ".jpg"; //img.Save(httpFileName + fileName);//保存位圖 System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return File(ms.GetBuffer(), "image/JPEG"); }
前臺:code
<script src="~/script/jquery-1.8.2.js"></script> <img src="#" alt="" class="code-img" id="validImg"> <input type="text" id="txt" /> <button id="subId">sub</button> <script> $("#subId").click(function () { $("#validImg").attr("src", "/Home/BarcodeImage?barcodeText=" + $("#txt").val()); }) </script>