项目演示地址:https://gitee.com/qq28069933146_admin/itextsharp_v416_qrcoder_simple(因为itextsharp_v416涉及敏感开源协议的原因项目已删除;虽然只是LGPL协议)
1、QRCoderHelper类
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:二维码QRCoder的操作工具类
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2023-06-22 16:21:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Util.QRCoderHelpers
*│ 类 名:QRCoderHelper
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.IO;
using QRCoder;
using System.Drawing.Imaging;
namespace Itextsharp_v416_QRCoder_Simple
{
/// <summary>
/// QRCoder的操作工具类
/// </summary>
public class QRCoderHelper
{
/// <summary>
/// 生成二维码QRCode
/// </summary>
/// <param name="qrCodeDataStr">二维码数据</param>
/// <param name="eCCLevel">设置容错率;默认为Q</param>
/// <returns>返回QRCode对象</returns>
public static QRCode CreateQRCode(string qrCodeDataStr, QRCodeGenerator.ECCLevel eCCLevel = QRCodeGenerator.ECCLevel.Q)
{
// QRCodeGenerator:二维码生成器
// QRCodeData:二维码数据
// QRCode:二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrCodeDataStr, eCCLevel); // 设置容错率;默认为Q
QRCode qrCode = new QRCode(qrCodeData);
return qrCode;
}
/// <summary>
/// 生成二维码QRCode - 自定义图片
/// </summary>
/// <param name="qrCodeDataStr">二维码数据</param>
/// <param name="eCCLevel">设置容错率;默认为Q</param>
/// <param name="pixelsPerModule">像素点大小</param>
/// <param name="darkColor">前景色</param>
/// <param name="lightColor">背景色</param>
/// <param name="icon">二维码中心图标</param>
/// <param name="iconSizePercent">二维码中心图标_图标尺寸</param>
/// <param name="iconBorderWidth">二维码中心图标_边框厚度</param>
/// <param name="drawQuietZones">是否有白边</param>
/// <param name="iconBackgroundColor">二维码中心图标_背景色</param>
/// <returns>返回Bitmap图片</returns>
public static Bitmap CreateQRCode(string qrCodeDataStr, QRCodeGenerator.ECCLevel eCCLevel,
int pixelsPerModule, Color darkColor, Color lightColor,
Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 0, bool drawQuietZones = true, Color? iconBackgroundColor = null)
{
// QRCodeGenerator:二维码生成器
// QRCodeData:二维码数据
// QRCode:二维码
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrCodeDataStr, eCCLevel); // 设置容错率;默认为Q
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones, iconBackgroundColor);
return qrCodeImage;
}
/// <summary>
/// 保存二维码QRCode到文件
/// </summary>
/// <param name="qrCode">二维码</param>
/// <param name="pixelsPerModule">像素大小</param>
/// <param name="imageFormat">图片格式</param>
/// <param name="path">保存路径</param>
/// <returns>返回QRCode对象</returns>
public static void SaveQRCode(QRCode qrCode, int pixelsPerModule, ImageFormat imageFormat, string path)
{
Bitmap qrCodeImage = qrCode.GetGraphic(pixelsPerModule);
qrCodeImage.Save(path, imageFormat);
}
/// <summary>
/// 保存二维码图片到文件
/// </summary>
/// <param name="qrCodeImg">二维码图片</param>
/// <param name="path">保存路径</param>
/// <param name="imageFormat">图片格式</param>
/// <returns>返回QRCode对象</returns>
public static void SaveQRCode(Bitmap qrCodeImg, string path, ImageFormat imageFormat)
{
qrCodeImg.Save(path, imageFormat);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
作者:꧁执笔小白꧂