2025-10-10 14:33:21 +08:00
|
|
|
|
using QRCoder;
|
2022-10-13 18:52:45 +08:00
|
|
|
|
using System;
|
2022-09-05 16:36:31 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using ThoughtWorks.QRCode.Codec;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BLL
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 上传附件相关
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class CreateQRCodeService
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成二维码方法一
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string CreateCode_Simple(string nr)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string imageUrl = string.Empty;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(nr))
|
|
|
|
|
|
{
|
|
|
|
|
|
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder
|
|
|
|
|
|
{
|
|
|
|
|
|
QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE,
|
|
|
|
|
|
QRCodeScale = nr.Length,
|
|
|
|
|
|
QRCodeVersion = 0,
|
|
|
|
|
|
QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
|
|
|
|
|
|
};
|
|
|
|
|
|
System.Drawing.Image image = qrCodeEncoder.Encode(nr, Encoding.UTF8);
|
|
|
|
|
|
string filepath = Funs.RootPath + UploadFileService.QRCodeImageFilePath;
|
|
|
|
|
|
//如果文件夹不存在,则创建
|
|
|
|
|
|
if (!Directory.Exists(filepath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(filepath);
|
|
|
|
|
|
}
|
|
|
|
|
|
string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
|
|
|
|
|
|
FileStream fs = new FileStream(filepath + filename, FileMode.OpenOrCreate, FileAccess.Write);
|
|
|
|
|
|
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
image.Dispose();
|
|
|
|
|
|
imageUrl = UploadFileService.QRCodeImageFilePath + filename;
|
|
|
|
|
|
}
|
|
|
|
|
|
return imageUrl;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrLogInfo.WriteLog(ex, "二维码生成" + nr, "CreateQRCodeService.CreateCode_Simple");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-13 18:52:45 +08:00
|
|
|
|
/*
|
|
|
|
|
|
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, [Bitmap icon=null], [int iconSizePercent=15], [int iconBorderWidth=6], [bool drawQuietZones=true])
|
|
|
|
|
|
int pixelsPerModule:生成二维码图片的像素大小;
|
|
|
|
|
|
Color darkColor:二维码图像暗色,一般设置为Color.Black。
|
|
|
|
|
|
Color lightColor:二维码图像亮色, 一般设置为Color.White。
|
|
|
|
|
|
Bitmap icon:二维码中间水印图标。
|
|
|
|
|
|
int iconSizePercent:水印图标的大小比例。
|
|
|
|
|
|
int iconBorderWidth:水印图标的边框。不能设为0,否则会显示“参数错误”,程序运行出错。
|
|
|
|
|
|
bool drawQuietZones:静止区,即是否绘画二维码的空白边框区域,默认为true。
|
|
|
|
|
|
*/
|
2025-10-10 14:33:21 +08:00
|
|
|
|
public static string CreateCode_Simple(string nr, string filename)
|
2022-09-05 16:36:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string imageUrl = string.Empty;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(nr))
|
|
|
|
|
|
{
|
2022-10-13 18:52:45 +08:00
|
|
|
|
//QRCodeEncoder qrCodeEncoder = new QRCodeEncoder
|
|
|
|
|
|
//{
|
|
|
|
|
|
// QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE,
|
|
|
|
|
|
// QRCodeScale = nr.Length,
|
|
|
|
|
|
// QRCodeVersion =0,
|
|
|
|
|
|
// QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
|
|
|
|
|
|
//};
|
|
|
|
|
|
//System.Drawing.Image image = qrCodeEncoder.Encode(nr, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
|
|
QRCoder.QRCode qRCode = new QRCoder.QRCode();
|
|
|
|
|
|
QRCodeGenerator qrGenerator = new QRCodeGenerator();
|
|
|
|
|
|
QRCodeData qrCodeData = qrGenerator.CreateQrCode(nr, QRCodeGenerator.ECCLevel.Q);
|
|
|
|
|
|
QRCode qrcode = new QRCode(qrCodeData);
|
|
|
|
|
|
|
|
|
|
|
|
System.Drawing.Image image = qrcode.GetGraphic(100);
|
2022-09-05 16:36:31 +08:00
|
|
|
|
string filepath = Funs.RootPath + UploadFileService.QRCodeImageFilePath;
|
|
|
|
|
|
//如果文件夹不存在,则创建
|
|
|
|
|
|
if (!Directory.Exists(filepath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(filepath);
|
|
|
|
|
|
}
|
2025-10-10 14:33:21 +08:00
|
|
|
|
// string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
|
2022-09-05 16:36:31 +08:00
|
|
|
|
filename = filename + ".jpg";
|
|
|
|
|
|
//if (File.Exists(filepath + filename))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// File.Delete(filepath + filename);
|
|
|
|
|
|
//}
|
|
|
|
|
|
FileStream fs = new FileStream(filepath + filename, FileMode.OpenOrCreate, FileAccess.Write);
|
|
|
|
|
|
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
image.Dispose();
|
|
|
|
|
|
imageUrl = UploadFileService.QRCodeImageFilePath + filename;
|
|
|
|
|
|
}
|
|
|
|
|
|
return imageUrl;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrLogInfo.WriteLog(ex, "二维码生成" + nr, "CreateQRCodeService.CreateCode_Simple");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool isHaveImage(string url)
|
|
|
|
|
|
{
|
|
|
|
|
|
string filepath = Funs.RootPath + url;
|
|
|
|
|
|
return File.Exists(filepath);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|