QRCoderHelper.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.NetworkInformation;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using QRCoder;
  10. using SixLabors.ImageSharp.Formats.Png;
  11. using SixLabors.ImageSharp.PixelFormats;
  12. //using System.IO;
  13. using ZXing;
  14. namespace Ropin.Core.Common
  15. {
  16. public class QRCoderHelper
  17. {
  18. ///// <summary>
  19. ///// 生成二维码
  20. ///// </summary>
  21. ///// <param name="value">二维码保存的值</param>
  22. ///// <param name="level"></param>
  23. //public static string RenderQrCode(string value,string level,string linsence)
  24. //{
  25. // Bitmap img = null;
  26. // string path;
  27. // QRCodeGenerator.ECCLevel eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
  28. // using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
  29. // using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(value, eccLevel))
  30. // using (QRCode qrCode = new QRCode(qrCodeData))
  31. // {
  32. // img = qrCode.GetGraphic(20);
  33. // var relativePath = Path.Combine("wwwroot/uploads/Files/", linsence + "/" + DateTime.Now.ToString("yyyyMM"));
  34. // var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  35. // if (!Directory.Exists(directoryPath))
  36. // {
  37. // Directory.CreateDirectory(directoryPath);
  38. // }
  39. // path = Path.Combine("/" + relativePath + "/", value + ".png");
  40. // img.Save(directoryPath + "/" + value + ".png");
  41. // }
  42. // return path;
  43. //}
  44. /// <summary>
  45. /// 生成二维码
  46. /// </summary>
  47. /// <param name="value">value</param>
  48. /// <param name="barcodeFormat">生成的类型 CODE_39/ CODE_93/ CODE_128/ QR_CODE ....</param>
  49. /// <param name="pathUrl">保存路径</param>
  50. /// <param name="fileName">文件名字</param>
  51. /// <param name="width">二维码宽,默认500</param>
  52. /// <param name="height">二维码高,默认500</param>
  53. public static string RenderQrCode(string value, string level, string linsence, int width = 500, int height = 500)
  54. {
  55. string path;
  56. //if(level == "M")
  57. var barcodeFormat = "QR_CODE";
  58. var barcodeFormatType = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), barcodeFormat);
  59. var writer = new ZXing.ImageSharp.BarcodeWriter<Rgba32>
  60. {
  61. Format = barcodeFormatType,
  62. Options = new ZXing.QrCode.QrCodeEncodingOptions
  63. {
  64. DisableECI = true,
  65. CharacterSet = "UTF-8",
  66. Width = width,
  67. Height = height,
  68. Margin = 1
  69. }
  70. };
  71. var image = writer.WriteAsImageSharp<Rgba32>(value);
  72. var ms = new MemoryStream();
  73. image.Save(ms, new PngEncoder());
  74. //pathUrl = pathUrl + "/";
  75. var relativePath = Path.Combine("wwwroot/uploads/Files/", linsence + "/" + DateTime.Now.ToString("yyyyMM"));
  76. var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  77. if (!Directory.Exists(directoryPath))
  78. {
  79. Directory.CreateDirectory(directoryPath);
  80. }
  81. path = Path.Combine("/" + relativePath + "/", value + ".png");
  82. //img.Save(directoryPath + "/" + value + ".png");
  83. using (var fileStream = File.Create(directoryPath + "/" + value + ".png"))
  84. {
  85. ms.Seek(0, SeekOrigin.Begin);
  86. ms.CopyTo(fileStream);
  87. }
  88. return path;
  89. }
  90. }
  91. }