|
| 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 | + |
| 18 | +namespace Zxing; |
| 19 | + |
| 20 | +use Zxing\Common\BitArray; |
| 21 | +use Zxing\Common\BitMatrix; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects |
| 26 | + * accept a BinaryBitmap and attempt to decode it. |
| 27 | + * |
| 28 | + * @author [email protected] (Daniel Switkin) |
| 29 | + */ |
| 30 | +final class BinaryBitmap { |
| 31 | + |
| 32 | + private $binarizer; |
| 33 | + private $matrix; |
| 34 | + |
| 35 | + public function __construct($binarizer) { |
| 36 | + if ($binarizer == null) { |
| 37 | + throw new \InvalidArgumentException("Binarizer must be non-null."); |
| 38 | + } |
| 39 | + $this->binarizer = $binarizer; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return The width of the bitmap. |
| 44 | + */ |
| 45 | + public function getWidth() { |
| 46 | + return $this->binarizer->getWidth(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return The height of the bitmap. |
| 51 | + */ |
| 52 | + public function getHeight() { |
| 53 | + return $this->binarizer->getHeight(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return |
| 58 | + * cached data. Callers should assume this method is expensive and call it as seldom as possible. |
| 59 | + * This method is intended for decoding 1D barcodes and may choose to apply sharpening. |
| 60 | + * |
| 61 | + * @param y The row to fetch, which must be in [0, bitmap height) |
| 62 | + * @param row An optional preallocated array. If null or too small, it will be ignored. |
| 63 | + * If used, the Binarizer will call BitArray.clear(). Always use the returned object. |
| 64 | + * @return The array of bits for this row (true means black). |
| 65 | + * @throws NotFoundException if row can't be binarized |
| 66 | + */ |
| 67 | + public function getBlackRow($y, $row) { |
| 68 | + return $this->binarizer->getBlackRow($y, $row); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive |
| 73 | + * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or |
| 74 | + * may not apply sharpening. Therefore, a row from this matrix may not be identical to one |
| 75 | + * fetched using getBlackRow(), so don't mix and match between them. |
| 76 | + * |
| 77 | + * @return The 2D array of bits for the image (true means black). |
| 78 | + * @throws NotFoundException if image can't be binarized to make a matrix |
| 79 | + */ |
| 80 | + public function getBlackMatrix(){ |
| 81 | +// The matrix is created on demand the first time it is requested, then cached. There are two |
| 82 | +// reasons for this: |
| 83 | +// 1. This work will never be done if the caller only installs 1D Reader objects, or if a |
| 84 | +// 1D Reader finds a barcode before the 2D Readers run. |
| 85 | +// 2. This work will only be done once even if the caller installs multiple 2D Readers. |
| 86 | + if ($this->matrix == null) { |
| 87 | + $this->matrix = $this->binarizer->getBlackMatrix(); |
| 88 | + } |
| 89 | + return $this->matrix; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return Whether this bitmap can be cropped. |
| 94 | + */ |
| 95 | + public function isCropSupported() { |
| 96 | + return $this->binarizer->getLuminanceSource()->isCropSupported(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns a new object with cropped image data. Implementations may keep a reference to the |
| 101 | + * original data rather than a copy. Only callable if isCropSupported() is true. |
| 102 | + * |
| 103 | + * @param left The left coordinate, which must be in [0,getWidth()) |
| 104 | + * @param top The top coordinate, which must be in [0,getHeight()) |
| 105 | + * @param width The width of the rectangle to crop. |
| 106 | + * @param height The height of the rectangle to crop. |
| 107 | + * @return A cropped version of this object. |
| 108 | + */ |
| 109 | + public function crop($left, $top, $width, $height) { |
| 110 | + $newSource = $this->binarizer->getLuminanceSource()->crop($left, $top, $width, $height); |
| 111 | + return new BinaryBitmap($this->binarizer->createBinarizer($newSource)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return Whether this bitmap supports counter-clockwise rotation. |
| 116 | + */ |
| 117 | + public function isRotateSupported() { |
| 118 | + return $this->binarizer->getLuminanceSource()->isRotateSupported(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns a new object with rotated image data by 90 degrees counterclockwise. |
| 123 | + * Only callable if {@link #isRotateSupported()} is true. |
| 124 | + * |
| 125 | + * @return A rotated version of this object. |
| 126 | + */ |
| 127 | + public function rotateCounterClockwise() { |
| 128 | + $newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise(); |
| 129 | + return new BinaryBitmap($this->binarizer->createBinarizer($newSource)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns a new object with rotated image data by 45 degrees counterclockwise. |
| 134 | + * Only callable if {@link #isRotateSupported()} is true. |
| 135 | + * |
| 136 | + * @return A rotated version of this object. |
| 137 | + */ |
| 138 | + public function rotateCounterClockwise45() { |
| 139 | + $newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise45(); |
| 140 | + return new BinaryBitmap($this->binarizer->createBinarizer($newSource)); |
| 141 | + } |
| 142 | + |
| 143 | +//@Override |
| 144 | + public function toString() { |
| 145 | + try { |
| 146 | + return $this->getBlackMatrix()->toString(); |
| 147 | + } catch (NotFoundException $e) { |
| 148 | + return ""; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments