翼度科技»论坛 编程开发 .net 查看内容

基于ZXing.NET实现的二维码生成和识别客户端

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
一、前言

ZXing.Net的一个可移植软件包,是一个开源的、多格式的1D/2D条形码图像处理库,最初是用Java实现的。已经过大量优化和改进,它已经被手动移植。它与.Net 2.0、.Net 3.5、.Net 4.x、.Net 5.x、.Net 6.x、.Net 7.x、Windows RT类库和组件、UWP、.Net Standard 1.x和2.0x、.Net Core App 3.x、Silverlight 4、Silverlight 5、Windows Phone 7.x和Windows Phone 8.x以及Xamarin.Android兼容。
二、项目环境和搭建

项目框架:.NET Framework 4.6.1
项目依赖:ZXing.Net

项目结构和界面设计

三、实现核心代码

ZXing帮助类
  1. /// <summary>
  2. /// ZXing.NET 二维码帮助类
  3. /// </summary>
  4. public class ZXingHelper
  5. {
  6.      /// <summary>
  7.      /// 站点二维码的目录
  8.      /// </summary>
  9.      private static string QRCodeDirectory = "QRCode";
  10.      /// <summary>
  11.      /// 使用zxing动态库生成二维码
  12.      /// </summary>
  13.      /// <param name="conetnt">二维码内容</param>
  14.      /// <param name="errorMessage">异常信息</param>
  15.      /// <param name="logoPath">logo图片路径,默认为空。为空时生成的二维码不带logo</param>
  16.      /// <param name="height">二维码图片高度,默认240 单位 pixels</param>
  17.      /// <param name="width">二维码图片宽度,默认240 单位 pixels</param>
  18.      /// <param name="margin">二维码图片边距,默认为0</param>
  19.      /// <returns></returns>
  20.      public static Bitmap GenerateQRCode(string conetnt, out string errorMessage, string logoPath = "", int height = 240, int width = 240, int margin = 0)
  21.      {
  22.          errorMessage = string.Empty;
  23.          try
  24.          {
  25.              BarcodeWriter barCodeWriter = new BarcodeWriter();
  26.              barCodeWriter.Format = BarcodeFormat.QR_CODE;
  27.              barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  28.              barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  29.              barCodeWriter.Options.Height = height;
  30.              barCodeWriter.Options.Width = width;
  31.              barCodeWriter.Options.Margin = margin;
  32.              BitMatrix bm = barCodeWriter.Encode(conetnt);
  33.              Bitmap qrCodeImage = barCodeWriter.Write(bm);
  34.              if (!string.IsNullOrEmpty(logoPath))
  35.              {
  36.                  // 添加Logo
  37.                  Bitmap logo = new Bitmap(logoPath);
  38.                  int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小为二维码大小的1/5
  39.                  int logoX = (qrCodeImage.Width - logoSize) / 2;
  40.                  int logoY = (qrCodeImage.Height - logoSize) / 2;
  41.                  Graphics qrGraphics = Graphics.FromImage(qrCodeImage);
  42.                  qrGraphics.DrawImage(logo, new Rectangle(logoX, logoY, logoSize, logoSize));
  43.              }
  44.              return qrCodeImage;
  45.          }
  46.          catch (Exception ex)
  47.          {
  48.              errorMessage = $"Exception raised when generating QRCode,, Exception;{ex}";
  49.              return null;
  50.          }
  51.      }
  52.      /// <summary>
  53.      /// 使用zxing动态库解析:二维码,条形码......
  54.      /// </summary>
  55.      /// <param name="image">二维码图像</param>
  56.      /// <returns></returns>
  57.      public static string ParseBarCode(Bitmap image)
  58.      {
  59.          BarcodeReader reader = new BarcodeReader();
  60.          Result result = reader.Decode(image);
  61.          return result.Text;
  62.      }
  63.      /// <summary>
  64.      /// 使用zxing动态库生成二维码
  65.      /// </summary>
  66.      /// <param name="conetnt">二维码内容</param>
  67.      /// <param name="logoPath">logo图片路径,默认为空。为空时生成的二维码不带logo</param>
  68.      /// <param name="height">二维码图片高度,默认240 单位 pixels</param>
  69.      /// <param name="width">二维码图片宽度,默认240 单位 pixels</param>
  70.      /// <param name="margin">二维码图片边距,默认为0</param>
  71.      /// <returns></returns>
  72.      public static void GenerateQRCode(string conetnt, string logoPath = "", int height = 240, int width = 240, int margin = 0)
  73.      {
  74.          try
  75.          {
  76.              BarcodeWriter barCodeWriter = new BarcodeWriter();
  77.              barCodeWriter.Format = BarcodeFormat.QR_CODE;
  78.              barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  79.              barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  80.              barCodeWriter.Options.Height = height;
  81.              barCodeWriter.Options.Width = width;
  82.              barCodeWriter.Options.Margin = margin;
  83.              BitMatrix bm = barCodeWriter.Encode(conetnt);
  84.              Bitmap qrCodeImage = barCodeWriter.Write(bm);
  85.              if (!string.IsNullOrEmpty(logoPath))
  86.              {
  87.                  // 添加Logo
  88.                  Bitmap logo = new Bitmap(logoPath);
  89.                  int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小为二维码大小的1/5
  90.                  int logoX = (qrCodeImage.Width - logoSize) / 2;
  91.                  int logoY = (qrCodeImage.Height - logoSize) / 2;
  92.                  Graphics qrGraphics = Graphics.FromImage(qrCodeImage);
  93.                  qrGraphics.DrawImage(logo, new Rectangle(logoX, logoY, logoSize, logoSize));
  94.              }
  95.              string qrCodeFile = AppDomain.CurrentDomain.BaseDirectory + QRCodeDirectory + "/" + "qrcode.jpg";
  96.              if (File.Exists(qrCodeFile))
  97.              {
  98.                  File.Delete(qrCodeFile);
  99.              }
  100.              qrCodeImage.Save(qrCodeFile, System.Drawing.Imaging.ImageFormat.Jpeg);//保存二维码图片
  101.          }
  102.          catch (Exception ex)
  103.          {
  104.          }
  105.      }
  106. }
复制代码
选择logo
  1.     /// <summary>
  2.     /// 选择logo
  3.     /// </summary>
  4.     /// <param name="sender"></param>
  5.     /// <param name="e"></param>
  6.     private void btn_selectlogo_Click(object sender, EventArgs e)
  7.     {
  8.         try
  9.         {
  10.             OpenFileDialog openFileDialog = new OpenFileDialog();
  11.             openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始路径为桌面
  12.             openFileDialog.Filter = "Logo图片|*.png;*.jpg;*.jpeg;*.ico";
  13.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  14.             {
  15.                 picLogo.Image = Image.FromFile(openFileDialog.FileName);
  16.                 logPath = openFileDialog.FileName;//logo文件路径
  17.             }
  18.         }
  19.         catch (Exception ex)
  20.         {
  21.             MessageBox.Show(ex.Message);
  22.         }
  23.     }
复制代码
生成二维码
  1.   /// <summary>
  2.   /// 生成二维码
  3.   /// </summary>
  4.   /// <param name="sender"></param>
  5.   /// <param name="e"></param>
  6.   private void bt_generate_Click(object sender, EventArgs e)
  7.   {
  8.       try
  9.       {
  10.           string errorMsg = "";
  11.           string content = rtbQRCodeContent.Text;
  12.           if (string.IsNullOrWhiteSpace(content))
  13.           {
  14.               MessageBox.Show("二维码内容不能为空!");
  15.               return;
  16.           }
  17.           picQRCode.Image = ZXingHelper.GenerateQRCode(content, out errorMsg, logPath);
  18.           if (!string.IsNullOrWhiteSpace(errorMsg))
  19.           {
  20.               MessageBox.Show(errorMsg);
  21.           }
  22.       }
  23.       catch (Exception ex)
  24.       {
  25.           MessageBox.Show(ex.Message);
  26.       }
  27.   }
复制代码
识别二维码
  1.   /// <summary>
  2.   /// 识别二维码
  3.   /// </summary>
  4.   /// <param name="sender"></param>
  5.   /// <param name="e"></param>
  6.   private void btn_identityQrCode_Click(object sender, EventArgs e)
  7.   {
  8.       try
  9.       {
  10.           if (picQRCode.Image == null)
  11.           {
  12.               MessageBox.Show("请先生成二维码!");
  13.               return;
  14.           }
  15.           Bitmap Imagemap = new Bitmap(picQRCode.Image);
  16.           string QRCodeResult = ZXingHelper.ParseBarCode(Imagemap);
  17.           rtbQRCodeResult.Text = QRCodeResult;
  18.       }
  19.       catch (Exception ex)
  20.       {
  21.           MessageBox.Show(ex.Message);
  22.       }
  23.   }
复制代码
保存二维码
  1.   /// <summary>
  2.   /// 保存二维码
  3.   /// </summary>
  4.   /// <param name="sender"></param>
  5.   /// <param name="e"></param>
  6.   private void btn_save_Click(object sender, EventArgs e)
  7.   {
  8.       try
  9.       {
  10.           if (picQRCode.Image == null)
  11.           {
  12.               MessageBox.Show("请先生成二维码!");
  13.               return;
  14.           }
  15.           SaveFileDialog saveFileDialog = new SaveFileDialog();
  16.           saveFileDialog.Filter = "保存图片|*.png;*.jpg;*.jpeg";
  17.           if (saveFileDialog.ShowDialog() == DialogResult.OK)
  18.           {
  19.               picQRCode.Image.Save(saveFileDialog.FileName, ImageFormat.Png);
  20.               MessageBox.Show("保存成功!");
  21.           }
  22.       }
  23.       catch (Exception ex)
  24.       {
  25.           MessageBox.Show(ex.Message);
  26.       }
  27.   }
复制代码
四、实现演示


五、源码工具获取

关注公众号,后台回复关键字:二维码工具

来源:https://www.cnblogs.com/wml-it/archive/2023/10/13/17761875.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具