QRCodeWriter.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace FastReport.Barcode.QRCode
  18. {
  19. /*/// <summary> This object renders a QR Code as a ByteMatrix 2D array of greyscale values.
  20. ///
  21. /// </summary>
  22. /// <author> dswitkin@google.com (Daniel Switkin)
  23. /// </author>
  24. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  25. /// </author>*/
  26. internal static class QRCodeWriter
  27. {
  28. private const int QUIET_ZONE_SIZE = 4;
  29. public static ByteMatrix encode(System.String contents, int width, int height,
  30. ErrorCorrectionLevel errorCorrectionLevel, System.String encoding, bool quietZone)
  31. {
  32. if (contents == null || contents.Length == 0)
  33. {
  34. throw new System.ArgumentException("Found empty contents");
  35. }
  36. if (width < 0 || height < 0)
  37. {
  38. throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  39. }
  40. QRCode code = new QRCode();
  41. Encoder.encode(contents, errorCorrectionLevel, encoding, code);
  42. return renderResult(code, width, height, quietZone);
  43. }
  44. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  45. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  46. private static ByteMatrix renderResult(QRCode code, int width, int height, bool quietZone)
  47. {
  48. unchecked
  49. {
  50. ByteMatrix input = code.Matrix;
  51. int inputWidth = input.Width;
  52. int inputHeight = input.Height;
  53. int qrWidth = inputWidth + (quietZone ? QUIET_ZONE_SIZE << 1 : 0);
  54. int qrHeight = inputHeight + (quietZone ? QUIET_ZONE_SIZE << 1 : 0);
  55. int outputWidth = System.Math.Max(width, qrWidth);
  56. int outputHeight = System.Math.Max(height, qrHeight);
  57. int multiple = System.Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
  58. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  59. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  60. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  61. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  62. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  63. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  64. ByteMatrix output = new ByteMatrix(outputWidth, outputHeight);
  65. sbyte[][] outputArray = output.Array;
  66. // We could be tricky and use the first row in each set of multiple as the temporary storage,
  67. // instead of allocating this separate array.
  68. sbyte[] row = new sbyte[outputWidth];
  69. // 1. Write the white lines at the top
  70. for (int y = 0; y < topPadding; y++)
  71. {
  72. setRowColor(outputArray[y], (sbyte)SupportClass.Identity(255));
  73. }
  74. // 2. Expand the QR image to the multiple
  75. sbyte[][] inputArray = input.Array;
  76. for (int y = 0; y < inputHeight; y++)
  77. {
  78. // a. Write the white pixels at the left of each row
  79. for (int x = 0; x < leftPadding; x++)
  80. {
  81. row[x] = (sbyte)SupportClass.Identity(255);
  82. }
  83. // b. Write the contents of this row of the barcode
  84. int offset = leftPadding;
  85. for (int x = 0; x < inputWidth; x++)
  86. {
  87. // Redivivus.in Java to c# Porting update - Type cased sbyte
  88. // 30/01/2010
  89. // sbyte value_Renamed = (inputArray[y][x] == 1)?0:(sbyte) SupportClass.Identity(255);
  90. sbyte value_Renamed = (sbyte)((inputArray[y][x] == 1) ? 0 : SupportClass.Identity(255));
  91. for (int z = 0; z < multiple; z++)
  92. {
  93. row[offset + z] = value_Renamed;
  94. }
  95. offset += multiple;
  96. }
  97. // c. Write the white pixels at the right of each row
  98. offset = leftPadding + (inputWidth * multiple);
  99. for (int x = offset; x < outputWidth; x++)
  100. {
  101. row[x] = (sbyte)SupportClass.Identity(255);
  102. }
  103. // d. Write the completed row multiple times
  104. offset = topPadding + (y * multiple);
  105. for (int z = 0; z < multiple; z++)
  106. {
  107. Array.Copy(row, 0, outputArray[offset + z], 0, outputWidth);
  108. }
  109. }
  110. // 3. Write the white lines at the bottom
  111. int offset2 = topPadding + (inputHeight * multiple);
  112. for (int y = offset2; y < outputHeight; y++)
  113. {
  114. setRowColor(outputArray[y], (sbyte)SupportClass.Identity(255));
  115. }
  116. return output;
  117. }
  118. }
  119. private static void setRowColor(sbyte[] row, sbyte value_Renamed)
  120. {
  121. for (int x = 0; x < row.Length; x++)
  122. {
  123. row[x] = value_Renamed;
  124. }
  125. }
  126. }
  127. }