/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:二维码QRCoder的操作工具类(QRCoder1.5.1)
*│ 作 者:执笔小白
*│ 版 本: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 CreateAndSaveQRCode(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 CreateAndSaveQRCode(Bitmap qrCodeImg, string path, ImageFormat imageFormat)
{
qrCodeImg.Save(path, imageFormat);
}
}
}
标签:QRCoderHelper,QRCodeGenerator,Bitmap,qrCode,二维码,QRCode,工具,public From: https://www.cnblogs.com/qq2806933146xiaobai/p/18178514