diff options
Diffstat (limited to 'OpenPGP-Keychain/src/com/google/zxing/common/detector')
-rw-r--r-- | OpenPGP-Keychain/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java | 209 | ||||
-rw-r--r-- | OpenPGP-Keychain/src/com/google/zxing/common/detector/WhiteRectangleDetector.java | 347 |
2 files changed, 0 insertions, 556 deletions
diff --git a/OpenPGP-Keychain/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java b/OpenPGP-Keychain/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java deleted file mode 100644 index 950a22364..000000000 --- a/OpenPGP-Keychain/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright 2009 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.common.detector; - -import com.google.zxing.NotFoundException; -import com.google.zxing.ResultPoint; -import com.google.zxing.common.BitMatrix; - -/** - * <p>A somewhat generic detector that looks for a barcode-like rectangular region within an image. - * It looks within a mostly white region of an image for a region of black and white, but mostly - * black. It returns the four corners of the region, as best it can determine.</p> - * - * @author Sean Owen - */ -public final class MonochromeRectangleDetector { - - private static final int MAX_MODULES = 32; - - private final BitMatrix image; - - public MonochromeRectangleDetector(BitMatrix image) { - this.image = image; - } - - /** - * <p>Detects a rectangular region of black and white -- mostly black -- with a region of mostly - * white, in an image.</p> - * - * @return {@link ResultPoint}[] describing the corners of the rectangular region. The first and - * last points are opposed on the diagonal, as are the second and third. The first point will be - * the topmost point and the last, the bottommost. The second point will be leftmost and the - * third, the rightmost - * @throws NotFoundException if no Data Matrix Code can be found - */ - public ResultPoint[] detect() throws NotFoundException { - int height = image.getHeight(); - int width = image.getWidth(); - int halfHeight = height >> 1; - int halfWidth = width >> 1; - int deltaY = Math.max(1, height / (MAX_MODULES << 3)); - int deltaX = Math.max(1, width / (MAX_MODULES << 3)); - - int top = 0; - int bottom = height; - int left = 0; - int right = width; - ResultPoint pointA = findCornerFromCenter(halfWidth, 0, left, right, - halfHeight, -deltaY, top, bottom, halfWidth >> 1); - top = (int) pointA.getY() - 1; - ResultPoint pointB = findCornerFromCenter(halfWidth, -deltaX, left, right, - halfHeight, 0, top, bottom, halfHeight >> 1); - left = (int) pointB.getX() - 1; - ResultPoint pointC = findCornerFromCenter(halfWidth, deltaX, left, right, - halfHeight, 0, top, bottom, halfHeight >> 1); - right = (int) pointC.getX() + 1; - ResultPoint pointD = findCornerFromCenter(halfWidth, 0, left, right, - halfHeight, deltaY, top, bottom, halfWidth >> 1); - bottom = (int) pointD.getY() + 1; - - // Go try to find point A again with better information -- might have been off at first. - pointA = findCornerFromCenter(halfWidth, 0, left, right, - halfHeight, -deltaY, top, bottom, halfWidth >> 2); - - return new ResultPoint[] { pointA, pointB, pointC, pointD }; - } - - /** - * 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 ResultPoint findCornerFromCenter(int centerX, int deltaX, int left, int right, - int centerY, int deltaY, int top, int bottom, int maxWhiteRun) throws NotFoundException { - int[] lastRange = null; - for (int y = centerY, x = centerX; - y < bottom && y >= top && x < right && x >= left; - y += deltaY, x += deltaX) { - int[] range; - if (deltaX == 0) { - // horizontal slices, up and down - range = blackWhiteRange(y, maxWhiteRun, left, right, true); - } else { - // vertical slices, left and right - range = blackWhiteRange(x, maxWhiteRun, top, bottom, false); - } - if (range == null) { - if (lastRange == null) { - throw NotFoundException.getNotFoundInstance(); - } - // lastRange was found - if (deltaX == 0) { - int 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 { - int 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(); - } - - /** - * Computes the start and end of a region of pixels, either horizontally or vertically, that could - * be part of a Data Matrix barcode. - * - * @param fixedDimension if scanning horizontally, this is the row (the fixed vertical location) - * where we are scanning. If scanning vertically it's the column, the fixed horizontal location - * @param maxWhiteRun largest run of white pixels that can still be considered part of the - * barcode region - * @param minDim minimum pixel location, horizontally or vertically, to consider - * @param maxDim maximum pixel location, horizontally or vertically, to consider - * @param horizontal if true, we're scanning left-right, instead of up-down - * @return int[] with start and end of found range, or null if no such range is found - * (e.g. only white was found) - */ - private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, - boolean horizontal) { - - int center = (minDim + maxDim) >> 1; - - // Scan left/up first - int start = center; - while (start >= minDim) { - if (horizontal ? image.get(start, fixedDimension) : image.get(fixedDimension, start)) { - start--; - } else { - int whiteRunStart = start; - do { - start--; - } while (start >= minDim && !(horizontal ? image.get(start, fixedDimension) : - image.get(fixedDimension, start))); - int whiteRunSize = whiteRunStart - start; - if (start < minDim || whiteRunSize > maxWhiteRun) { - start = whiteRunStart; - break; - } - } - } - start++; - - // Then try right/down - int end = center; - while (end < maxDim) { - if (horizontal ? image.get(end, fixedDimension) : image.get(fixedDimension, end)) { - end++; - } else { - int whiteRunStart = end; - do { - end++; - } while (end < maxDim && !(horizontal ? image.get(end, fixedDimension) : - image.get(fixedDimension, end))); - int whiteRunSize = end - whiteRunStart; - if (end >= maxDim || whiteRunSize > maxWhiteRun) { - end = whiteRunStart; - break; - } - } - } - end--; - - return end > start ? new int[]{start, end} : null; - } - -}
\ No newline at end of file diff --git a/OpenPGP-Keychain/src/com/google/zxing/common/detector/WhiteRectangleDetector.java b/OpenPGP-Keychain/src/com/google/zxing/common/detector/WhiteRectangleDetector.java deleted file mode 100644 index 31d87e9d0..000000000 --- a/OpenPGP-Keychain/src/com/google/zxing/common/detector/WhiteRectangleDetector.java +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright 2010 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.common.detector; - -import com.google.zxing.NotFoundException; -import com.google.zxing.ResultPoint; -import com.google.zxing.common.BitMatrix; - -/** - * <p> - * Detects a candidate barcode-like rectangular region within an image. It - * starts around the center of the image, increases the size of the candidate - * region until it finds a white rectangular region. By keeping track of the - * last black points it encountered, it determines the corners of the barcode. - * </p> - * - * @author David Olivier - */ -public final class WhiteRectangleDetector { - - private static final int INIT_SIZE = 30; - private static final int CORR = 1; - - private final BitMatrix image; - private final int height; - private final int width; - private final int leftInit; - private final int rightInit; - private final int downInit; - private final int upInit; - - /** - * @throws NotFoundException if image is too small - */ - public WhiteRectangleDetector(BitMatrix image) throws NotFoundException { - this.image = image; - height = image.getHeight(); - width = image.getWidth(); - leftInit = (width - INIT_SIZE) >> 1; - rightInit = (width + INIT_SIZE) >> 1; - upInit = (height - INIT_SIZE) >> 1; - downInit = (height + INIT_SIZE) >> 1; - if (upInit < 0 || leftInit < 0 || downInit >= height || rightInit >= width) { - throw NotFoundException.getNotFoundInstance(); - } - } - - /** - * @throws NotFoundException if image is too small - */ - public WhiteRectangleDetector(BitMatrix image, int initSize, int x, int y) throws NotFoundException { - this.image = image; - height = image.getHeight(); - width = image.getWidth(); - int halfsize = initSize >> 1; - leftInit = x - halfsize; - rightInit = x + halfsize; - upInit = y - halfsize; - downInit = y + halfsize; - if (upInit < 0 || leftInit < 0 || downInit >= height || rightInit >= width) { - throw NotFoundException.getNotFoundInstance(); - } - } - - /** - * <p> - * Detects a candidate barcode-like rectangular region within an image. It - * starts around the center of the image, increases the size of the candidate - * region until it finds a white rectangular region. - * </p> - * - * @return {@link ResultPoint[]} describing the corners of the rectangular - * region. The first and last points are opposed on the diagonal, as - * are the second and third. The first point will be the topmost - * point and the last, the bottommost. The second point will be - * leftmost and the third, the rightmost - * @throws NotFoundException if no Data Matrix Code can be found - */ - public ResultPoint[] detect() throws NotFoundException { - - int left = leftInit; - int right = rightInit; - int up = upInit; - int down = downInit; - boolean sizeExceeded = false; - boolean aBlackPointFoundOnBorder = true; - boolean atLeastOneBlackPointFoundOnBorder = false; - - while (aBlackPointFoundOnBorder) { - - aBlackPointFoundOnBorder = false; - - // ..... - // . | - // ..... - boolean rightBorderNotWhite = true; - while (rightBorderNotWhite && right < width) { - rightBorderNotWhite = containsBlackPoint(up, down, right, false); - if (rightBorderNotWhite) { - right++; - aBlackPointFoundOnBorder = true; - } - } - - if (right >= width) { - sizeExceeded = true; - break; - } - - // ..... - // . . - // .___. - boolean bottomBorderNotWhite = true; - while (bottomBorderNotWhite && down < height) { - bottomBorderNotWhite = containsBlackPoint(left, right, down, true); - if (bottomBorderNotWhite) { - down++; - aBlackPointFoundOnBorder = true; - } - } - - if (down >= height) { - sizeExceeded = true; - break; - } - - // ..... - // | . - // ..... - boolean leftBorderNotWhite = true; - while (leftBorderNotWhite && left >= 0) { - leftBorderNotWhite = containsBlackPoint(up, down, left, false); - if (leftBorderNotWhite) { - left--; - aBlackPointFoundOnBorder = true; - } - } - - if (left < 0) { - sizeExceeded = true; - break; - } - - // .___. - // . . - // ..... - boolean topBorderNotWhite = true; - while (topBorderNotWhite && up >= 0) { - topBorderNotWhite = containsBlackPoint(left, right, up, true); - if (topBorderNotWhite) { - up--; - aBlackPointFoundOnBorder = true; - } - } - - if (up < 0) { - sizeExceeded = true; - break; - } - - if (aBlackPointFoundOnBorder) { - atLeastOneBlackPointFoundOnBorder = true; - } - - } - - if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) { - - int maxSize = right - left; - - ResultPoint z = null; - for (int i = 1; i < maxSize; i++) { - z = getBlackPointOnSegment(left, down - i, left + i, down); - if (z != null) { - break; - } - } - - if (z == null) { - throw NotFoundException.getNotFoundInstance(); - } - - ResultPoint t = null; - //go down right - for (int i = 1; i < maxSize; i++) { - t = getBlackPointOnSegment(left, up + i, left + i, up); - if (t != null) { - break; - } - } - - if (t == null) { - throw NotFoundException.getNotFoundInstance(); - } - - ResultPoint x = null; - //go down left - for (int i = 1; i < maxSize; i++) { - x = getBlackPointOnSegment(right, up + i, right - i, up); - if (x != null) { - break; - } - } - - if (x == null) { - throw NotFoundException.getNotFoundInstance(); - } - - ResultPoint y = null; - //go up left - for (int i = 1; i < maxSize; i++) { - y = getBlackPointOnSegment(right, down - i, right - i, down); - if (y != null) { - break; - } - } - - if (y == null) { - throw NotFoundException.getNotFoundInstance(); - } - - return centerEdges(y, z, x, t); - - } else { - throw NotFoundException.getNotFoundInstance(); - } - } - - /** - * Ends up being a bit faster than Math.round(). This merely rounds its - * argument to the nearest int, where x.5 rounds up. - */ - private static int round(float d) { - return (int) (d + 0.5f); - } - - private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) { - int dist = distanceL2(aX, aY, bX, bY); - float xStep = (bX - aX) / dist; - float yStep = (bY - aY) / dist; - - for (int i = 0; i < dist; i++) { - int x = round(aX + i * xStep); - int y = round(aY + i * yStep); - if (image.get(x, y)) { - return new ResultPoint(x, y); - } - } - return null; - } - - private static int distanceL2(float aX, float aY, float bX, float bY) { - float xDiff = aX - bX; - float yDiff = aY - bY; - return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff)); - } - - /** - * recenters the points of a constant distance towards the center - * - * @param y bottom most point - * @param z left most point - * @param x right most point - * @param t top most point - * @return {@link ResultPoint}[] describing the corners of the rectangular - * region. The first and last points are opposed on the diagonal, as - * are the second and third. The first point will be the topmost - * point and the last, the bottommost. The second point will be - * leftmost and the third, the rightmost - */ - private ResultPoint[] centerEdges(ResultPoint y, ResultPoint z, - ResultPoint x, ResultPoint t) { - - // - // t t - // z x - // x OR z - // y y - // - - float yi = y.getX(); - float yj = y.getY(); - float zi = z.getX(); - float zj = z.getY(); - float xi = x.getX(); - float xj = x.getY(); - float ti = t.getX(); - float tj = t.getY(); - - if (yi < width / 2) { - return new ResultPoint[]{ - new ResultPoint(ti - CORR, tj + CORR), - new ResultPoint(zi + CORR, zj + CORR), - new ResultPoint(xi - CORR, xj - CORR), - new ResultPoint(yi + CORR, yj - CORR)}; - } else { - return new ResultPoint[]{ - new ResultPoint(ti + CORR, tj + CORR), - new ResultPoint(zi + CORR, zj - CORR), - new ResultPoint(xi - CORR, xj + CORR), - new ResultPoint(yi - CORR, yj - CORR)}; - } - } - - /** - * Determines whether a segment contains a black point - * - * @param a min value of the scanned coordinate - * @param b max value of the scanned coordinate - * @param fixed value of fixed coordinate - * @param horizontal set to true if scan must be horizontal, false if vertical - * @return true if a black point has been found, else false. - */ - private boolean containsBlackPoint(int a, int b, int fixed, boolean horizontal) { - - if (horizontal) { - for (int x = a; x <= b; x++) { - if (image.get(x, fixed)) { - return true; - } - } - } else { - for (int y = a; y <= b; y++) { - if (image.get(fixed, y)) { - return true; - } - } - } - - return false; - } - -}
\ No newline at end of file |