QrCode C#生成二维码 及JavaScript生成二维码

时间:2023-03-10 07:01:40
QrCode C#生成二维码 及JavaScript生成二维码

一 C#的二维码

   示例:

    class Program
{
static void Main(string[] args)
{
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode("菡,现在想起你,真的是太突然了,你跳个舞给我看嘛,能不能不要这么诱人啊,我已经逻辑混乱了", out qrCode);
int ModuleSize = ; //大小
QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域
var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
using (System.IO.Stream stream = File.OpenWrite(@"E:\RuPeng_Project\DiDao\DIDAO.Front\News\11\8.png"))
{
render.WriteToStream(qrCode.Matrix,System.Drawing.Imaging.ImageFormat.Png, stream);
}
}
}

Program.cs

   实例:

1 当新闻静态化时,获得这个文章的url,
2 把这个文章的url写入新路径的图片
3 这个图片可以在原来url的img标签显示出来

        /// <summary>
/// 对于每一篇文章url,生成一个二维码
/// </summary>
/// <param name="ViewStaticDirecPre">二维码所在目录前缀</param>
/// <param name="categoryId">文章的类别id</param>
/// <param name="id">文章id</param>
public static void CreateQrCode(string ViewStaticDirecPre, long categoryId, long id)
{
string newsUrl = "http://localhost:19826/News/" + categoryId + "/" + id + ".shtml"; //文章地址路径url
string qrCodePath = Path.Combine(ViewStaticDirecPre, categoryId + "\\" + id + ".png"); //生成的二维码路径
string qrPath = Path.GetDirectoryName(qrCodePath);
if (!Directory.Exists(qrPath))
{
Directory.CreateDirectory(qrPath);
}
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(newsUrl, out qrCode);
int ModuleSize = ; //大小
QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域
var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
using (System.IO.Stream stream = File.OpenWrite(qrCodePath))
{
render.WriteToStream(qrCode.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream);
}
}

二 JavaScript的二维码

    分享一下:jq生成二维码,不用再生成一堆png了。
<script src="/js/jquery.qrcode-0.11.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("#qrcode").qrcode({
//render: "table", //table方式
width: , //宽度
height: , //高度
text: "http://localhost:6158/News/@(Model.CategoryId)/@(Model.Id).shtml" //任意内容
});
})
</script>