APS.NET MVC4生成二维码简单解析
程序员文章站
2023-12-21 19:29:34
一、视图
@{
layout = null;
}
一、视图
@{ layout = null; } <!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>index</title> <script src="~/scripts/jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function () { //生成 $('#createbtn').click(function () { createimage(); }); //解析 $('#analysisbtn').click(function () { analysisimage(); }); }); //生成二维码 function createimage() { var content = $('#content').val(); //ajax开始 $.post("/orcode/getorimage/", { "content": content }, function (data) { $('#imgdiv').empty(); $('#imgdiv').append('<img id="orimage" />'); $("#orimage").attr("src", data); }); //ajax结束 } //解析二维码 function analysisimage() { //获取图片名称 var imageurl = $('#orimage').attr("src"); var array = imageurl.split('/'); var imagename = array[array.length - 1]; //如:123.jpg //ajax开始 $.post("/orcode/getorimagecontent/", { "imagename": imagename }, function (data) { $('#orcodecontent').empty(); $('#orcodecontent').text(data); }); //ajax结束 } </script> </head> <body> <div style="text-align:center;"> <div> <textarea style="width:230px;height:120px;" id="content"></textarea> <br /> <input id="createbtn" type="button" value="生成二维码" /> </div> <div> <textarea style="width:230px;height:120px;" id="orcodecontent"></textarea> <br /> <input id="analysisbtn" type="button" value="解析二维码" /> </div> <div style="margin-top:20px;" id="imgdiv"> </div> </div> </body> </html>
二、控制器
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using common; using system.drawing; namespace qrcodedemo.controllers { public class orcodecontroller : controller { // // get: /orcode/ public actionresult index() { return view(); } [httppost] public actionresult getorimage(string content) { //if (string.isnullorempty(content)) //{ // return content(""); //} string timestr = datetime.now.tofiletime().tostring(); bitmap bitmap = qrcodeop.qrcodeencoderutil(content); string filename = server.mappath("~") + "content\\images\\qrimages\\" + timestr + ".jpg"; bitmap.save(filename);//保存位图 string imageurl = "/content/images/qrimages/" + timestr + ".jpg";//显示图片 return content(imageurl); } [httppost] public actionresult getorimagecontent(string imagename) { string fileurl = server.mappath("~") + "content\\images\\qrimages\\" + imagename; bitmap bitmap = new bitmap(fileurl); string content = qrcodeop.qrcodedecoderutil(bitmap); return content(content); } } }
三、二维码生成工具类
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using thoughtworks.qrcode.codec; using thoughtworks.qrcode.codec.data; using system.drawing; namespace common { public class qrcodeop { /// <summary> /// 生成二维码 /// </summary> /// <param name="qrcodecontent">要编码的内容</param> /// <returns>返回二维码位图</returns> public static bitmap qrcodeencoderutil(string qrcodecontent) { qrcodeencoder qrcodeencoder = new qrcodeencoder(); qrcodeencoder.qrcodeversion = 0; bitmap img = qrcodeencoder.encode(qrcodecontent, encoding.utf8);//指定utf-8编码, 支持中文 return img; } /// <summary> /// 解析二维码 /// </summary> /// <param name="bitmap">要解析的二维码位图</param> /// <returns>解析后的字符串</returns> public static string qrcodedecoderutil(bitmap bitmap) { qrcodedecoder decoder = new qrcodedecoder(); string decodedstring = decoder.decode(new qrcodebitmapimage(bitmap), encoding.utf8);//指定utf-8编码, 支持中文 return decodedstring; } } }
以上就是简单解析了aps.net mvc4下二维码的生成过程,希望对大家的学习有所帮助,制作属于自己的二维码。
下载源码:aps.net mvc4二维码