Result.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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;
  18. /**
  19. * <p>Encapsulates the result of decoding a barcode within an image.</p>
  20. *
  21. * @author Sean Owen
  22. */
  23. final class Result
  24. {
  25. private $text;
  26. private $rawBytes;
  27. private $resultPoints;
  28. private $format;
  29. private $resultMetadata;
  30. private $timestamp;
  31. public function __construct(
  32. $text,
  33. $rawBytes,
  34. $resultPoints,
  35. $format,
  36. $timestamp = ''
  37. ) {
  38. $this->text = $text;
  39. $this->rawBytes = $rawBytes;
  40. $this->resultPoints = $resultPoints;
  41. $this->format = $format;
  42. $this->resultMetadata = null;
  43. $this->timestamp = $timestamp ?: time();
  44. }
  45. /**
  46. * @return raw text encoded by the barcode
  47. */
  48. public function getText()
  49. {
  50. return $this->text;
  51. }
  52. /**
  53. * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
  54. */
  55. public function getRawBytes()
  56. {
  57. return $this->rawBytes;
  58. }
  59. /**
  60. * @return points related to the barcode in the image. These are typically points
  61. * identifying finder patterns or the corners of the barcode. The exact meaning is
  62. * specific to the type of barcode that was decoded.
  63. */
  64. public function getResultPoints()
  65. {
  66. return $this->resultPoints;
  67. }
  68. /**
  69. * @return {@link BarcodeFormat} representing the format of the barcode that was decoded
  70. */
  71. public function getBarcodeFormat()
  72. {
  73. return $this->format;
  74. }
  75. /**
  76. * @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
  77. * {@code null}. This contains optional metadata about what was detected about the barcode,
  78. * like orientation.
  79. */
  80. public function getResultMetadata()
  81. {
  82. return $this->resultMetadata;
  83. }
  84. public function putMetadata($type, $value)
  85. {
  86. if ($this->resultMetadata === null) {
  87. $this->resultMetadata = [];
  88. }
  89. $resultMetadata[$type] = $value;
  90. }
  91. public function putAllMetadata($metadata)
  92. {
  93. if ($metadata !== null) {
  94. if ($this->resultMetadata === null) {
  95. $this->resultMetadata = $metadata;
  96. } else {
  97. $this->resultMetadata = array_merge($this->resultMetadata, $metadata);
  98. }
  99. }
  100. }
  101. public function addResultPoints($newPoints)
  102. {
  103. $oldPoints = $this->resultPoints;
  104. if ($oldPoints === null) {
  105. $this->resultPoints = $newPoints;
  106. } else if ($newPoints !== null && count($newPoints) > 0) {
  107. $allPoints = fill_array(0, count($oldPoints) + count($newPoints), 0);
  108. $allPoints = arraycopy($oldPoints, 0, $allPoints, 0, count($oldPoints));
  109. $allPoints = arraycopy($newPoints, 0, $allPoints, count($oldPoints), count($newPoints));
  110. $this->resultPoints = $allPoints;
  111. }
  112. }
  113. public function getTimestamp()
  114. {
  115. return $this->timestamp;
  116. }
  117. public function toString()
  118. {
  119. return $this->text;
  120. }
  121. }