using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using QRCoder;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
//using System.IO;
using ZXing;
namespace Ropin.Core.Common
{
public class QRCoderHelper
{
/////
///// 生成二维码
/////
///// 二维码保存的值
/////
//public static string RenderQrCode(string value,string level,string linsence)
//{
// Bitmap img = null;
// string path;
// QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
// using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(value, eccLevel))
// using (QRCode qrCode = new QRCode(qrCodeData))
// {
// img = qrCode.GetGraphic(20);
// var relativePath = Path.Combine("wwwroot/uploads/Files/", linsence + "/" + DateTime.Now.ToString("yyyyMM"));
// var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
// if (!Directory.Exists(directoryPath))
// {
// Directory.CreateDirectory(directoryPath);
// }
// path = Path.Combine("/" + relativePath + "/", value + ".png");
// img.Save(directoryPath + "/" + value + ".png");
// }
// return path;
//}
///
/// 生成二维码
///
/// value
/// 生成的类型 CODE_39/ CODE_93/ CODE_128/ QR_CODE ....
/// 保存路径
/// 文件名字
/// 二维码宽,默认500
/// 二维码高,默认500
public static string RenderQrCode(string value, string level, string linsence, int width = 500, int height = 500)
{
string path;
//if(level == "M")
var barcodeFormat = "QR_CODE";
var barcodeFormatType = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), barcodeFormat);
var writer = new ZXing.ImageSharp.BarcodeWriter
{
Format = barcodeFormatType,
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height,
Margin = 1
}
};
var image = writer.WriteAsImageSharp(value);
var ms = new MemoryStream();
image.Save(ms, new PngEncoder());
//pathUrl = pathUrl + "/";
var relativePath = Path.Combine("wwwroot/uploads/Files/", linsence + "/" + DateTime.Now.ToString("yyyyMM"));
var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
path = Path.Combine("/" + relativePath + "/", value + ".png");
//img.Save(directoryPath + "/" + value + ".png");
using (var fileStream = File.Create(directoryPath + "/" + value + ".png"))
{
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fileStream);
}
return path;
}
}
}