Class Detector
- java.lang.Object
-
- com.google.zxing.qrcode.detector.Detector
-
- Direct Known Subclasses:
MultiDetector
public class Detector extends java.lang.Object
Encapsulates logic that can detect a QR Code in an image, even if the QR Code is rotated or skewed, or partially obscured.
-
-
Field Summary
Fields Modifier and Type Field Description private BitMatrix
image
private ResultPointCallback
resultPointCallback
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected float
calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft)
Computes an average estimated module size based on estimated derived from the positions of the three finder patterns.private float
calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern)
Estimates module size based on two finder patterns -- it usessizeOfBlackWhiteBlackRunBothWays(int, int, int, int)
to figure the width of each, measuring along the axis between their centers.private static int
computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, float moduleSize)
Computes the dimension (number of modules on a size) of the QR Code based on the position of the finder patterns and estimated module size.private static PerspectiveTransform
createTransform(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint alignmentPattern, int dimension)
DetectorResult
detect()
Detects a QR Code in an image.DetectorResult
detect(java.util.Map<DecodeHintType,?> hints)
Detects a QR Code in an image.protected AlignmentPattern
findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY, float allowanceFactor)
Attempts to locate an alignment pattern in a limited region of the image, which is guessed to contain it.protected BitMatrix
getImage()
protected ResultPointCallback
getResultPointCallback()
protected DetectorResult
processFinderPatternInfo(FinderPatternInfo info)
private static BitMatrix
sampleGrid(BitMatrix image, PerspectiveTransform transform, int dimension)
private float
sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
This method traces a line from a point in the image, in the direction towards another point.private float
sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY)
SeesizeOfBlackWhiteBlackRun(int, int, int, int)
; computes the total width of a finder pattern by looking for a black-white-black run from the center in the direction of another point (another finder pattern center), and in the opposite direction too.
-
-
-
Field Detail
-
image
private final BitMatrix image
-
resultPointCallback
private ResultPointCallback resultPointCallback
-
-
Constructor Detail
-
Detector
public Detector(BitMatrix image)
-
-
Method Detail
-
getImage
protected final BitMatrix getImage()
-
getResultPointCallback
protected final ResultPointCallback getResultPointCallback()
-
detect
public DetectorResult detect() throws NotFoundException, FormatException
Detects a QR Code in an image.
- Returns:
DetectorResult
encapsulating results of detecting a QR Code- Throws:
NotFoundException
- if QR Code cannot be foundFormatException
- if a QR Code cannot be decoded
-
detect
public final DetectorResult detect(java.util.Map<DecodeHintType,?> hints) throws NotFoundException, FormatException
Detects a QR Code in an image.
- Parameters:
hints
- optional hints to detector- Returns:
DetectorResult
encapsulating results of detecting a QR Code- Throws:
NotFoundException
- if QR Code cannot be foundFormatException
- if a QR Code cannot be decoded
-
processFinderPatternInfo
protected final DetectorResult processFinderPatternInfo(FinderPatternInfo info) throws NotFoundException, FormatException
- Throws:
NotFoundException
FormatException
-
createTransform
private static PerspectiveTransform createTransform(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint alignmentPattern, int dimension)
-
sampleGrid
private static BitMatrix sampleGrid(BitMatrix image, PerspectiveTransform transform, int dimension) throws NotFoundException
- Throws:
NotFoundException
-
computeDimension
private static int computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, float moduleSize) throws NotFoundException
Computes the dimension (number of modules on a size) of the QR Code based on the position of the finder patterns and estimated module size.
- Throws:
NotFoundException
-
calculateModuleSize
protected final float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft)
Computes an average estimated module size based on estimated derived from the positions of the three finder patterns.
- Parameters:
topLeft
- detected top-left finder pattern centertopRight
- detected top-right finder pattern centerbottomLeft
- detected bottom-left finder pattern center- Returns:
- estimated module size
-
calculateModuleSizeOneWay
private float calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern)
Estimates module size based on two finder patterns -- it uses
sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)
to figure the width of each, measuring along the axis between their centers.
-
sizeOfBlackWhiteBlackRunBothWays
private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY)
SeesizeOfBlackWhiteBlackRun(int, int, int, int)
; computes the total width of a finder pattern by looking for a black-white-black run from the center in the direction of another point (another finder pattern center), and in the opposite direction too.
-
sizeOfBlackWhiteBlackRun
private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
This method traces a line from a point in the image, in the direction towards another point. It begins in a black region, and keeps going until it finds white, then black, then white again. It reports the distance from the start to this point.
This is used when figuring out how wide a finder pattern is, when the finder pattern may be skewed or rotated.
-
findAlignmentInRegion
protected final AlignmentPattern findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY, float allowanceFactor) throws NotFoundException
Attempts to locate an alignment pattern in a limited region of the image, which is guessed to contain it. This method uses
AlignmentPattern
.- Parameters:
overallEstModuleSize
- estimated module size so farestAlignmentX
- x coordinate of center of area probably containing alignment patternestAlignmentY
- y coordinate of aboveallowanceFactor
- number of pixels in all directions to search from the center- Returns:
AlignmentPattern
if found, or null otherwise- Throws:
NotFoundException
- if an unexpected error occurs during detection
-
-