QRrawcode.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * QRrawcode.php
  4. *
  5. * Created by arielferrandini
  6. */
  7. namespace PHPQRCode;
  8. use Exception;
  9. class QRrawcode {
  10. public $version;
  11. public $datacode = array();
  12. public $ecccode = array();
  13. public $blocks;
  14. public $rsblocks = array(); //of RSblock
  15. public $count;
  16. public $dataLength;
  17. public $eccLength;
  18. public $b1;
  19. //----------------------------------------------------------------------
  20. public function __construct(QRinput $input)
  21. {
  22. $spec = array(0,0,0,0,0);
  23. $this->datacode = $input->getByteStream();
  24. if(is_null($this->datacode)) {
  25. throw new Exception('null input string');
  26. }
  27. QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
  28. $this->version = $input->getVersion();
  29. $this->b1 = QRspec::rsBlockNum1($spec);
  30. $this->dataLength = QRspec::rsDataLength($spec);
  31. $this->eccLength = QRspec::rsEccLength($spec);
  32. $this->ecccode = array_fill(0, $this->eccLength, 0);
  33. $this->blocks = QRspec::rsBlockNum($spec);
  34. $ret = $this->init($spec);
  35. if($ret < 0) {
  36. throw new Exception('block alloc error');
  37. return null;
  38. }
  39. $this->count = 0;
  40. }
  41. //----------------------------------------------------------------------
  42. public function init(array $spec)
  43. {
  44. $dl = QRspec::rsDataCodes1($spec);
  45. $el = QRspec::rsEccCodes1($spec);
  46. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  47. $blockNo = 0;
  48. $dataPos = 0;
  49. $eccPos = 0;
  50. for($i=0; $i<QRspec::rsBlockNum1($spec); $i++) {
  51. $ecc = array_slice($this->ecccode,$eccPos);
  52. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  53. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  54. $dataPos += $dl;
  55. $eccPos += $el;
  56. $blockNo++;
  57. }
  58. if(QRspec::rsBlockNum2($spec) == 0)
  59. return 0;
  60. $dl = QRspec::rsDataCodes2($spec);
  61. $el = QRspec::rsEccCodes2($spec);
  62. $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  63. if($rs == NULL) return -1;
  64. for($i=0; $i<QRspec::rsBlockNum2($spec); $i++) {
  65. $ecc = array_slice($this->ecccode,$eccPos);
  66. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  67. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  68. $dataPos += $dl;
  69. $eccPos += $el;
  70. $blockNo++;
  71. }
  72. return 0;
  73. }
  74. //----------------------------------------------------------------------
  75. public function getCode()
  76. {
  77. $ret = null;
  78. if($this->count < $this->dataLength) {
  79. $row = $this->count % $this->blocks;
  80. $col = $this->count / $this->blocks;
  81. if($col >= $this->rsblocks[0]->dataLength) {
  82. $row += $this->b1;
  83. }
  84. $ret = $this->rsblocks[$row]->data[$col];
  85. } else if($this->count < $this->dataLength + $this->eccLength) {
  86. $row = ($this->count - $this->dataLength) % $this->blocks;
  87. $col = ($this->count - $this->dataLength) / $this->blocks;
  88. $ret = $this->rsblocks[$row]->ecc[$col];
  89. } else {
  90. return 0;
  91. }
  92. $this->count++;
  93. return $ret;
  94. }
  95. }