ZXing.NET(條碼、二維碼)


        

ZXing.NET用來生成和解釋 (條碼、二維碼)

添加引用:zxing.dll、zxing.presentation.dll

1 生成 (條碼、二維碼)主要用到 BarcodeWriterBarcodeFormatEncodingOptionsBitmapRenderer 這幾個類。

2  解釋 (條碼、二維碼)主要用到 BarcodeReaderDecodingOptions 這兩個類。

  /// <summary>
  /// 條碼、二維碼
  /// </summary>
  public static class BarQrCodeHelper
  {
    /// <summary>
    /// 生成條碼、二維碼
    /// </summary>
    /// <param name="code">要編碼內容</param>
    /// <param name="format">編碼格式(條碼CODE_128、二維碼QR_CODE)</param>
    /// <param name="options">編碼參數(條碼EncodingOptions、二維碼QrCodeEncodingOptions)</param>
    /// <param name="renderer">條碼、二維碼Bitmap參數</param>
    /// <param name="qrcodeoptions">二維碼參數</param>
    /// <returns></returns>
    public static Bitmap EncodeBarQrCode(string code, BarcodeFormat format, EncodingOptions options, BitmapRenderer renderer = null, QrCodeOptions qrcodeoptions = null)
    {
      BarcodeWriter writer = new BarcodeWriter();
      writer.Options = options;
      writer.Format = format;
      if (renderer != null)
        writer.Renderer = renderer;
      Bitmap bmp = writer.Write(code);


      if ((options is QrCodeEncodingOptions) && qrcodeoptions != null && qrcodeoptions.IsLogo)
      {
        int w = qrcodeoptions.Width - qrcodeoptions.LogoPadding * 2;
        int h = qrcodeoptions.Height - qrcodeoptions.LogoPadding * 2;
        Rectangle logo_rect = new Rectangle((bmp.Width - w) / 2, (bmp.Height - h) / 2, w, h);
        if (qrcodeoptions.LogoPadding == 0)
        {
          logo_rect = new Rectangle((bmp.Width - qrcodeoptions.Width) / 2, (bmp.Height - qrcodeoptions.Height) / 2, qrcodeoptions.Width, qrcodeoptions.Height);
        }

        Graphics bmp_g = Graphics.FromImage(bmp);
        bmp_g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        bmp_g.CompositingQuality = CompositingQuality.HighQuality;
        bmp_g.SmoothingMode = SmoothingMode.AntiAlias;

        Bitmap logo_bmp = (Bitmap)qrcodeoptions.Logo;//按比例縮放Logo
        if ((qrcodeoptions.Logo.Width > logo_rect.Width || qrcodeoptions.Logo.Height > logo_rect.Height) || (qrcodeoptions.Logo.Width < logo_rect.Width && qrcodeoptions.Logo.Height < logo_rect.Height))
        {
          logo_bmp = new Bitmap(logo_rect.Width, logo_rect.Height);
          Graphics logo_g = Graphics.FromImage(logo_bmp);
          logo_g.InterpolationMode = InterpolationMode.HighQualityBicubic;
          logo_g.CompositingQuality = CompositingQuality.HighQuality;
          logo_g.SmoothingMode = SmoothingMode.AntiAlias;

          Rectangle rect = new Rectangle(0, 0, 0, 0);
          float num1 = (float)logo_rect.Width / (float)qrcodeoptions.Logo.Width;
          float num2 = (float)logo_rect.Height / (float)qrcodeoptions.Logo.Height;
          float num3 = Math.Min(num1, num2);
          rect.Width = (int)((float)qrcodeoptions.Logo.Width * (float)num3);
          rect.Height = (int)((float)qrcodeoptions.Logo.Height * (float)num3);
          rect.X = (logo_rect.Width - rect.Width) / 2;
          rect.Y = (logo_rect.Height - rect.Height) / 2;

          logo_g.DrawImage((Bitmap)qrcodeoptions.Logo, rect);
          logo_g.Dispose();
        }

        Bitmap corner_bmp = new Bitmap(logo_rect.Width, logo_rect.Height);//圓角處理
        Graphics corner_g = Graphics.FromImage(corner_bmp);
        corner_g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        corner_g.CompositingQuality = CompositingQuality.HighQuality;
        corner_g.SmoothingMode = SmoothingMode.AntiAlias;

        corner_g.SetClip(CreateRoundedRectanglePath(new Rectangle(0, 0, logo_rect.Width, logo_rect.Height), qrcodeoptions.Radius));
        corner_g.DrawImage(logo_bmp, new Rectangle(0, 0, logo_bmp.Width, logo_bmp.Height));
        logo_bmp.Dispose();

        if (qrcodeoptions.LogoPadding != 0)
        {
          Pen padding_pen = new Pen(Color.Gray);
          SolidBrush padding_sb = new SolidBrush(qrcodeoptions.LogoBackColor);
          Rectangle padding_rect = new Rectangle((bmp.Width - qrcodeoptions.Width) / 2, (bmp.Height - qrcodeoptions.Height) / 2, qrcodeoptions.Width, qrcodeoptions.Height);
          GraphicsPath padding_path = CreateRoundedRectanglePath(padding_rect, qrcodeoptions.Radius);
          bmp_g.FillPath(padding_sb, padding_path);
          bmp_g.DrawPath(padding_pen, padding_path);
          padding_sb.Dispose();
          padding_pen.Dispose();
          padding_path.Dispose();
        }

        bmp_g.DrawImage(corner_bmp, logo_rect);

        corner_bmp.Dispose();
        corner_g.Dispose();
        bmp_g.Dispose();
      }
      return bmp;
    }

    /// <summary>
    /// 解釋條碼、二維碼
    /// </summary>
    /// <param name="code">要解碼內容</param>
    /// <param name="options">解碼參數</param>
    /// <returns></returns>
    public static string DecodeBarQrCode(Bitmap code, DecodingOptions options)
    {
      BarcodeReader reader = new BarcodeReader();
      reader.Options = options;
      reader.AutoRotate = true;
      Result result = reader.Decode(code);
      return result != null ? result.Text : String.Empty;
    }

    /// <summary>
    /// 創建圓角矩形
    /// </summary>
    /// <param name="rect">區域</param>
    /// <param name="cornerRadius">圓角角度</param>
    /// <returns></returns>
    private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
    {
      GraphicsPath result = new GraphicsPath();
      int diameter = cornerRadius * 2;

      result.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
      result.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius, rect.Y);

      result.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
      result.AddLine(rect.Right, rect.Y + cornerRadius, rect.Right, rect.Bottom - cornerRadius);

      result.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
      result.AddLine(rect.Right - cornerRadius, rect.Bottom, rect.X + cornerRadius, rect.Bottom);

      result.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
      result.AddLine(rect.X, rect.Bottom - cornerRadius, rect.X, rect.Y + cornerRadius);

      result.CloseFigure();
      return result;
    }


    /// <summary>
    /// 二維碼參數
    /// </summary>
    public class QrCodeOptions
    {
      public QrCodeOptions()
      {

      }

      public QrCodeOptions(int _Radius, bool _IsLogo, Image Logo, int _Width, int _Height, int _LogoPadding, Color _LogoBackColor, Color _LogoBorderColor)
      {
        this.Radius = _Radius;
        this.IsLogo = _IsLogo;
        this.Logo = Logo;
        this.Width = _Width;
        this.Height = _Height;
        this.LogoPadding = _LogoPadding;
        this.LogoBackColor = _LogoBackColor;
        this.LogoBorderColor = _LogoBorderColor;
      }

      /// <summary>
      /// 圓角度數
      /// </summary>
      public int Radius = 4;
      /// <summary>
      /// 是否顯示Logo
      /// </summary>
      public bool IsLogo = false;
      /// <summary>
      /// Logo圖片
      /// </summary>
      public Image Logo;
      /// <summary>
      /// Logo寬度
      /// </summary>
      public int Width;
      /// <summary>
      /// Logo高度
      /// </summary>
      public int Height;
      /// <summary>
      /// Logo內邊距
      /// </summary>
      public int LogoPadding = 4;
      /// <summary>
      /// Logo內邊距背景顏色
      /// </summary>
      public Color LogoBackColor = Color.White;
      /// <summary>
      /// Logo邊框顏色
      /// </summary>
      public Color LogoBorderColor = Color.Gray;
    }
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM