ErrorCorrectionLevel.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.5.1. This enum encapsulates the four error correction levels
  20. * defined by the QR code standard.</p>
  21. *
  22. * @author Sean Owen
  23. */
  24. class ErrorCorrectionLevel {
  25. private static $FOR_BITS;
  26. private $bits;
  27. private $ordinal;
  28. function __construct($bits,$ordinal=0) {
  29. $this->bits = $bits;
  30. $this->ordinal = $ordinal;
  31. }
  32. public static function Init(){
  33. self::$FOR_BITS = array(
  34. new ErrorCorrectionLevel(0x00,1), //M
  35. new ErrorCorrectionLevel(0x01,0), //L
  36. new ErrorCorrectionLevel(0x02,3), //H
  37. new ErrorCorrectionLevel(0x03,2), //Q
  38. );
  39. }
  40. /** L = ~7% correction */
  41. // self::$L = new ErrorCorrectionLevel(0x01);
  42. /** M = ~15% correction */
  43. //self::$M = new ErrorCorrectionLevel(0x00);
  44. /** Q = ~25% correction */
  45. //self::$Q = new ErrorCorrectionLevel(0x03);
  46. /** H = ~30% correction */
  47. //self::$H = new ErrorCorrectionLevel(0x02);
  48. public function getBits() {
  49. return $this->bits;
  50. }
  51. public function toString() {
  52. return $this->bits;
  53. }
  54. public function getOrdinal() {
  55. return $this->ordinal;
  56. }
  57. /**
  58. * @param bits int containing the two bits encoding a QR Code's error correction level
  59. * @return ErrorCorrectionLevel representing the encoded error correction level
  60. */
  61. public static function forBits($bits) {
  62. if ($bits < 0 || $bits >= count(self::$FOR_BITS)) {
  63. throw new InvalidArgumentException();
  64. }
  65. $level = self::$FOR_BITS[$bits];
  66. // $lev = self::$$bit;
  67. return $level;
  68. }
  69. }
  70. ErrorCorrectionLevel::Init();