控制台应用,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.

RGBLuminanceSource.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 class is used to help decode images from files which arrive as RGB data from
  20. * an ARGB pixel array. It does not support rotation.
  21. *
  22. * @author dswitkin@google.com (Daniel Switkin)
  23. * @author Betaminos
  24. */
  25. final class RGBLuminanceSource extends LuminanceSource
  26. {
  27. public $luminances;
  28. private $dataWidth;
  29. private $dataHeight;
  30. /**
  31. * @var mixed|int
  32. */
  33. private $left;
  34. /**
  35. * @var mixed|int
  36. */
  37. private $top;
  38. /**
  39. * @var mixed|null
  40. */
  41. private $pixels;
  42. public function __construct(
  43. $pixels,
  44. $dataWidth,
  45. $dataHeight,
  46. $left = null,
  47. $top = null,
  48. $width = null,
  49. $height = null
  50. ) {
  51. if (!$left && !$top && !$width && !$height) {
  52. $this->RGBLuminanceSource_($pixels, $dataWidth, $dataHeight);
  53. return;
  54. }
  55. parent::__construct($width, $height);
  56. if ($left + $width > $dataWidth || $top + $height > $dataHeight) {
  57. throw new \InvalidArgumentException("Crop rectangle does not fit within image data.");
  58. }
  59. $this->luminances = $pixels;
  60. $this->dataWidth = $dataWidth;
  61. $this->dataHeight = $dataHeight;
  62. $this->left = $left;
  63. $this->top = $top;
  64. }
  65. public function RGBLuminanceSource_($width, $height, $pixels): void
  66. {
  67. parent::__construct($width, $height);
  68. $this->dataWidth = $width;
  69. $this->dataHeight = $height;
  70. $this->left = 0;
  71. $this->top = 0;
  72. $this->pixels = $pixels;
  73. // In order to measure pure decoding speed, we convert the entire image to a greyscale array
  74. // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
  75. $this->luminances = [];
  76. //$this->luminances = $this->grayScaleToBitmap($this->grayscale());
  77. foreach ($pixels as $key => $pixel) {
  78. $r = $pixel['red'];
  79. $g = $pixel['green'];
  80. $b = $pixel['blue'];
  81. /* if (($pixel & 0xFF000000) == 0) {
  82. $pixel = 0xFFFFFFFF; // = white
  83. }
  84. // .229R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC)
  85. $this->luminances[$key] =
  86. (306 * (($pixel >> 16) & 0xFF) +
  87. 601 * (($pixel >> 8) & 0xFF) +
  88. 117 * ($pixel & 0xFF) +
  89. 0x200) >> 10;
  90. */
  91. //$r = ($pixel >> 16) & 0xff;
  92. //$g = ($pixel >> 8) & 0xff;
  93. //$b = $pixel & 0xff;
  94. if ($r == $g && $g == $b) {
  95. // Image is already greyscale, so pick any channel.
  96. $this->luminances[$key] = $r;//(($r + 128) % 256) - 128;
  97. } else {
  98. // Calculate luminance cheaply, favoring green.
  99. $this->luminances[$key] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
  100. }
  101. }
  102. /*
  103. for ($y = 0; $y < $height; $y++) {
  104. $offset = $y * $width;
  105. for ($x = 0; $x < $width; $x++) {
  106. $pixel = $pixels[$offset + $x];
  107. $r = ($pixel >> 16) & 0xff;
  108. $g = ($pixel >> 8) & 0xff;
  109. $b = $pixel & 0xff;
  110. if ($r == $g && $g == $b) {
  111. // Image is already greyscale, so pick any channel.
  112. $this->luminances[(int)($offset + $x)] = (($r+128) % 256) - 128;
  113. } else {
  114. // Calculate luminance cheaply, favoring green.
  115. $this->luminances[(int)($offset + $x)] = (((($r + 2 * $g + $b) / 4)+128)%256) - 128;
  116. }
  117. }
  118. */
  119. //}
  120. // $this->luminances = $this->grayScaleToBitmap($this->luminances);
  121. }
  122. public function grayscale()
  123. {
  124. $width = $this->dataWidth;
  125. $height = $this->dataHeight;
  126. $ret = fill_array(0, $width * $height, 0);
  127. for ($y = 0; $y < $height; $y++) {
  128. for ($x = 0; $x < $width; $x++) {
  129. $gray = $this->getPixel($x, $y, $width, $height);
  130. $ret[$x + $y * $width] = $gray;
  131. }
  132. }
  133. return $ret;
  134. }
  135. public function getPixel($x, $y, $width, $height)
  136. {
  137. $image = $this->pixels;
  138. if ($width < $x) {
  139. die('error');
  140. }
  141. if ($height < $y) {
  142. die('error');
  143. }
  144. $point = ($x) + ($y * $width);
  145. $r = $image[$point]['red'];//($image[$point] >> 16) & 0xff;
  146. $g = $image[$point]['green'];//($image[$point] >> 8) & 0xff;
  147. $b = $image[$point]['blue'];//$image[$point] & 0xff;
  148. $p = (int)(($r * 33 + $g * 34 + $b * 33) / 100);
  149. return $p;
  150. }
  151. public function grayScaleToBitmap($grayScale)
  152. {
  153. $middle = $this->getMiddleBrightnessPerArea($grayScale);
  154. $sqrtNumArea = is_countable($middle) ? count($middle) : 0;
  155. $areaWidth = floor($this->dataWidth / $sqrtNumArea);
  156. $areaHeight = floor($this->dataHeight / $sqrtNumArea);
  157. $bitmap = fill_array(0, $this->dataWidth * $this->dataHeight, 0);
  158. for ($ay = 0; $ay < $sqrtNumArea; $ay++) {
  159. for ($ax = 0; $ax < $sqrtNumArea; $ax++) {
  160. for ($dy = 0; $dy < $areaHeight; $dy++) {
  161. for ($dx = 0; $dx < $areaWidth; $dx++) {
  162. $bitmap[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] = ($grayScale[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)] < $middle[$ax][$ay]) ? 0 : 255;
  163. }
  164. }
  165. }
  166. }
  167. return $bitmap;
  168. }
  169. public function getMiddleBrightnessPerArea($image)
  170. {
  171. $numSqrtArea = 4;
  172. //obtain middle brightness((min + max) / 2) per area
  173. $areaWidth = floor($this->dataWidth / $numSqrtArea);
  174. $areaHeight = floor($this->dataHeight / $numSqrtArea);
  175. $minmax = fill_array(0, $numSqrtArea, 0);
  176. for ($i = 0; $i < $numSqrtArea; $i++) {
  177. $minmax[$i] = fill_array(0, $numSqrtArea, 0);
  178. for ($i2 = 0; $i2 < $numSqrtArea; $i2++) {
  179. $minmax[$i][$i2] = [0, 0];
  180. }
  181. }
  182. for ($ay = 0; $ay < $numSqrtArea; $ay++) {
  183. for ($ax = 0; $ax < $numSqrtArea; $ax++) {
  184. $minmax[$ax][$ay][0] = 0xFF;
  185. for ($dy = 0; $dy < $areaHeight; $dy++) {
  186. for ($dx = 0; $dx < $areaWidth; $dx++) {
  187. $target = $image[(int)($areaWidth * $ax + $dx + ($areaHeight * $ay + $dy) * $this->dataWidth)];
  188. if ($target < $minmax[$ax][$ay][0]) {
  189. $minmax[$ax][$ay][0] = $target;
  190. }
  191. if ($target > $minmax[$ax][$ay][1]) {
  192. $minmax[$ax][$ay][1] = $target;
  193. }
  194. }
  195. }
  196. //minmax[ax][ay][0] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
  197. }
  198. }
  199. $middle = [];
  200. for ($i3 = 0; $i3 < $numSqrtArea; $i3++) {
  201. $middle[$i3] = [];
  202. }
  203. for ($ay = 0; $ay < $numSqrtArea; $ay++) {
  204. for ($ax = 0; $ax < $numSqrtArea; $ax++) {
  205. $middle[$ax][$ay] = floor(($minmax[$ax][$ay][0] + $minmax[$ax][$ay][1]) / 2);
  206. //Console.out.print(middle[ax][ay] + ",");
  207. }
  208. //Console.out.println("");
  209. }
  210. //Console.out.println("");
  211. return $middle;
  212. }
  213. //@Override
  214. public function getRow($y, $row = null)
  215. {
  216. if ($y < 0 || $y >= $this->getHeight()) {
  217. throw new \InvalidArgumentException("Requested row is outside the image: " + \Y);
  218. }
  219. $width = $this->getWidth();
  220. if ($row == null || (is_countable($row) ? count($row) : 0) < $width) {
  221. $row = [];
  222. }
  223. $offset = ($y + $this->top) * $this->dataWidth + $this->left;
  224. $row = arraycopy($this->luminances, $offset, $row, 0, $width);
  225. return $row;
  226. }
  227. //@Override
  228. public function getMatrix()
  229. {
  230. $width = $this->getWidth();
  231. $height = $this->getHeight();
  232. // If the caller asks for the entire underlying image, save the copy and give them the
  233. // original data. The docs specifically warn that result.length must be ignored.
  234. if ($width == $this->dataWidth && $height == $this->dataHeight) {
  235. return $this->luminances;
  236. }
  237. $area = $width * $height;
  238. $matrix = [];
  239. $inputOffset = $this->top * $this->dataWidth + $this->left;
  240. // If the width matches the full width of the underlying data, perform a single copy.
  241. if ($width == $this->dataWidth) {
  242. $matrix = arraycopy($this->luminances, $inputOffset, $matrix, 0, $area);
  243. return $matrix;
  244. }
  245. // Otherwise copy one cropped row at a time.
  246. $rgb = $this->luminances;
  247. for ($y = 0; $y < $height; $y++) {
  248. $outputOffset = $y * $width;
  249. $matrix = arraycopy($rgb, $inputOffset, $matrix, $outputOffset, $width);
  250. $inputOffset += $this->dataWidth;
  251. }
  252. return $matrix;
  253. }
  254. //@Override
  255. public function isCropSupported()
  256. {
  257. return true;
  258. }
  259. //@Override
  260. public function crop($left, $top, $width, $height): \Zxing\RGBLuminanceSource
  261. {
  262. return new RGBLuminanceSource(
  263. $this->luminances,
  264. $this->dataWidth,
  265. $this->dataHeight,
  266. $this->left + $left,
  267. $this->top + $top,
  268. $width,
  269. $height
  270. );
  271. }
  272. }