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

Mode.cs 5.8 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2007 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 ZXing.QrCode.Internal
  18. {
  19. /// <summary>
  20. /// <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
  21. /// data can be encoded to bits in the QR code standard.</p>
  22. /// </summary>
  23. /// <author>Sean Owen</author>
  24. public sealed class Mode
  25. {
  26. /// <summary>
  27. /// Gets the name.
  28. /// </summary>
  29. public String Name
  30. {
  31. get
  32. {
  33. return name;
  34. }
  35. }
  36. // No, we can't use an enum here. J2ME doesn't support it.
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public static readonly Mode TERMINATOR = new Mode(new int[] { 0, 0, 0 }, 0x00, "TERMINATOR"); // Not really a mode...
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public static readonly Mode NUMERIC = new Mode(new int[] { 10, 12, 14 }, 0x01, "NUMERIC");
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. public static readonly Mode ALPHANUMERIC = new Mode(new int[] { 9, 11, 13 }, 0x02, "ALPHANUMERIC");
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public static readonly Mode STRUCTURED_APPEND = new Mode(new int[] { 0, 0, 0 }, 0x03, "STRUCTURED_APPEND"); // Not supported
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public static readonly Mode BYTE = new Mode(new int[] { 8, 16, 16 }, 0x04, "BYTE");
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. public static readonly Mode ECI = new Mode(null, 0x07, "ECI"); // character counts don't apply
  61. /// <summary>
  62. ///
  63. /// </summary>
  64. public static readonly Mode KANJI = new Mode(new int[] { 8, 10, 12 }, 0x08, "KANJI");
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. public static readonly Mode FNC1_FIRST_POSITION = new Mode(null, 0x05, "FNC1_FIRST_POSITION");
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. public static readonly Mode FNC1_SECOND_POSITION = new Mode(null, 0x09, "FNC1_SECOND_POSITION");
  73. /// <summary>See GBT 18284-2000; "Hanzi" is a transliteration of this mode name.</summary>
  74. public static readonly Mode HANZI = new Mode(new int[] { 8, 10, 12 }, 0x0D, "HANZI");
  75. private readonly int[] characterCountBitsForVersions;
  76. private readonly int bits;
  77. private readonly String name;
  78. private Mode(int[] characterCountBitsForVersions, int bits, System.String name)
  79. {
  80. this.characterCountBitsForVersions = characterCountBitsForVersions;
  81. this.bits = bits;
  82. this.name = name;
  83. }
  84. /// <summary>
  85. /// Fors the bits.
  86. /// </summary>
  87. /// <param name="bits">four bits encoding a QR Code data mode</param>
  88. /// <returns>
  89. /// <see cref="Mode"/> encoded by these bits
  90. /// </returns>
  91. /// <exception cref="ArgumentException">if bits do not correspond to a known mode</exception>
  92. public static Mode forBits(int bits)
  93. {
  94. switch (bits)
  95. {
  96. case 0x0:
  97. return TERMINATOR;
  98. case 0x1:
  99. return NUMERIC;
  100. case 0x2:
  101. return ALPHANUMERIC;
  102. case 0x3:
  103. return STRUCTURED_APPEND;
  104. case 0x4:
  105. return BYTE;
  106. case 0x5:
  107. return FNC1_FIRST_POSITION;
  108. case 0x7:
  109. return ECI;
  110. case 0x8:
  111. return KANJI;
  112. case 0x9:
  113. return FNC1_SECOND_POSITION;
  114. case 0xD:
  115. // 0xD is defined in GBT 18284-2000, may not be supported in foreign country
  116. return HANZI;
  117. default:
  118. throw new ArgumentException();
  119. }
  120. }
  121. /// <param name="version">version in question
  122. /// </param>
  123. /// <returns> number of bits used, in this QR Code symbol {@link Version}, to encode the
  124. /// count of characters that will follow encoded in this {@link Mode}
  125. /// </returns>
  126. public int getCharacterCountBits(Version version)
  127. {
  128. if (characterCountBitsForVersions == null)
  129. {
  130. throw new ArgumentException("Character count doesn't apply to this mode");
  131. }
  132. int number = version.VersionNumber;
  133. int offset;
  134. if (number <= 9)
  135. {
  136. offset = 0;
  137. }
  138. else if (number <= 26)
  139. {
  140. offset = 1;
  141. }
  142. else
  143. {
  144. offset = 2;
  145. }
  146. return characterCountBitsForVersions[offset];
  147. }
  148. /// <summary>
  149. /// Gets the bits.
  150. /// </summary>
  151. public int Bits
  152. {
  153. get
  154. {
  155. return bits;
  156. }
  157. }
  158. /// <summary>
  159. /// Returns a <see cref="System.String"/> that represents this instance.
  160. /// </summary>
  161. /// <returns>
  162. /// A <see cref="System.String"/> that represents this instance.
  163. /// </returns>
  164. public override String ToString()
  165. {
  166. return name;
  167. }
  168. }
  169. }