引用ThoughtWorks.QRCode.dll (源代码里有)
1、简单二维码生成及解码代码://生成二维码方法一PRivatevoidCreateCode_Simple(stringnr)
{
QRCodeEncoder qrCodeEncoder=newQRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale=4;
qrCodeEncoder.QRCodeVersion=8;
qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.M;//System.Drawing.Image image = qrCodeEncoder.Encode("4408810820 深圳-广州 小江");System.Drawing.Image image =qrCodeEncoder.Encode(nr);stringfilename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() +".jpg";stringfilepath = Server.MapPath(@"~\Upload") +"\\"+filename;
System.IO.FileStream fs=newSystem.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.Fileaccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();//二维码解码varcodeDecoder =CodeDecoder(filepath);
}///<summary>///二维码解码///</summary>///<param name="filePath">图片路径</param>///<returns></returns>publicstringCodeDecoder(stringfilePath)
{if(!System.IO.File.Exists(filePath))returnnull;
Bitmap myBitmap=newBitmap(Image.FromFile(filePath));
QRCodeDecoder decoder=newQRCodeDecoder();stringdecodedString = decoder.decode(newQRCodeBitmapImage(myBitmap));returndecodedString;
}
2、源代码演示,相对复杂的生成方式:界面效果:
前端代码:
@{
Layout=null;
}<!DOCTYPE html>
<html>
<head>
<meta name="viewport"content="width=device-width"/>
<title>Code</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">$(function () {
$("#createCode").click(function () {varstrData = $("#strData").val();varlevel = $("#level").val();varencoding = $("#encoding").val();varversion = $("#version").val();varscale = $("#scale").val();
$.post("/home/CreateCode_Choose", { strData: strData, level: level, qrEncoding: encoding, version: version, scale: scale }, function (data) {
$("img").attr("src", data);
});
});
});</script>
</head>
<body>
<div>
<p>要生成的文字(支持中文):<input id="strData"type="text"value="123456 测试"/></p>
<p>Encoding:<selectid="encoding">
<option value="Byte">Byte</option>
<option value="AlphaNumeric">AlphaNumeric</option>
<option value="Numeric">Numeric</option>
</select>
</p>
<p>Correction Level:<selectid="level">
<option value="M">M</option>
<option value="L">L</option>
<option value="Q">Q</option>
<option value="H">H</option>
</select>
</p>
<p>Version:<input id="version"type="text"value="8"/></p>
<p>scale:<input id="scale"type="text"value="4"/></p>
<img src=""alt=""/>
<input id="createCode"type="button"value="生成"/>
</div>
</body>
</html>
后台:
///生成二维码///</summary>///<param name="strData">要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param>///<param name="qrEncoding">三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param>///<param name="level">大小:L M Q H</param>///<param name="version">版本:如 8</param>///<param name="scale">比例:如 4</param>///<returns></returns>publicActionResult CreateCode_Choose(stringstrData,stringqrEncoding,stringlevel,intversion,intscale)
{
QRCodeEncoder qrCodeEncoder=newQRCodeEncoder();stringencoding =qrEncoding;switch(encoding)
{case"Byte":
qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.BYTE;
break;case"AlphaNumeric":
qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
break;case"Numeric":
qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.NUMERIC;
break;default:
qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.BYTE;
break;
}
qrCodeEncoder.QRCodeScale=scale;
qrCodeEncoder.QRCodeVersion=version;switch(level)
{case"L":
qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.L;
break;case"M":
qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.M;
break;case"Q":
qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.Q;
break;default:
qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.H;
break;
}//文字生成图片Image image =qrCodeEncoder.Encode(strData);stringfilename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() +".jpg";stringfilepath = Server.MapPath(@"~\Upload") +"\\"+filename;//如果文件夹不存在,则创建//if (!Directory.Exists(filepath))//Directory.CreateDirectory(filepath);System.IO.FileStream fs =newSystem.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();returnContent(@"/Upload/"+filename);
}
源代码演示下载:点击下载