Mode.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Qrcode\Decoder;
  18. /**
  19. * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
  20. * data can be encoded to bits in the QR code standard.</p>
  21. *
  22. * @author Sean Owen
  23. */
  24. class Mode
  25. {
  26. public static $TERMINATOR;
  27. public static $NUMERIC;
  28. public static $ALPHANUMERIC;
  29. public static $STRUCTURED_APPEND;
  30. public static $BYTE;
  31. public static $ECI;
  32. public static $KANJI;
  33. public static $FNC1_FIRST_POSITION;
  34. public static $FNC1_SECOND_POSITION;
  35. public static $HANZI;
  36. private $characterCountBitsForVersions;
  37. private $bits;
  38. public function __construct($characterCountBitsForVersions, $bits)
  39. {
  40. $this->characterCountBitsForVersions = $characterCountBitsForVersions;
  41. $this->bits = $bits;
  42. }
  43. public static function Init()
  44. {
  45. self::$TERMINATOR = new Mode([0, 0, 0], 0x00); // Not really a mode...
  46. self::$NUMERIC = new Mode([10, 12, 14], 0x01);
  47. self::$ALPHANUMERIC = new Mode([9, 11, 13], 0x02);
  48. self::$STRUCTURED_APPEND = new Mode([0, 0, 0], 0x03); // Not supported
  49. self::$BYTE = new Mode([8, 16, 16], 0x04);
  50. self::$ECI = new Mode([0, 0, 0], 0x07); // character counts don't apply
  51. self::$KANJI = new Mode([8, 10, 12], 0x08);
  52. self::$FNC1_FIRST_POSITION = new Mode([0, 0, 0], 0x05);
  53. self::$FNC1_SECOND_POSITION = new Mode([0, 0, 0], 0x09);
  54. /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
  55. self::$HANZI = new Mode([8, 10, 12], 0x0D);
  56. }
  57. /**
  58. * @param bits four bits encoding a QR Code data mode
  59. *
  60. * @return Mode encoded by these bits
  61. * @throws InvalidArgumentException if bits do not correspond to a known mode
  62. */
  63. public static function forBits($bits)
  64. {
  65. switch ($bits) {
  66. case 0x0:
  67. return self::$TERMINATOR;
  68. case 0x1:
  69. return self::$NUMERIC;
  70. case 0x2:
  71. return self::$ALPHANUMERIC;
  72. case 0x3:
  73. return self::$STRUCTURED_APPEND;
  74. case 0x4:
  75. return self::$BYTE;
  76. case 0x5:
  77. return self::$FNC1_FIRST_POSITION;
  78. case 0x7:
  79. return self::$ECI;
  80. case 0x8:
  81. return self::$KANJI;
  82. case 0x9:
  83. return self::$FNC1_SECOND_POSITION;
  84. case 0xD:
  85. // 0xD is defined in GBT 18284-2000, may not be supported in foreign country
  86. return self::$HANZI;
  87. default:
  88. throw new \InvalidArgumentException();
  89. }
  90. }
  91. /**
  92. * @param version version in question
  93. *
  94. * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
  95. * count of characters that will follow encoded in this Mode
  96. */
  97. public function getCharacterCountBits($version)
  98. {
  99. $number = $version->getVersionNumber();
  100. $offset = 0;
  101. if ($number <= 9) {
  102. $offset = 0;
  103. } else if ($number <= 26) {
  104. $offset = 1;
  105. } else {
  106. $offset = 2;
  107. }
  108. return $this->characterCountBitsForVersions[$offset];
  109. }
  110. public function getBits()
  111. {
  112. return $this->bits;
  113. }
  114. }
  115. Mode::Init();