控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlanarYUVLuminanceSource.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * Copyright 2009 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. * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
  20. * with the option to crop to a rectangle within the full data. This can be used to exclude
  21. * superfluous pixels around the perimeter and speed up decoding.
  22. *
  23. * It works for any pixel format where the Y channel is planar and appears first, including
  24. * YCbCr_420_SP and YCbCr_422_SP.
  25. *
  26. * @author dswitkin@google.com (Daniel Switkin)
  27. */
  28. final class PlanarYUVLuminanceSource extends LuminanceSource
  29. {
  30. private static int $THUMBNAIL_SCALE_FACTOR = 2;
  31. private $dataWidth;
  32. private $dataHeight;
  33. private $left;
  34. private $top;
  35. public function __construct(
  36. private $yuvData,
  37. $dataWidth,
  38. $dataHeight,
  39. $left,
  40. $top,
  41. $width,
  42. $height,
  43. $reverseHorizontal
  44. )
  45. {
  46. parent::__construct($width, $height);
  47. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  48. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  49. }
  50. $this->dataWidth = $dataWidth;
  51. $this->dataHeight = $dataHeight;
  52. $this->left = $left;
  53. $this->top = $top;
  54. if ($reverseHorizontal) {
  55. $this->reverseHorizontal($width, $height);
  56. }
  57. }
  58. //@Override
  59. public function getRow($y, $row = null)
  60. {
  61. if ($y < 0 || $y >= $this->getHeight()) {
  62. throw new \InvalidArgumentException("Requested row is outside the image: " + \Y);
  63. }
  64. $width = $this->getWidth();
  65. if ($row == null || (is_countable($row) ? count($row) : 0) < $width) {
  66. $row = [];//new byte[width];
  67. }
  68. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  69. $row = arraycopy($this->yuvData, $offset, $row, 0, $width);
  70. return $row;
  71. }
  72. //@Override
  73. public function getMatrix()
  74. {
  75. $width = $this->getWidth();
  76. $height = $this->getHeight();
  77. // If the caller asks for the entire underlying image, save the copy and give them the
  78. // original data. The docs specifically warn that result.length must be ignored.
  79. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  80. return $this->yuvData;
  81. }
  82. $area = $width * $height;
  83. $matrix = [];//new byte[area];
  84. $inputOffset = $this->top * $this->dataWidth + $this->left;
  85. // If the width matches the full width of the underlying data, perform a single copy.
  86. if ($width == $this->dataWidth) {
  87. $matrix = arraycopy($this->yuvData, $inputOffset, $matrix, 0, $area);
  88. return $matrix;
  89. }
  90. // Otherwise copy one cropped row at a time.
  91. $yuv = $this->yuvData;
  92. for ($y = 0; $y < $height; $y++) {
  93. $outputOffset = $y * $width;
  94. $matrix = arraycopy($this->yuvData, $inputOffset, $matrix, $outputOffset, $width);
  95. $inputOffset += $this->dataWidth;
  96. }
  97. return $matrix;
  98. }
  99. // @Override
  100. public function isCropSupported()
  101. {
  102. return true;
  103. }
  104. // @Override
  105. public function crop($left, $top, $width, $height): \Zxing\PlanarYUVLuminanceSource
  106. {
  107. return new PlanarYUVLuminanceSource(
  108. $this->yuvData,
  109. $this->dataWidth,
  110. $this->dataHeight,
  111. $this->left + $left,
  112. $this->top + $top,
  113. $width,
  114. $height,
  115. false
  116. );
  117. }
  118. public function renderThumbnail()
  119. {
  120. $width = (int)($this->getWidth() / self::$THUMBNAIL_SCALE_FACTOR);
  121. $height = (int)($this->getHeight() / self::$THUMBNAIL_SCALE_FACTOR);
  122. $pixels = [];//new int[width * height];
  123. $yuv = $this->yuvData;
  124. $inputOffset = $this->top * $this->dataWidth + $this->left;
  125. for ($y = 0; $y < $height; $y++) {
  126. $outputOffset = $y * $width;
  127. for ($x = 0; $x < $width; $x++) {
  128. $grey = ($yuv[$inputOffset + $x * self::$THUMBNAIL_SCALE_FACTOR] & 0xff);
  129. $pixels[$outputOffset + $x] = (0xFF000000 | ($grey * 0x00010101));
  130. }
  131. $inputOffset += $this->dataWidth * self::$THUMBNAIL_SCALE_FACTOR;
  132. }
  133. return $pixels;
  134. }
  135. /**
  136. * @return width of image from {@link #renderThumbnail()}
  137. */
  138. /*
  139. public int getThumbnailWidth() {
  140. return getWidth() / THUMBNAIL_SCALE_FACTOR;
  141. }*/
  142. /**
  143. * @return height of image from {@link #renderThumbnail()}
  144. */
  145. /*
  146. public int getThumbnailHeight() {
  147. return getHeight() / THUMBNAIL_SCALE_FACTOR;
  148. }
  149. private void reverseHorizontal(int width, int height) {
  150. byte[] yuvData = this.yuvData;
  151. for (int y = 0, rowStart = top * dataWidth + left; y < height; y++, rowStart += dataWidth) {
  152. int middle = rowStart + width / 2;
  153. for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
  154. byte temp = yuvData[x1];
  155. yuvData[x1] = yuvData[x2];
  156. yuvData[x2] = temp;
  157. }
  158. }
  159. }
  160. */
  161. }