生成二维码

public static class QRcodeUtils
{
  private static string QrSaveUrl = "/img/QRcodeFile/";

  /// <summary>
  ///生成二维码
  /// </summary>
  /// <param name="QrContent">二维码内容</param>
  static void Generate(string QrContent)
  {
    //创建二维码生成类
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(QrContent);
    //输出显示在控制台
    for (int j = 0; j < qrCode.Matrix.Height; j++)
    {
      for (int i = 0; i < qrCode.Matrix.Width; i++)
      {
      char charToPoint = qrCode.Matrix[i, j] ? '█' : ' ';
      Console.Write(charToPoint);
      }
      Console.WriteLine();
    }
  }

  /// <summary>
  /// 生成图片
  /// </summary>
  /// <param name="QrContent">二维码内容</param>
  /// <param name="saveUrl">生成二维码保存路径</param>
  static string GenerateIMG(string QrContent, string saveUrl)
  {
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(QrContent);

    //保存成png文件
    string firstName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
    string lastName = new Random().Next(100, 999).ToString();
    string fullName = firstName + lastName;
    string fileUrl = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, QrSaveUrl, fullName);
    string resultUrl = QrSaveUrl + fullName;

    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
    using (FileStream stream = new FileStream(fileUrl, FileMode.Create))
    {  
      render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
      return resultUrl;
    }
  }

  /// <summary>
  /// 生成中文二维码
  /// </summary>
  /// <param name="QrContent">二维码内容</param>
  /// <param name="QrText">中文</param>
  static void GenerateTEXT(string QrContent, string QrText)
  {
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(QrText);

    //保存成png文件
    string firstName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
    string lastName = new Random().Next(100, 999).ToString();
    string fullName = firstName + lastName;
    string fileUrl = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, QrSaveUrl, fullName);
    string resultUrl = QrSaveUrl + fullName;
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

    Bitmap map = new Bitmap(500, 500);
    Graphics g = Graphics.FromImage(map);
    g.FillRectangle(Brushes.Red, 0, 0, 500, 500);
    render.Draw(g, qrCode.Matrix, new Point(20, 20));
    map.Save(fileUrl, ImageFormat.Png);
  }

  /// <summary>
  /// 设置二维码大小
  /// </summary>
  /// <param name="QrContent">二维码内容</param>
  /// <param name="QrText">中文</param>
  static void SetGenerateSIZE(string QrContent, string QrText)
  {
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(QrText);

    //保存成png文件
    string firstName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
    string lastName = new Random().Next(100, 999).ToString();
    string fullName = firstName + lastName;
    string fileUrl = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, QrSaveUrl, fullName);
    string resultUrl = QrSaveUrl + fullName;

    //ModuleSize 设置图片大小
    //QuietZoneModules 设置周边padding
    /*
    * 5----150*150 padding:5
    * 10----300*300 padding:10
    */
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), Brushes.Black, Brushes.White);

    Point padding = new Point(10, 10);
    DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
    Bitmap map = new Bitmap(dSize.CodeWidth + padding.X, dSize.CodeWidth + padding.Y);
    Graphics g = Graphics.FromImage(map);
    render.Draw(g, qrCode.Matrix, padding);
    map.Save(fileUrl, ImageFormat.Png);
  }
}

posted @ 2018-07-06 16:00  GU天乐乐乐!  阅读(203)  评论(1编辑  收藏  举报