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

一、引用

二、代码

帮助类
C# 复制代码
/// <summary>
  /// ZXing.NET 二维码帮助类
  /// </summary>
  public class ZXingHelper
  {
      /// <summary>
      /// 站点二维码的目录
      /// </summary>
      private static string QRCodeDirectory = "QRCode";

      /// <summary>
      /// 使用zxing动态库生成二维码
      /// </summary>
      /// <param name="conetnt">二维码内容</param>
      /// <param name="logoPath">logo图片路径,默认为空。为空时生成的二维码不带logo</param>
      /// <param name="height">二维码图片高度,默认240 单位 pixels</param>
      /// <param name="width">二维码图片宽度,默认240 单位 pixels</param>
      /// <param name="margin">二维码图片边距,默认为0</param>
      /// <returns></returns>
      public static System.DrawingCore.Bitmap GenerateQRCode(string conetnt, string logoPath = "", int height = 240, int width = 240, int margin = 0)
      {
          try
          {
              BarcodeWriter barCodeWriter = new BarcodeWriter();
              barCodeWriter.Format = BarcodeFormat.QR_CODE;
              barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
              barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
              barCodeWriter.Options.Height = height;
              barCodeWriter.Options.Width = width;
              barCodeWriter.Options.Margin = margin;
              BitMatrix bm = barCodeWriter.Encode(conetnt);
              System.DrawingCore.Bitmap qrCodeImage = barCodeWriter.Write(bm);

              if (!string.IsNullOrEmpty(logoPath))
              {
                  // 添加Logo
                  System.DrawingCore.Bitmap logo = new System.DrawingCore.Bitmap(logoPath);
                  int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小为二维码大小的1/5
                  int logoX = (qrCodeImage.Width - logoSize) / 2;
                  int logoY = (qrCodeImage.Height - logoSize) / 2;

                  System.DrawingCore.Graphics qrGraphics = System.DrawingCore.Graphics.FromImage(qrCodeImage);
                  qrGraphics.DrawImage(logo, new System.DrawingCore.Rectangle(logoX, logoY, logoSize, logoSize));
              }
              return qrCodeImage;
          }
          catch (Exception ex)
          {
              return null;
          }
      }

      /// <summary>
      ///生成二维码
      /// </summary>
      /// <param name="message"></param>
      /// <param name="width"></param>
      /// <param name="height"></param>
      /// <returns></returns>
      public static byte[] Create(string message, int width = 600, int height = 600)
      {
          try
          {
              int heig = width;
              if (width > height)
              {
                  heig = height;
                  width = height;
              }
              if (string.IsNullOrWhiteSpace(message))
              {
                  return null;
              }
              var w = new QRCodeWriter();
              BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
              var zzb = new BarcodeWriter();
              zzb.Options = new EncodingOptions()
              {
                  Margin = 0,
              };
              var  b2 = zzb.Write(b);
              byte[] bytes = BitmapToArray(b2);
              return bytes;
          }
          catch (Exception ex)
          {
              return null;
          }

      }

      /// <summary>
      /// 将Bitmap  写为byte[]的方法
      /// </summary>
      /// <param name="bmp"></param>
      /// <returns></returns>
      public static byte[] BitmapToArray(System.DrawingCore.Bitmap bmp)
      {
          byte[] byteArray = null;
          using (MemoryStream stream = new MemoryStream())
          {
              bmp.Save(stream, ImageFormat.Png);
              byteArray = stream.GetBuffer();
          }
          return byteArray;
      }
  }
调用
C# 复制代码
/// <summary>
 /// 获取二维码
 /// </summary>
 /// <param name="qrUrl"></param>
 /// <returns></returns>
 public byte[] GetQrCode(string qrUrl)
 {
     System.DrawingCore.Bitmap qrImage = null;
     if (!System.IO.File.Exists(QRCodeLogoPath))
     {
         qrImage = ZXingHelper.GenerateQRCode(qrUrl);
     }
     else
     {
         qrImage = ZXingHelper.GenerateQRCode(qrUrl, QRCodeLogoPath);
     }

     MemoryStream ms = new MemoryStream();
     qrImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Bmp);
     byte[] bytes = ms.GetBuffer();
     ms.Close();
     //return ZXingHelper.Create(qrUrl);
     return bytes;
 }
相关推荐
Boilermaker199216 分钟前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维29 分钟前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
MM_MS32 分钟前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
玩泥巴的1 小时前
飞书 .NET SDK 事件处理的幂等性与去重机制
c#·.net·二次开发·飞书
꧁Q༒ོγ꧂1 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs1 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_991 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子1 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
sheji34161 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
古城小栈1 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust