效果图:
添加引用,添加生成页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>二维码生成工具</title>
<script type="text/javascript" src="../Jscript/admin/jquery-1.4.2.js"></script>
<style type="text/css">
.style1
{
width: 100%; min-width:800px;
text-align: left;
}
.txt
{
width: 40%;
}
</style>
</head>
<body>
<div>
<table class="style1">
<tr>
<td style="text-align:right; width:20%;">
输入要生成二维码的内容:
</td>
<td>
<input type="text" id="txt_qr" name="txt_qr" value="https://www.zhengdecai.com/" class="txt" /> <span>请使用生成“<a href="BarCode.aspx">条形码</a>”</span>
</td>
</tr>
<tr>
<td style="text-align:right;">
二维码图片:
</td>
<td>
<img id="qrimg" alt="二维码图片" src="../images/20130424105224.jpg" />
</td>
</tr>
<tr>
<td style="text-align:right;">
尺寸大小:
</td>
<td>
<input id="txt_size" type="text" value="4" class="txt" />
</td>
</tr>
<tr>
<td colspan="4">
<div style=" text-align:left; padding-left:200px; margin-top:50px;"><input type="button" onclick="getQrImg();" value="生成二维码" /></div>
</td>
</tr>
</table>
</div>
</body>
</html>
<script type="text/javascript">
function getQrImg() {
var txt_qr = escape($.trim($("#txt_qr").val()));
var txt_size = $("#txt_size").val(); ;
$.ajax({
type: "GET",
data: "cmd=set&txt_qr=" + txt_qr + "&txt_size=" + txt_size,
url: "../AjaxUrl/Admin/QrCode.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", "../File/" + dataObj[0].list[0].imgurl);
return false;
},
error: function (request, message, ex) {
alert("错误:" + message);
}
});
}
</script>
最后添加一般处理程序内容如下:
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 = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
filepath = context.Server.MapPath(@"~\File") + "\\" + 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\":\"" + filename + "\",\"qrtext\":\"" + qrdecode + "\"}]}]");
}
}
else
{
context.Response.Write("[{\"count\":0,\"list\":[{\"error\":\"没有上传文件\"}]}]");
}
break;
case "set":
string txt_qr = ConverToGB(context.Request["txt_qr"].ToString().Trim(), 16);
string qrEncoding = "Byte";
string Level = "M";
string txt_ver = "7";
string txt_size = context.Request["txt_size"].ToString();
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
String encoding = qrEncoding;
if (encoding == "Byte")
{
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
}
//else if (encoding == "AlphaNumeric")
//{
// qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
//}
//else if (encoding == "Numeric")
//{
// qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
//}
try
{
int scale = Convert.ToInt16(txt_size);
qrCodeEncoder.QRCodeScale = scale;
}
catch (Exception ex)
{
return;
}
try
{
int version = Convert.ToInt16(txt_ver);
qrCodeEncoder.QRCodeVersion = version;
}
catch (Exception ex)
{
return;
}
string errorCorrect = Level;
if (errorCorrect == "L")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
//else if (errorCorrect == "M")
// qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//else if (errorCorrect == "Q")
// qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
//else if (errorCorrect == "H")
// qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
Image image;
String data = txt_qr;
image = qrCodeEncoder.Encode(data);
filename = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
filepath = context.Server.MapPath(@"~\File") + "\\" + filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();
context.Response.Write("[{\"count\":1,\"list\":[{\"imgurl\":\"" + filename + "\"}]}]");
break;
}
}
/// <summary>
/// 10进制或16进制转换为中文
/// </summary>
/// <param name="name">要转换的字符串</param>
/// <param name="fromBase">进制(10或16)</param>
/// <returns></returns>
public string ConverToGB(string text, int fromBase)
{
string value = text;
MatchCollection mc;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
switch (fromBase)
{
case 10:
MatchCollection mc1 = Regex.Matches(text, @"&#([\d]{5})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match _v in mc1)
{
string w = _v.Value.Substring(2);
w = Convert.ToString(int.Parse(w), 16);
byte[] c = new byte[2];
string ss = w.Substring(0, 2);
int c1 = Convert.ToInt32(w.Substring(0, 2), 16);
int c2 = Convert.ToInt32(w.Substring(2), 16);
c[0] = (byte)c2;
c[1] = (byte)c1;
sb.Append(Encoding.Unicode.GetString(c));
}
value = sb.ToString();
break;
case 16:
mc = Regex.Matches(text, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (mc != null && mc.Count > 0)
{
foreach (Match m2 in mc)
{
string v = m2.Value;
string w = v.Substring(2);
byte[] c = new byte[2];
int c1 = Convert.ToInt32(w.Substring(0, 2), 16);
int c2 = Convert.ToInt32(w.Substring(2), 16);
c[0] = (byte)c2;
c[1] = (byte)c1;
sb.Append(Encoding.Unicode.GetString(c));
}
value = sb.ToString();
}
break;
}
return value;
}
评论列表: