public get_Renamed ( int x, int y ) : bool | ||
x | int | The horizontal component (i.e. which column) /// |
y | int | The vertical component (i.e. which row) /// |
return | bool |
public override BitMatrix sampleGrid(BitMatrix image, int dimension, PerspectiveTransform transform) { var bits = new BitMatrix(dimension); var points = new float[dimension << 1]; for (int y = 0; y < dimension; y++) { int max = points.Length; float iValue = y + 0.5f; for (int x = 0; x < max; x += 2) { points[x] = (x >> 1) + 0.5f; points[x + 1] = iValue; } transform.transformPoints(points); // Quick check to see if points transformed to something inside the image; // sufficient to check the endpoints checkAndNudgePoints(image, points); try { for (int x = 0; x < max; x += 2) { if (image.get_Renamed((int) points[x], (int) points[x + 1])) { // Black(-ish) pixel bits.set_Renamed(x >> 1, y); } } } catch (IndexOutOfRangeException) { // 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 ReaderException.Instance; } } return bits; }
/// <summary> This method detects a barcode in a "pure" image -- that is, pure monochrome image /// which contains only an unrotated, unskewed, image of a barcode, with some white border /// around it. This is a specialized method that works exceptionally fast in this special /// case. /// </summary> private static BitMatrix extractPureBits(BitMatrix image) { // Now need to determine module size in pixels int height = image.Height; int width = image.Width; int minDimension = Math.Min(height, width); // First, skip white border by tracking diagonally from the top left down and to the right: int borderWidth = 0; while (borderWidth < minDimension && !image.get_Renamed(borderWidth, borderWidth)) { borderWidth++; } if (borderWidth == minDimension) { throw ReaderException.Instance; } // And then keep tracking across the top-left black module to determine module size int moduleEnd = borderWidth; while (moduleEnd < minDimension && image.get_Renamed(moduleEnd, moduleEnd)) { moduleEnd++; } if (moduleEnd == minDimension) { throw ReaderException.Instance; } int moduleSize = moduleEnd - borderWidth; // And now find where the rightmost black module on the first row ends int rowEndOfSymbol = width - 1; while (rowEndOfSymbol >= 0 && !image.get_Renamed(rowEndOfSymbol, borderWidth)) { rowEndOfSymbol--; } if (rowEndOfSymbol < 0) { throw ReaderException.Instance; } rowEndOfSymbol++; // Make sure width of barcode is a multiple of module size if ((rowEndOfSymbol - borderWidth)%moduleSize != 0) { throw ReaderException.Instance; } int dimension = (rowEndOfSymbol - borderWidth)/moduleSize; // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. borderWidth += (moduleSize >> 1); int sampleDimension = borderWidth + (dimension - 1)*moduleSize; if (sampleDimension >= width || sampleDimension >= height) { throw ReaderException.Instance; } // Now just read off the bits var bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { int iOffset = borderWidth + i*moduleSize; for (int j = 0; j < dimension; j++) { if (image.get_Renamed(borderWidth + j*moduleSize, iOffset)) { bits.set_Renamed(j, i); } } } return bits; }
/// <param name="matrix">row of black/white values to search /// </param> /// <param name="column">x position to start search /// </param> /// <param name="row">y position to start search /// </param> /// <param name="width">the number of pixels to search on this row /// </param> /// <param name="pattern">pattern of counts of number of black and white pixels that are /// being searched for as a pattern /// </param> /// <returns> start/end horizontal offset of guard pattern, as an array of two ints. /// </returns> private static int[] findGuardPattern(BitMatrix matrix, int column, int row, int width, bool whiteFirst, int[] pattern) { int patternLength = pattern.Length; // TODO: Find a way to cache this array, as this method is called hundreds of times // per image, and we want to allocate as seldom as possible. var counters = new int[patternLength]; bool isWhite = whiteFirst; int counterPosition = 0; int patternStart = column; for (int x = column; x < column + width; x++) { bool pixel = matrix.get_Renamed(x, row); if (pixel ^ isWhite) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) { return new[] {patternStart, x}; } patternStart += counters[0] + counters[1]; for (int y = 2; y < patternLength; y++) { counters[y - 2] = counters[y]; } counters[patternLength - 2] = 0; counters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } return null; }