【愚公系列】2023年08月 .NET CORE工具案例-二维码生成器QRCoder
【摘要】 前言二维码是一种能够存储数据的编码形式,它可以被扫描器快速识别和解码。二维码通常由黑白相间的方格组成,可以将文本、网址、电子邮件、电话号码等信息编码成矩阵条码的形式,从而方便用户获取信息。二维码的应用范围非常广泛,如商业、广告、新闻、公共交通系统等领域。条形码,也称为商品条码,是一种以条形图案为基础的一维条码,是商业和物流领域中广泛使用的一种编码方式。它可以迅速、准确地读取产品信息,包括商...
前言
二维码是一种能够存储数据的编码形式,它可以被扫描器快速识别和解码。二维码通常由黑白相间的方格组成,可以将文本、网址、电子邮件、电话号码等信息编码成矩阵条码的形式,从而方便用户获取信息。二维码的应用范围非常广泛,如商业、广告、新闻、公共交通系统等领域。
条形码,也称为商品条码,是一种以条形图案为基础的一维条码,是商业和物流领域中广泛使用的一种编码方式。它可以迅速、准确地读取产品信息,包括商品名称、价格、品牌、生产日期等,对于追溯产品流转、提高物流效率、防止假冒伪劣产品等方面发挥了巨大的作用。
不好意思,因为带二维码图片csdn会拦截,下面有些图片不能看。
一、二维码生成器QRCoder
1.QRCoder是什么
QRCoder 是用 C#.NET 编写的简单库,使您能够创建 QR 码。它对其他库没有任何依赖关系,在NuGet上作为.NET Framework和.NET Core PCL版本提供,可以自定义像素、颜色、背景色、logo、logo大小、logo边框等属性的二维码。
除了二次位图的二维码格式外,还支持以下格式:
- QRCode
- ArtQRCode
- AsciiQRCode
- Base64QRCode
- BitmapByteQRCode
- PdfByteQRCode
- PngByteQRCode
- PostscriptQRCode
- SvgQRCode
- UnityQRCode
- XamlQRCode
该库支持以下结构化内容的二维码:
- BezahlCode
- Bitcoin-Like cryptocurrency payment address:虚拟币付款地址
- Bookmark:书签
- Calendar events (iCal/vEvent):日历事件
- ContactData(MeCard/vCard):联系人数据
- Geolocation:位置
- Girocode
- Email:邮件
- MMS:多媒体信息服务
- Monero 地址/付款
- One-Time-Password:一次性密码
- Phonenumber:手机号码
- RussiaPaymentOrder
- Shadowsocks configuration
- Skype
- SlovenianUpnQr
- SMS:短信
- SwissQrCode
- Url:网址
- WiFi
QRCoder的代码网址:https://github.com/codebude/QRCoder
2.安装包
Install-Package QRCoder
调整OS为windows操作系统
3.普通二维码
3.1 创建二维码
using QRCoder;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3.2 设置二维码颜色
using QRCoder;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置颜色
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, false);
Bitmap qrCodeImage = qrCode.GetGraphic(10, "#efefef", "#2e3e4e");
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3.3 带logo的二维码
1、设置logo
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
2、设置logo图标边框
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"), iconBorderWidth: 6);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
3、设置logo图标边框背景色
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
//设置logo
//Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, icon: (Bitmap)Bitmap.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"), iconBorderWidth: 6, iconBackgroundColor: Color.DarkGreen);
qrCodeImage.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.艺术二维码
4.1 创建二维码
using QRCoder;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic(10);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.2 设置二维码颜色和图像大小
using QRCoder;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var finder = new Bitmap(15, 15);
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, finderPatternImage: finder);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.3 满屏二维码
using QRCoder;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic(10, Color.Black, Color.White, Color.Transparent, drawQuietZones: false);
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
4.4 带logo的二维码
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
var bmp = new ArtQRCode(data).GetGraphic((Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"));
bmp.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
5.纯文本二维码
5.1 创建二维码
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var targetCode = " \n \n \n \n ██████████████ ██ ██ ██████████████ \n ██ ██ ██ ████ ██ ██ \n ██ ██████ ██ ██ ██ ██ ██ ██████ ██ \n ██ ██████ ██ ██ ██ ██████ ██ \n ██ ██████ ██ ██ ██ ██████ ██ \n ██ ██ ████████ ██ ██ \n ██████████████ ██ ██ ██ ██████████████ \n ██ ████ \n ██████████ ████ ████████ ██ ████ \n ████ ██ ██ ████ ████████ ██ \n ██ ██ ██████████ ██ ██ ██ ████ \n ██ ██ ████ ████ ████ \n ████████ ██████ ████ ██ ██ \n ████████ \n ██████████████ ████ ████ ██ ████ ████ \n ██ ██ ████████ \n ██ ██████ ██ ██ ██ ██ ██ ██ ██ \n ██ ██████ ██ ██████ ██ ██ \n ██ ██████ ██ ██ ██ ██ ██ ████ ████ \n ██ ██ ████ ████ ██ ██ \n ██████████████ ██████ ██ ██████ \n \n \n \n ";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(1);
5.2 满屏二维码
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var targetCode = "██████████████ ██ ██ ██████████████\n██ ██ ██ ████ ██ ██\n██ ██████ ██ ██ ██ ██ ██ ██████ ██\n██ ██████ ██ ██ ██ ██████ ██\n██ ██████ ██ ██ ██ ██████ ██\n██ ██ ████████ ██ ██\n██████████████ ██ ██ ██ ██████████████\n ██ ████ \n██████████ ████ ████████ ██ ████ \n████ ██ ██ ████ ████████ ██\n ██ ██ ██████████ ██ ██ ██ ████ \n██ ██ ████ ████ ████ \n ████████ ██████ ████ ██ ██\n ████████ \n██████████████ ████ ████ ██ ████ ████\n██ ██ ████████ \n██ ██████ ██ ██ ██ ██ ██ ██ ██\n██ ██████ ██ ██████ ██ ██ \n██ ██████ ██ ██ ██ ██ ██ ████ ████\n██ ██ ████ ████ ██ ██ \n██████████████ ██████ ██ ██████";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(1, drawQuietZones: false);
5.3 x字二维码
using QRCoder;
//创建二维码
var targetCode = " \n \n \n \n \n \n \n \n XXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXX \n XXXXXXXXXXXXXX XXXX XXXXXXXXXXXXXX \n XX XX XXXXXX XX XX XX \n XX XX XXXXXX XX XX XX \n XX XXXXXX XX XXXXXXXX XX XXXXXX XX \n XX XXXXXX XX XXXXXXXX XX XXXXXX XX \n XX XXXXXX XX XXXX XX XXXXXX XX \n XX XXXXXX XX XXXX XX XXXXXX XX \n XX XXXXXX XX XX XX XX XXXXXX XX \n XX XXXXXX XX XX XX XX XXXXXX XX \n XX XX XX XX XX \n XX XX XX XX XX \n XXXXXXXXXXXXXX XX XX XX XXXXXXXXXXXXXX \n XXXXXXXXXXXXXX XX XX XX XXXXXXXXXXXXXX \n XXXXXXXX \n XXXXXXXX \n XX XXXXXX XXXXXX XX XX XX \n XX XXXXXX XXXXXX XX XX XX \n XX XXXXXX XXXX XXXXXXXX XXXXXX XX \n XX XXXXXX XXXX XXXXXXXX XXXXXX XX \n XX XX XX XX XX XX \n XX XX XX XX XX XX \n XX XX XX XX XXXXXX \n XX XX XX XX XXXXXX \n XX XXXXXXXX XXXX XX XXXXXXXX XX \n XX XXXXXXXX XXXX XX XXXXXXXX XX \n XX XXXXXXXX XXXX \n XX XXXXXXXX XXXX \n XXXXXXXXXXXXXX XXXXXXXX XX XXXXXX \n XXXXXXXXXXXXXX XXXXXXXX XX XXXXXX \n XX XX XXXXXX XXXXXXXX \n XX XX XXXXXX XXXXXXXX \n XX XXXXXX XX XX XXXX XX XXXX \n XX XXXXXX XX XX XXXX XX XXXX \n XX XXXXXX XX XXXX XXXXXXXX \n XX XXXXXX XX XXXX XXXXXXXX \n XX XXXXXX XX XX XXXXXXXX XX XXXXXX \n XX XXXXXX XX XX XXXXXXXX XX XXXXXX \n XX XX XX XXXX XX \n XX XX XX XXXX XX \n XXXXXXXXXXXXXX XX XXXXXX XXXX XXXX \n XXXXXXXXXXXXXX XX XXXXXX XXXX XXXX \n \n \n \n \n \n \n \n ";
//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphic(2, "X", " ");
6.比特币生成器
using QRCoder;
//创建二维码
var address = "175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W";
var amount = .123;
var label = "Some Label to Encode";
var message = "Some Message to Encode";
var generator = new PayloadGenerator.BitcoinAddress(address, amount, label, message).ToString();
这边不仅仅是比特币,还内置了各种账号生成(莱特币、WIFI账号密码、邮箱账号密码等等)
7.字节二维码
using QRCoder;
using System;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }, false);
MemoryStream ms = new MemoryStream(pngCodeGfx);
Bitmap img = (Bitmap)Image.FromStream(ms);
img.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
8.二维数据生成
var gen = new QRCodeGenerator();
var checkString = string.Empty;
var gField = gen.GetType().GetField("galoisField", BindingFlags.NonPublic | BindingFlags.Static);
foreach (var listitem in (System.Collections.IEnumerable)gField.GetValue(gen))
{
foreach (PropertyInfo prop in listitem.GetType().GetProperties())
checkString += prop.GetValue(listitem, null).ToString() + ",";
checkString += ":";
}
还包含文本等其他方式
9.SVG二维码
using QRCoder;
using QRCoderTests.Helpers;
using System.Drawing;
//创建二维码
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
//Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909361
var logoSvg = File.ReadAllText(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg");
var logoObj = new SvgQRCode.SvgLogo(logoSvg, 20, iconEmbedded: false);
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);
挂到网页上就好了
10.WIFI二维码
using QRCoder;
using static QRCoder.PayloadGenerator;
WiFi generator = new WiFi("WIFI名称","WIFI密码",WiFi.Authentication.WPA);
string payload = generator.ToString();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
qrCodeAsBitmap.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
11.邮件二维码
using QRCoder;
using static QRCoder.PayloadGenerator;
Mail generator = new Mail("邮件地址","邮件标题", "邮件标题");
string payload = generator.ToString();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
qrCodeAsBitmap.Save(@$"qrCode.jpg", System.Drawing.Imaging.ImageFormat.Bmp);
二、二维码下面加文字说明
public class ImageUtility
{
/// <summary>
/// 二维码下面加上文字
/// </summary>
/// <param name="qrImg">QR图片</param>
/// <param name="content">文字内容</param>
/// <param name="n"></param>
/// <returns></returns>
public Bitmap InsertWords(Bitmap qrImg, string content = "")
{
Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
backgroudImg.MakeTransparent();
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.Transparent);
//画二维码到新的面板上
g2.DrawImage(qrImg, 0, 0);
if (!string.IsNullOrEmpty(content))
{
FontFamily fontFamily = new FontFamily("楷体");
Font font1 = new Font(fontFamily, 20f, FontStyle.Bold, GraphicsUnit.Pixel);
//文字长度
int strWidth = (int)g2.MeasureString(content, font1).Width;
//总长度减去文字长度的一半 (居中显示)
int wordStartX = (qrImg.Width - strWidth) / 2;
int wordStartY = qrImg.Height - 30;
g2.DrawString(content, font1, Brushes.Black, wordStartX, wordStartY);
}
g2.Dispose();
return backgroudImg;
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)