• ZXing.Net 的Core平台生成二维码


    一、引用

    image

    二、代码

    帮助类
    1. ///
    2. /// ZXing.NET 二维码帮助类
    3. ///
    4. public class ZXingHelper
    5. {
    6. ///
    7. /// 站点二维码的目录
    8. ///
    9. private static string QRCodeDirectory = "QRCode";
    10. ///
    11. /// 使用zxing动态库生成二维码
    12. ///
    13. /// 二维码内容
    14. /// logo图片路径,默认为空。为空时生成的二维码不带logo
    15. /// 二维码图片高度,默认240 单位 pixels
    16. /// 二维码图片宽度,默认240 单位 pixels
    17. /// 二维码图片边距,默认为0
    18. ///
    19. public static System.DrawingCore.Bitmap GenerateQRCode(string conetnt, string logoPath = "", int height = 240, int width = 240, int margin = 0)
    20. {
    21. try
    22. {
    23. BarcodeWriter barCodeWriter = new BarcodeWriter();
    24. barCodeWriter.Format = BarcodeFormat.QR_CODE;
    25. barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
    26. barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
    27. barCodeWriter.Options.Height = height;
    28. barCodeWriter.Options.Width = width;
    29. barCodeWriter.Options.Margin = margin;
    30. BitMatrix bm = barCodeWriter.Encode(conetnt);
    31. System.DrawingCore.Bitmap qrCodeImage = barCodeWriter.Write(bm);
    32. if (!string.IsNullOrEmpty(logoPath))
    33. {
    34. // 添加Logo
    35. System.DrawingCore.Bitmap logo = new System.DrawingCore.Bitmap(logoPath);
    36. int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小为二维码大小的1/5
    37. int logoX = (qrCodeImage.Width - logoSize) / 2;
    38. int logoY = (qrCodeImage.Height - logoSize) / 2;
    39. System.DrawingCore.Graphics qrGraphics = System.DrawingCore.Graphics.FromImage(qrCodeImage);
    40. qrGraphics.DrawImage(logo, new System.DrawingCore.Rectangle(logoX, logoY, logoSize, logoSize));
    41. }
    42. return qrCodeImage;
    43. }
    44. catch (Exception ex)
    45. {
    46. return null;
    47. }
    48. }
    49. ///
    50. ///生成二维码
    51. ///
    52. ///
    53. ///
    54. ///
    55. ///
    56. public static byte[] Create(string message, int width = 600, int height = 600)
    57. {
    58. try
    59. {
    60. int heig = width;
    61. if (width > height)
    62. {
    63. heig = height;
    64. width = height;
    65. }
    66. if (string.IsNullOrWhiteSpace(message))
    67. {
    68. return null;
    69. }
    70. var w = new QRCodeWriter();
    71. BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
    72. var zzb = new BarcodeWriter();
    73. zzb.Options = new EncodingOptions()
    74. {
    75. Margin = 0,
    76. };
    77. var b2 = zzb.Write(b);
    78. byte[] bytes = BitmapToArray(b2);
    79. return bytes;
    80. }
    81. catch (Exception ex)
    82. {
    83. return null;
    84. }
    85. }
    86. ///
    87. /// 将Bitmap 写为byte[]的方法
    88. ///
    89. ///
    90. ///
    91. public static byte[] BitmapToArray(System.DrawingCore.Bitmap bmp)
    92. {
    93. byte[] byteArray = null;
    94. using (MemoryStream stream = new MemoryStream())
    95. {
    96. bmp.Save(stream, ImageFormat.Png);
    97. byteArray = stream.GetBuffer();
    98. }
    99. return byteArray;
    100. }
    101. }
    调用
    1. ///
    2. /// 获取二维码
    3. ///
    4. ///
    5. ///
    6. public byte[] GetQrCode(string qrUrl)
    7. {
    8. System.DrawingCore.Bitmap qrImage = null;
    9. if (!System.IO.File.Exists(QRCodeLogoPath))
    10. {
    11. qrImage = ZXingHelper.GenerateQRCode(qrUrl);
    12. }
    13. else
    14. {
    15. qrImage = ZXingHelper.GenerateQRCode(qrUrl, QRCodeLogoPath);
    16. }
    17. MemoryStream ms = new MemoryStream();
    18. qrImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Bmp);
    19. byte[] bytes = ms.GetBuffer();
    20. ms.Close();
    21. //return ZXingHelper.Create(qrUrl);
    22. return bytes;
    23. }
  • 相关阅读:
    多测师肖sir_高级金牌讲师___python之configparser模块
    网络安全(黑客)—-2024自学手册
    java设计模式7,工厂方法模式 | 文末送《Java核心技术》第12版
    Thinger.io 支持多协议、插件化开源 IoT 物联网平台
    Java语言编写猜字游戏
    老年人入住 ICU 的疾病严重程度和死亡率的趋势
    基于 Hive 的 Flutter 文档类型存储
    正确使用Impala的invalidate metadata与refresh语句
    03-HTTP报文
    电影《平凡英雄》
  • 原文地址:https://blog.csdn.net/weixin_43163153/article/details/133920983