You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.2 KiB

  1. #if NET40 || NET461
  2. using Apewer.Internals.QrCode;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Apewer.Internals.QrCode
  9. {
  10. internal sealed class QRCodeUtility
  11. {
  12. public static Bitmap Generate(string argText, int argBlock, QRCodeGenerator.ECCLevel argEccLevel)
  13. {
  14. var text = argText ?? "";
  15. var block = argBlock > 1 ? argBlock : 1;
  16. var ecclevel = argEccLevel;
  17. try
  18. {
  19. var generator = new QRCodeGenerator();
  20. var content = generator.CreateQrCode(text, ecclevel);
  21. var qrcode = new QRCode(content);
  22. var bitmap = qrcode.GetGraphic(block);
  23. return bitmap;
  24. }
  25. catch { return null; }
  26. }
  27. /// <summary>生成含有 7% 容错区域的二维码图像,默认色块边长为 10 像素,失败时返回 NULL 值。</summary>
  28. public static Bitmap GenerateL(string argText)
  29. {
  30. return Generate(argText, 10, QRCodeGenerator.ECCLevel.L);
  31. }
  32. /// <summary>生成含有 7% 容错区域的二维码图像,可指定每个色块的边长,失败时返回 NULL 值。</summary>
  33. public static Bitmap GenerateL(string argText, int argBlock)
  34. {
  35. return Generate(argText, argBlock, QRCodeGenerator.ECCLevel.L);
  36. }
  37. /// <summary>生成含有 15% 容错区域的二维码图像,默认色块边长为 10 像素,失败时返回 NULL 值。</summary>
  38. public static Bitmap GenerateM(string argText)
  39. {
  40. return Generate(argText, 10, QRCodeGenerator.ECCLevel.M);
  41. }
  42. /// <summary>生成含有 15% 容错区域的二维码图像,可指定每个色块的边长,失败时返回 NULL 值。</summary>
  43. public static Bitmap GenerateM(string argText, int argBlock)
  44. {
  45. return Generate(argText, argBlock, QRCodeGenerator.ECCLevel.M);
  46. }
  47. /// <summary>生成含有 25% 容错区域的二维码图像,默认色块边长为 10 像素,失败时返回 NULL 值。</summary>
  48. public static Bitmap GenerateQ(string argText)
  49. {
  50. return Generate(argText, 10, QRCodeGenerator.ECCLevel.Q);
  51. }
  52. /// <summary>生成含有 25% 容错区域的二维码图像,可指定每个色块的边长,失败时返回 NULL 值。</summary>
  53. public static Bitmap GenerateQ(string argText, int argBlock)
  54. {
  55. return Generate(argText, argBlock, QRCodeGenerator.ECCLevel.Q);
  56. }
  57. /// <summary>生成含有 30% 容错区域的二维码图像,默认色块边长为 10 像素,失败时返回 NULL 值。</summary>
  58. public static Bitmap GenerateH(string argText)
  59. {
  60. return Generate(argText, 10, QRCodeGenerator.ECCLevel.H);
  61. }
  62. /// <summary>生成含有 30% 容错区域的二维码图像,可指定每个色块的边长,失败时返回 NULL 值。</summary>
  63. public static Bitmap GenerateH(string argText, int argBlock)
  64. {
  65. return Generate(argText, argBlock, QRCodeGenerator.ECCLevel.H);
  66. }
  67. }
  68. }
  69. #endif