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.

98 lines
4.4 KiB

  1. #if NET40 || NET461
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer.Internals.QrCode
  6. {
  7. internal class AsciiQRCode : AbstractQRCode, IDisposable
  8. {
  9. /// <summary>
  10. /// Constructor without params to be used in COM Objects connections
  11. /// </summary>
  12. public AsciiQRCode() { }
  13. public AsciiQRCode(QRCodeData data) : base(data) { }
  14. /// <summary>
  15. /// Returns a strings that contains the resulting QR code as ASCII chars.
  16. /// </summary>
  17. /// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
  18. /// <returns></returns>
  19. public string GetGraphic(int repeatPerModule)
  20. {
  21. return string.Join("\n", GetLineByLineGraphic(repeatPerModule));
  22. }
  23. /// <summary>
  24. /// Returns a strings that contains the resulting QR code as ASCII chars.
  25. /// </summary>
  26. /// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
  27. /// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
  28. /// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
  29. /// <param name="endOfLine">End of line separator. (Default: \n)</param>
  30. /// <returns></returns>
  31. public string GetGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString, string endOfLine = "\n")
  32. {
  33. return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString));
  34. }
  35. /// <summary>
  36. /// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
  37. /// </summary>
  38. /// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
  39. /// <returns></returns>
  40. public string[] GetLineByLineGraphic(int repeatPerModule)
  41. {
  42. return GetLineByLineGraphic(repeatPerModule, "██", " ");
  43. }
  44. /// <summary>
  45. /// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
  46. /// </summary>
  47. /// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
  48. /// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
  49. /// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
  50. /// <returns></returns>
  51. public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString)
  52. {
  53. var qrCode = new List<string>();
  54. //We need to adjust the repeatPerModule based on number of characters in darkColorString
  55. //(we assume whiteSpaceString has the same number of characters)
  56. //to keep the QR code as square as possible.
  57. var adjustmentValueForNumberOfCharacters = darkColorString.Length / 2 != 1 ? darkColorString.Length / 2 : 0;
  58. var verticalNumberOfRepeats = repeatPerModule + adjustmentValueForNumberOfCharacters;
  59. var sideLength = QrCodeData.ModuleMatrix.Count * verticalNumberOfRepeats;
  60. for (var y = 0; y < sideLength; y++)
  61. {
  62. bool emptyLine = true;
  63. var lineBuilder = new StringBuilder();
  64. for (var x = 0; x < QrCodeData.ModuleMatrix.Count; x++)
  65. {
  66. var module = QrCodeData.ModuleMatrix[x][(y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1];
  67. for (var i = 0; i < repeatPerModule; i++)
  68. {
  69. lineBuilder.Append(module ? darkColorString : whiteSpaceString);
  70. }
  71. if (module)
  72. {
  73. emptyLine = false;
  74. }
  75. }
  76. if (!emptyLine)
  77. {
  78. qrCode.Add(lineBuilder.ToString());
  79. }
  80. }
  81. return qrCode.ToArray();
  82. }
  83. }
  84. }
  85. #endif