package com.google.zxing.qrcode.detector;
import com.google.zxing.DecodeHintType;
-import com.google.zxing.ReaderException;
+import com.google.zxing.FormatException;
+import com.google.zxing.NotFoundException;
import com.google.zxing.ResultPoint;
import com.google.zxing.ResultPointCallback;
import com.google.zxing.common.BitMatrix;
* <p>Detects a QR Code in an image, simply.</p>
*
* @return {@link DetectorResult} encapsulating results of detecting a QR Code
- * @throws ReaderException if no QR Code can be found
+ * @throws NotFoundException if no QR Code can be found
*/
- public DetectorResult detect() throws ReaderException {
+ public DetectorResult detect() throws NotFoundException, FormatException {
return detect(null);
}
* <p>Detects a QR Code in an image, simply.</p>
*
* @param hints optional hints to detector
- * @return {@link DetectorResult} encapsulating results of detecting a QR Code
- * @throws ReaderException if no QR Code can be found
+ * @return {@link NotFoundException} encapsulating results of detecting a QR Code
+ * @throws NotFoundException if QR Code cannot be found
+ * @throws FormatException if a QR Code cannot be decoded
*/
- public DetectorResult detect(Hashtable hints) throws ReaderException {
+ public DetectorResult detect(Hashtable hints) throws NotFoundException, FormatException {
resultPointCallback = hints == null ? null :
(ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
return processFinderPatternInfo(info);
}
- protected DetectorResult processFinderPatternInfo(FinderPatternInfo info) throws ReaderException {
+ protected DetectorResult processFinderPatternInfo(FinderPatternInfo info)
+ throws NotFoundException, FormatException {
FinderPattern topLeft = info.getTopLeft();
FinderPattern topRight = info.getTopRight();
float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft);
if (moduleSize < 1.0f) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
int dimension = computeDimension(topLeft, topRight, bottomLeft, moduleSize);
Version provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
estAlignmentY,
(float) i);
break;
- } catch (ReaderException re) {
+ } catch (NotFoundException re) {
// try next round
}
}
sourceBottomRightX = sourceBottomRightY = dimMinusThree;
}
- PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
+ return PerspectiveTransform.quadrilateralToQuadrilateral(
3.5f,
3.5f,
dimMinusThree,
bottomRightY,
bottomLeft.getX(),
bottomLeft.getY());
-
- return transform;
}
private static BitMatrix sampleGrid(BitMatrix image,
PerspectiveTransform transform,
- int dimension) throws ReaderException {
+ int dimension) throws NotFoundException {
GridSampler sampler = GridSampler.getInstance();
- return sampler.sampleGrid(image, dimension, transform);
+ return sampler.sampleGrid(image, dimension, dimension, transform);
}
/**
protected static int computeDimension(ResultPoint topLeft,
ResultPoint topRight,
ResultPoint bottomLeft,
- float moduleSize) throws ReaderException {
+ float moduleSize) throws NotFoundException {
int tltrCentersDimension = round(ResultPoint.distance(topLeft, topRight) / moduleSize);
int tlblCentersDimension = round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
dimension--;
break;
case 3:
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
return dimension;
}
if (otherToX < 0) {
scale = (float) fromX / (float) (fromX - otherToX);
otherToX = 0;
- } else if (otherToX >= image.getWidth()) {
- scale = (float) (image.getWidth() - 1 - fromX) / (float) (otherToX - fromX);
- otherToX = image.getWidth() - 1;
+ } else if (otherToX > image.getWidth()) {
+ scale = (float) (image.getWidth() - fromX) / (float) (otherToX - fromX);
+ otherToX = image.getWidth();
}
int otherToY = (int) (fromY - (toY - fromY) * scale);
if (otherToY < 0) {
scale = (float) fromY / (float) (fromY - otherToY);
otherToY = 0;
- } else if (otherToY >= image.getHeight()) {
- scale = (float) (image.getHeight() - 1 - fromY) / (float) (otherToY - fromY);
- otherToY = image.getHeight() - 1;
+ } else if (otherToY > image.getHeight()) {
+ scale = (float) (image.getHeight() - fromY) / (float) (otherToY - fromY);
+ otherToY = image.getHeight();
}
otherToX = (int) (fromX + (otherToX - fromX) * scale);
result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
- return result - 1.0f; // -1 because we counted the middle pixel twice
+ return result;
}
/**
if (state == 3) { // Found black, white, black, and stumbled back onto white; done
int diffX = x - fromX;
int diffY = y - fromY;
+ if (xstep < 0) {
+ diffX++;
+ }
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
}
error += dy;
* @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 ReaderException if an unexpected error occurs during detection
+ * @throws NotFoundException if an unexpected error occurs during detection
*/
protected AlignmentPattern findAlignmentInRegion(float overallEstModuleSize,
int estAlignmentX,
int estAlignmentY,
float allowanceFactor)
- throws ReaderException {
+ throws NotFoundException {
// Look for an alignment pattern (3 modules in size) around where it
// should be
int allowance = (int) (allowanceFactor * overallEstModuleSize);
int alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance);
int alignmentAreaRightX = Math.min(image.getWidth() - 1, estAlignmentX + allowance);
if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
int alignmentAreaTopY = Math.max(0, estAlignmentY - allowance);
int alignmentAreaBottomY = Math.min(image.getHeight() - 1, estAlignmentY + allowance);
+ if (alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize * 3) {
+ throw NotFoundException.getNotFoundInstance();
+ }
AlignmentPatternFinder alignmentFinder =
new AlignmentPatternFinder(