Mode.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. static $TERMINATOR;
  26. static $NUMERIC;
  27. static $ALPHANUMERIC;
  28. static $STRUCTURED_APPEND;
  29. static $BYTE;
  30. static $ECI;
  31. static $KANJI;
  32. static $FNC1_FIRST_POSITION;
  33. static $FNC1_SECOND_POSITION;
  34. static $HANZI;
  35. private $characterCountBitsForVersions;
  36. private $bits;
  37. function __construct($characterCountBitsForVersions, $bits) {
  38. $this->characterCountBitsForVersions = $characterCountBitsForVersions;
  39. $this->bits = $bits;
  40. }
  41. static function Init()
  42. {
  43. self::$TERMINATOR = new Mode(array(0, 0, 0), 0x00); // Not really a mode...
  44. self::$NUMERIC = new Mode(array(10, 12, 14), 0x01);
  45. self::$ALPHANUMERIC = new Mode(array(9, 11, 13), 0x02);
  46. self::$STRUCTURED_APPEND = new Mode(array(0, 0, 0), 0x03); // Not supported
  47. self::$BYTE = new Mode(array(8, 16, 16), 0x04);
  48. self::$ECI = new Mode(array(0, 0, 0), 0x07); // character counts don't apply
  49. self::$KANJI = new Mode(array(8, 10, 12), 0x08);
  50. self::$FNC1_FIRST_POSITION = new Mode(array(0, 0, 0), 0x05);
  51. self::$FNC1_SECOND_POSITION =new Mode(array(0, 0, 0), 0x09);
  52. /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
  53. self::$HANZI = new Mode(array(8, 10, 12), 0x0D);
  54. }
  55. /**
  56. * @param bits four bits encoding a QR Code data mode
  57. * @return Mode encoded by these bits
  58. * @throws IllegalArgumentException if bits do not correspond to a known mode
  59. */
  60. public static function forBits($bits) {
  61. switch ($bits) {
  62. case 0x0:
  63. return self::$TERMINATOR;
  64. case 0x1:
  65. return self::$NUMERIC;
  66. case 0x2:
  67. return self::$ALPHANUMERIC;
  68. case 0x3:
  69. return self::$STRUCTURED_APPEND;
  70. case 0x4:
  71. return self::$BYTE;
  72. case 0x5:
  73. return self::$FNC1_FIRST_POSITION;
  74. case 0x7:
  75. return self::$ECI;
  76. case 0x8:
  77. return self::$KANJI;
  78. case 0x9:
  79. return self::$FNC1_SECOND_POSITION;
  80. case 0xD:
  81. // 0xD is defined in GBT 18284-2000, may not be supported in foreign country
  82. return self::$HANZI;
  83. default:
  84. throw new \InvalidArgumentException();
  85. }
  86. }
  87. /**
  88. * @param version version in question
  89. * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
  90. * count of characters that will follow encoded in this Mode
  91. */
  92. public function getCharacterCountBits($version) {
  93. $number = $version->getVersionNumber();
  94. $offset = 0;
  95. if ($number <= 9) {
  96. $offset = 0;
  97. } else if ($number <= 26) {
  98. $offset = 1;
  99. } else {
  100. $offset = 2;
  101. }
  102. return $this->characterCountBitsForVersions[$offset];
  103. }
  104. public function getBits() {
  105. return $this->bits;
  106. }
  107. }
  108. Mode::Init();