12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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
- {
- ///// <summary>
- ///// 生成二维码
- ///// </summary>
- ///// <param name="value">二维码保存的值</param>
- ///// <param name="level"></param>
- //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;
- //}
- /// <summary>
- /// 生成二维码
- /// </summary>
- /// <param name="value">value</param>
- /// <param name="barcodeFormat">生成的类型 CODE_39/ CODE_93/ CODE_128/ QR_CODE ....</param>
- /// <param name="pathUrl">保存路径</param>
- /// <param name="fileName">文件名字</param>
- /// <param name="width">二维码宽,默认500</param>
- /// <param name="height">二维码高,默认500</param>
- 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<Rgba32>
- {
- Format = barcodeFormatType,
- Options = new ZXing.QrCode.QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = width,
- Height = height,
- Margin = 1
- }
- };
- var image = writer.WriteAsImageSharp<Rgba32>(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;
- }
- }
- }
|