ASP.NET实现二维码(QRCode)的创建和读取
欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入
一、项目引用QRCode的DLL文件(ThoughtWorks.QRCode.dll)
二、ASPX页面(两个jquery的js文件请自行去官网下载):
[html]
<html xmlns="http://http://www.zjjv.com///1999/xhtml">
<head runat="server">
<title>二维码工具测试</title>
<script type="text/javascript" src="//Scripts/Jquery/jquery-1.6.2.js"></script>
<script type="text/javascript" src="//Scripts/Jquery/jquery.form.js"></script>
<script type="text/javascript" src="js/test.js"></script>
<style type="text/css">
.style1
{
width: 100%;
}
#txt_qr
{
width: 632px;
}
</style>
</head>
<body>
<div>
<table>
<tr>
<td>
输入文字:
</td>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>
二维码图片
</td>
<td>
<img alt="二维码图片" />
</td>
</tr>
<tr>
<td>
生成选项
</td>
<td>
Encoding:<select>
<option value="Byte">Byte</option>
<option value="AlphaNumeric">AlphaNumeric</option>
<option value="Numeric">Numeric</option>
</select>
Correction Level:<select>
<option value="M">M</option>
<option value="L">L</option>
<option value="Q">Q</option>
<option value="H">H</option>
</select>
Version:<input type="text" value="7" />(1-40) Size:<input
type="text" value="4" />
</td>
</tr>
<tr>
<td colspan="4">
<input type="button" value="生成二维码" />
</td>
</tr>
<tr>
<td>
<form action="Ashx/test.ashx" method="post" enctype="multipart/form-data">
<input type="file" /><input type="submit" value="读取二维码" />
</form>
</td>
<td colspan="1">
<img alt="要读取的图片" /><br />
<input type="text" />
</td>
</tr>
</table>
</div>
</body>
</html>
三、test.js文件
[javascript]
$(document)。ready(function ()
{
var options = {
beforeSubmit: showRequest,
success: showResponse,
dataType: 'json',
clearForm: true,
error: function (request, message, ex)
{
alert('错误:' + message);
}
};
$('#qrForm')。ajaxForm(options);
});
function showRequest(formData, jqForm, options)
{
return true;
}
function showResponse(responseText, statusText, xhr, $form)
{
if (responseText[0].count == 0)
{
alert(responseText[0].list[0].error);
return false;
}
$("#img_qr")。attr("src", responseText[0].list[0].imgurl);
$("#txt_readqr")。val(responseText[0].list[0].qrtext);
return false;
}
function getQrImg()
{
var txt_qr = escape($.trim($("#txt_qr")。val()));
var qrEncoding = $("#Encoding")。val(); ;
var Level = $("#Level")。val(); ;
var txt_ver = $("#txt_ver")。val(); ;
var txt_size = $("#txt_size")。val(); ;
$.ajax({
type: "GET",
data: "cmd=set&txt_qr=" + txt_qr + "&qrEncoding=" + qrEncoding + "&Level=" + Level + "&txt_ver=" + txt_ver + "&txt_size=" + txt_size,
url: "Ashx/test.ashx",
dataType: 'text',
beforeSend: function (x)
{
x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
},
success: function (json)
{
var dataObj = eval(json);
$("#qrimg")。attr("src", dataObj[0].list[0].imgurl);
return false;
},
error: function (request, message, ex)
{
alert("错误:" + message);
}
});
}
四、test.ashx,没有判断目录是否存在等问题,请自行建立或者更改代码。
[csharp]
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Text.RegularExpressions;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
public class test : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string cmd = context.Request["cmd"] == null ? "get" : context.Request["cmd"].ToString();
string filename = string.Empty;
string filepath = string.Empty;
switch (cmd)
{
case "get":
if (context.Request.Files.Count > 0)
{
for (int j = 0; j < context.Request.Files.Count; j++)
{
filename = Guid.NewGuid()。ToString() + "_tmp.jpg";
filepath = context.Server.MapPath(@"~\Utilty\QRCode\upload") + "\\" + filename;
string qrdecode = string.Empty;
HttpPostedFile uploadFile = context.Request.Files[j];
uploadFile.SaveAs(filepath);
QRCodeDecoder decoder = new QRCodeDecoder();
Bitmap bm = new Bitmap(filepath);
qrdecode = decoder.decode(new QRCodeBitmapImage(bm));
bm.Dispose();
context.Response.Write("[{\"count\":1,\"list\":[{\"imgurl\":\"upload/" + filename + "\",\"qrtext\":\"" + qrdecode + "\"}]}]");
}
}
[1][2]下一页
【责编:peter】