public function sampleGrid_($image, $dimensionX, $dimensionY, $transform) { if ($dimensionX <= 0 || $dimensionY <= 0) { throw NotFoundException::getNotFoundInstance(); } $bits = new BitMatrix($dimensionX, $dimensionY); $points = fill_array(0, 2 * $dimensionX, 0.0); for ($y = 0; $y < $dimensionY; $y++) { $max = count($points); $iValue = (double) $y + 0.5; for ($x = 0; $x < $max; $x += 2) { $points[$x] = (double) ($x / 2) + 0.5; $points[$x + 1] = $iValue; } $transform->transformPoints($points); // Quick check to see if points transformed to something inside the image; // sufficient to check the endpoints $this->checkAndNudgePoints($image, $points); try { for ($x = 0; $x < $max; $x += 2) { if ($image->get((int) $points[$x], (int) $points[$x + 1])) { // Black(-ish) pixel $bits->set($x / 2, $y); } } } catch (\Exception $aioobe) { //ArrayIndexOutOfBoundsException // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting // transform gets "twisted" such that it maps a straight line of points to a set of points // whose endpoints are in bounds, but others are not. There is probably some mathematical // way to detect this about the transformation that I don't know yet. // This results in an ugly runtime exception despite our clever checks above -- can't have // that. We could check each point's coordinates but that feels duplicative. We settle for // catching and wrapping ArrayIndexOutOfBoundsException. throw NotFoundException::getNotFoundInstance(); } } return $bits; }
/** * Attempts to locate a corner of the barcode by scanning up, down, left or right from a center * point which should be within the barcode. * * @param centerX center's x component (horizontal) * @param deltaX same as deltaY but change in x per step instead * @param left minimum value of x * @param right maximum value of x * @param centerY center's y component (vertical) * @param deltaY change in y per step. If scanning up this is negative; down, positive; * left or right, 0 * @param top minimum value of y to search through (meaningless when di == 0) * @param bottom maximum value of y * @param maxWhiteRun maximum run of white pixels that can still be considered to be within * the barcode * @return a {@link com.google.zxing.ResultPoint} encapsulating the corner that was found * @throws NotFoundException if such a point cannot be found */ private function findCornerFromCenter($centerX, $deltaX, $left, $right, $centerY, $deltaY, $top, $bottom, $maxWhiteRun) { $lastRange = null; for ($y = $centerY, $x = $centerX; $y < $bottom && $y >= $top && $x < $right && $x >= $left; $y += $deltaY, $x += $deltaX) { $range = 0; if ($deltaX == 0) { // horizontal slices, up and down $range = $this->blackWhiteRange($y, $maxWhiteRun, $left, $right, true); } else { // vertical slices, left and right $range = $this->blackWhiteRange($x, $maxWhiteRun, $top, $bottom, false); } if ($range == null) { if ($lastRange == null) { throw NotFoundException::getNotFoundInstance(); } // lastRange was found if ($deltaX == 0) { $lastY = $y - $deltaY; if ($lastRange[0] < $centerX) { if ($lastRange[1] > $centerX) { // straddle, choose one or the other based on direction return new ResultPoint($deltaY > 0 ? $lastRange[0] : $lastRange[1], $lastY); } return new ResultPoint($lastRange[0], $lastY); } else { return new ResultPoint($lastRange[1], $lastY); } } else { $lastX = $x - $deltaX; if ($lastRange[0] < $centerY) { if ($lastRange[1] > $centerY) { return new ResultPoint($lastX, $deltaX < 0 ? $lastRange[0] : $lastRange[1]); } return new ResultPoint($lastX, $lastRange[0]); } else { return new ResultPoint($lastX, $lastRange[1]); } } } $lastRange = $range; } throw NotFoundException::getNotFoundInstance(); }
/** * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since * it's pretty performance-critical and so is written to be fast foremost.</p> * * @return {@link AlignmentPattern} if found * @throws NotFoundException if not found */ function find() { $startX = $this->startX; $height = $this->height; $maxJ = $startX + $this->width; $middleI = $this->startY + $height / 2; // We are looking for black/white/black modules in 1:1:1 ratio; // this tracks the number of black/white/black modules seen so far $stateCount = array(); for ($iGen = 0; $iGen < $height; $iGen++) { // Search from middle outwards $i = $middleI + (($iGen & 0x1) == 0 ? ($iGen + 1) / 2 : -(($iGen + 1) / 2)); $i = intval($i); $stateCount[0] = 0; $stateCount[1] = 0; $stateCount[2] = 0; $j = $startX; // Burn off leading white pixels before anything else; if we start in the middle of // a white run, it doesn't make sense to count its length, since we don't know if the // white run continued to the left of the start point while ($j < $maxJ && !$this->image->get($j, $i)) { $j++; } $currentState = 0; while ($j < $maxJ) { if ($this->image->get($j, $i)) { // Black pixel if ($currentState == 1) { // Counting black pixels $stateCount[$currentState]++; } else { // Counting white pixels if ($currentState == 2) { // A winner? if ($this->foundPatternCross($stateCount)) { // Yes $confirmed = $this->handlePossibleCenter($stateCount, $i, $j); if ($confirmed != null) { return $confirmed; } } $stateCount[0] = $stateCount[2]; $stateCount[1] = 1; $stateCount[2] = 0; $currentState = 1; } else { $stateCount[++$currentState]++; } } } else { // White pixel if ($currentState == 1) { // Counting black pixels $currentState++; } $stateCount[$currentState]++; } $j++; } if ($this->foundPatternCross($stateCount)) { $confirmed = $this->handlePossibleCenter($stateCount, $i, $maxJ); if ($confirmed != null) { return $confirmed; } } } // Hmm, nothing we saw was observed and confirmed twice. If we had // any guess at all, return it. if (count($this->possibleCenters)) { return $this->possibleCenters[0]; } throw NotFoundException::getNotFoundInstance(); }
private static function moduleSize($leftTopBlack, $image) { $height = $image->getHeight(); $width = $image->getWidth(); $x = $leftTopBlack[0]; $y = $leftTopBlack[1]; $inBlack = true; $transitions = 0; while ($x < $width && $y < $height) { if ($inBlack != $image->get($x, $y)) { if (++$transitions == 5) { break; } $inBlack = !$inBlack; } $x++; $y++; } if ($x == $width || $y == $height) { throw NotFoundException::getNotFoundInstance(); } return ($x - $leftTopBlack[0]) / 7.0; //return ($x - $leftTopBlack[0]) / 7.0f; }
/** * <p>Checks a set of points that have been transformed to sample points on an image against * the image's dimensions to see if the point are even within the image.</p> * * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder * patterns in an image where the QR Code runs all the way to the image border.</p> * * <p>For efficiency, the method will check points from either end of the line until one is found * to be within the image. Because the set of points are assumed to be linear, this is valid.</p> * * @param image image into which the points should map * @param points actual points in x1,y1,...,xn,yn form * @throws NotFoundException if an endpoint is lies outside the image boundaries */ protected static function checkAndNudgePoints($image, $points) { $width = $image->getWidth(); $height = $image->getHeight(); // Check and nudge points from start until we see some that are OK: $nudged = true; for ($offset = 0; $offset < count($points) && $nudged; $offset += 2) { $x = (int) $points[$offset]; $y = (int) $points[$offset + 1]; if ($x < -1 || $x > $width || $y < -1 || $y > $height) { throw NotFoundException::getNotFoundInstance(); } $nudged = false; if ($x == -1) { $points[$offset] = 0.0; $nudged = true; } else { if ($x == $width) { $points[$offset] = $width - 1; $nudged = true; } } if ($y == -1) { $points[$offset + 1] = 0.0; $nudged = true; } else { if ($y == $height) { $points[$offset + 1] = $height - 1; $nudged = true; } } } // Check and nudge points from end: $nudged = true; for ($offset = count($points) - 2; $offset >= 0 && $nudged; $offset -= 2) { $x = (int) $points[$offset]; $y = (int) $points[$offset + 1]; if ($x < -1 || $x > $width || $y < -1 || $y > $height) { throw NotFoundException::getNotFoundInstance(); } $nudged = false; if ($x == -1) { $points[$offset] = 0.0; $nudged = true; } else { if ($x == $width) { $points[$offset] = $width - 1; $nudged = true; } } if ($y == -1) { $points[$offset + 1] = 0.0; $nudged = true; } else { if ($y == $height) { $points[$offset + 1] = $height - 1; $nudged = true; } } } }
/** * <p>Attempts to locate an alignment pattern in a limited region of the image, which is * guessed to contain it. This method uses {@link AlignmentPattern}.</p> * * @param overallEstModuleSize estimated module size so far * @param estAlignmentX x coordinate of center of area probably containing alignment pattern * @param estAlignmentY y coordinate of above * @param allowanceFactor number of pixels in all directions to search from the center * @return {@link AlignmentPattern} if found, or null otherwise * @throws NotFoundException if an unexpected error occurs during detection */ protected final function findAlignmentInRegion($overallEstModuleSize, $estAlignmentX, $estAlignmentY, $allowanceFactor) { // Look for an alignment pattern (3 modules in size) around where it // should be $allowance = (int) ($allowanceFactor * $overallEstModuleSize); $alignmentAreaLeftX = max(0, $estAlignmentX - $allowance); $alignmentAreaRightX = min($this->image->getWidth() - 1, $estAlignmentX + $allowance); if ($alignmentAreaRightX - $alignmentAreaLeftX < $overallEstModuleSize * 3) { throw NotFoundException::getNotFoundInstance(); } $alignmentAreaTopY = max(0, $estAlignmentY - $allowance); $alignmentAreaBottomY = min($this->image->getHeight() - 1, $estAlignmentY + $allowance); if ($alignmentAreaBottomY - $alignmentAreaTopY < $overallEstModuleSize * 3) { throw NotFoundException::getNotFoundInstance(); } $alignmentFinder = new AlignmentPatternFinder($this->image, $alignmentAreaLeftX, $alignmentAreaTopY, $alignmentAreaRightX - $alignmentAreaLeftX, $alignmentAreaBottomY - $alignmentAreaTopY, $overallEstModuleSize, $this->resultPointCallback); return $alignmentFinder->find(); }
private static function estimateBlackPoint($buckets) { // Find the tallest peak in the histogram. $numBuckets = count($buckets); $maxBucketCount = 0; $firstPeak = 0; $firstPeakSize = 0; for ($x = 0; $x < $numBuckets; $x++) { if ($buckets[$x] > $firstPeakSize) { $firstPeak = $x; $firstPeakSize = $buckets[$x]; } if ($buckets[$x] > $maxBucketCount) { $maxBucketCount = $buckets[$x]; } } // Find the second-tallest peak which is somewhat far from the tallest peak. $secondPeak = 0; $secondPeakScore = 0; for ($x = 0; $x < $numBuckets; $x++) { $distanceToBiggest = $x - $firstPeak; // Encourage more distant second peaks by multiplying by square of distance. $score = $buckets[$x] * $distanceToBiggest * $distanceToBiggest; if ($score > $secondPeakScore) { $secondPeak = $x; $secondPeakScore = $score; } } // Make sure firstPeak corresponds to the black peak. if ($firstPeak > $secondPeak) { $temp = $firstPeak; $firstPeak = $secondPeak; $secondPeak = $temp; } // If there is too little contrast in the image to pick a meaningful black point, throw rather // than waste time trying to decode the image, and risk false positives. if ($secondPeak - $firstPeak <= $numBuckets / 16) { throw NotFoundException::getNotFoundInstance(); } // Find a valley between them that is low and closer to the white peak. $bestValley = $secondPeak - 1; $bestValleyScore = -1; for ($x = $secondPeak - 1; $x > $firstPeak; $x--) { $fromFirst = $x - $firstPeak; $score = $fromFirst * $fromFirst * ($secondPeak - $x) * ($maxBucketCount - $buckets[$x]); if ($score > $bestValleyScore) { $bestValley = $x; $bestValleyScore = $score; } } return intval32bits($bestValley << self::$LUMINANCE_SHIFT); }