首页 技术 正文
技术 2022年11月21日
0 收藏 726 点赞 2,513 浏览 4370 个字

随着互联网越来越生活化,二维码的使用越来越普遍,不论是扫码支付还是扫码关注引流,似乎我们总是离不开二维码,那么很多需要推广的文章或社区想要自己的二维码,那么你是不是需要在网站直接提供给用户呢?很多开发者就在网上百度解决方案,边做边踩坑,甚至很多人写的开发案例都是截图或者类库引用都没说清楚,在这个摸索的途中造成很多时间上的浪费。

尤其是尝试新技术那些旧的操作还会有所改变,为了节约开发时间,我们把解决方案收入到一个个demo中,方便以后即拿即用。而且这些demo有博客文档支持,帮助任何人非常容易上手开发跨平台的.net core。随着时间的推移,我们的demo库会日益强大请及时收藏GitHub

一、首先在Common公用项目中引用QRCoder类库

 Install-Package QRCoder -Version 1.3.

二、在Common公用项目中创建QRCoderHelper类

建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板

        #region 普通二维码
/// <summary>
///
/// </summary>
/// <param name="url">存储内容</param>
/// <param name="pixel">像素大小</param>
/// <returns></returns>
public static Bitmap GetPTQRCode(string url, int pixel)
{
QRCodeGenerator generator = new QRCodeGenerator();
QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true);
QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);
Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, true);
return qrImage;
}
#endregion #region 带logo的二维码
/// <summary>
///
/// </summary>
/// <param name="url">存储内容</param>
/// <param name="pixel">像素大小</param>
/// <returns></returns>
public static Bitmap GetLogoQRCode(string url,string logoPath, int pixel)
{
QRCodeGenerator generator = new QRCodeGenerator();
QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true);
QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);
Bitmap icon = new Bitmap(logoPath);
Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, icon,,, true);
#region 参数介绍
//GetGraphic方法参数介绍
//pixelsPerModule //生成二维码图片的像素大小 ,我这里设置的是5
//darkColor //暗色 一般设置为Color.Black 黑色
//lightColor //亮色 一般设置为Color.White 白色
//icon //二维码 水印图标 例如:Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); 默认为NULL ,加上这个二维码中间会显示一个图标
//iconSizePercent //水印图标的大小比例 ,可根据自己的喜好设置
//iconBorderWidth // 水印图标的边框
//drawQuietZones //静止区,位于二维码某一边的空白边界,用来阻止读者获取与正在浏览的二维码无关的信息 即是否绘画二维码的空白边框区域 默认为true
#endregion
return qrImage;
}
#endregion

注意:如果你想做其它调整可以参考我第二个方法中的参数介绍进行设置,考虑到大家的难处,我把那些相关参数帮各位查了。

三、添加QRCodeController控制器处理请求并返回二维码图片

建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板

private readonly IWebHostEnvironment webHostEnvironment;
//private readonly IHostingEnvironment _hostingEnvironment; 3.0之前使用它
public QRCodeController(IWebHostEnvironment _webHostEnvironment)
{
webHostEnvironment=_webHostEnvironment;
}
public IActionResult Index()
{
return View();
}
public IActionResult GetPTQRCode(string url, int pixel=)
{
url = HttpUtility.UrlDecode(url);
Response.ContentType = "image/jpeg"; var bitmap = QRCoderHelper.GetPTQRCode(url, pixel);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
return File(ms.ToArray(), "image/png");
}
public IActionResult GetLogoQRCode(string url,string logoPath, int pixel = )
{
url = HttpUtility.UrlDecode(url);
logoPath= webHostEnvironment.WebRootPath + HttpUtility.UrlDecode(logoPath);
Response.ContentType = "image/jpeg"; var bitmap = QRCoderHelper.GetLogoQRCode(url, logoPath, pixel);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Jpeg);
return File(ms.ToArray(), "image/png");
}

注意:其中IHostingEnvironment是.net core 3.0之前使用获取目录位置的,而我们.net core 3.0已经由IHostingEnvironment变更为IWebHostEnvironment来获取了。

四、前端请求设计

<link href="~/Lib/Mobile/jquery.mobile-1.4.5.min.css" rel="external nofollow"  rel="stylesheet" />
<script src="~/Lib/Mobile/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript">
$(function () {
PTCreate();
LogoCreate();
});
function PTCreate() {
pt = escape($("#PT").val());//防止中文等特殊字符引起问题
ptSize = $("#PTSize").val();
var id = document.getElementById("PTImg");
var str = "../QRCode/GetPTQRCode?url=" + pt + "&pixel=" + ptSize + "&random=" + Math.random();
id.setAttribute("src", str);
}
function LogoCreate() {
logo = escape($("#Logo").val());
logoPath = escape($("#LogoPath").val());
logoSize = $("#LogoSize").val();
var id = document.getElementById("LogoImg");
var str = "../QRCode/GetLogoQRCode?url=" + logo + "&logoPath=" + logoPath + "&pixel=" + logoSize + "&random=" + Math.random();
id.setAttribute("src", str);
}
</script>
<div style="width:60%; margin:auto;text-align:center;">
<h2>普通二维码</h2>
<input type="text" class="form-control" id="PT" placeholder="请输入字符" value="www.jiyuwu.com"><button onclick="PTCreate();">生成</button>
<input type="range" name="points" id="PTSize" value="" min="" max="" onchange="PTCreate();">
<br />
<img id="PTImg" />
<h2>Logo二维码</h2>
<input type="text" class="form-control" id="Logo" placeholder="请输入字符" value="www.jiyuwu.com">
<input type="text" class="form-control" id="LogoPath" placeholder="logo位置" value="\Lib\Markdown\images\logos\editormd-logo-32x32.png">
<button onclick="LogoCreate();">生成</button>
<input type="range" name="points" id="LogoSize" value="" min="" max="" onchange="LogoCreate();">
<img id="LogoImg" />
</div>

五、那么看下效果吧

建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板

开源地址 动动小手,点个推荐吧!

注意:我们机遇屋该项目将长期为大家提供asp.net core各种好用demo,旨在帮助.net开发者提升竞争力和开发速度,建议尽早收藏该模板集合项目

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774