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 3.3 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 BYTE = new Mode(new int[] { 8, 16, 16 }, 0x04, "BYTE");
  41. private readonly int[] characterCountBitsForVersions;
  42. private readonly int bits;
  43. private readonly String name;
  44. private Mode(int[] characterCountBitsForVersions, int bits, System.String name)
  45. {
  46. this.characterCountBitsForVersions = characterCountBitsForVersions;
  47. this.bits = bits;
  48. this.name = name;
  49. }
  50. /// <summary>
  51. /// Fors the bits.
  52. /// </summary>
  53. /// <param name="bits">four bits encoding a QR Code data mode</param>
  54. /// <returns>
  55. /// <see cref="Mode"/> encoded by these bits
  56. /// </returns>
  57. /// <exception cref="ArgumentException">if bits do not correspond to a known mode</exception>
  58. public static Mode forBits(int bits)
  59. {
  60. switch (bits)
  61. {
  62. case 0x4:
  63. return BYTE;
  64. default:
  65. throw new ArgumentException();
  66. }
  67. }
  68. /// <param name="version">version in question
  69. /// </param>
  70. /// <returns> number of bits used, in this QR Code symbol {@link Version}, to encode the
  71. /// count of characters that will follow encoded in this {@link Mode}
  72. /// </returns>
  73. public int getCharacterCountBits(Version version)
  74. {
  75. if (characterCountBitsForVersions == null)
  76. {
  77. throw new ArgumentException("Character count doesn't apply to this mode");
  78. }
  79. int number = version.VersionNumber;
  80. int offset;
  81. if (number <= 9)
  82. {
  83. offset = 0;
  84. }
  85. else if (number <= 26)
  86. {
  87. offset = 1;
  88. }
  89. else
  90. {
  91. offset = 2;
  92. }
  93. return characterCountBitsForVersions[offset];
  94. }
  95. /// <summary>
  96. /// Gets the bits.
  97. /// </summary>
  98. public int Bits
  99. {
  100. get
  101. {
  102. return bits;
  103. }
  104. }
  105. }
  106. }