- /**\r
- * This method detects a barcode in a "pure" image -- that is, pure monochrome image\r
- * which contains only an unrotated, unskewed, image of a barcode, with some white border\r
- * around it. This is a specialized method that works exceptionally fast in this special\r
- * case.\r
- */\r
- private static BitMatrix extractPureBits(BinaryBitmap image) throws NotFoundException {\r
- // Now need to determine module size in pixels\r
- BitMatrix matrix = image.getBlackMatrix();\r
- int height = matrix.getHeight();\r
- int width = matrix.getWidth();\r
- int minDimension = Math.min(height, width);\r
-\r
- // First, skip white border by tracking diagonally from the top left down and to the right:\r
- int borderWidth = 0;\r
- while (borderWidth < minDimension && !matrix.get(borderWidth, borderWidth)) {\r
- borderWidth++;\r
- }\r
- if (borderWidth == minDimension) {\r
- throw NotFoundException.getNotFoundInstance();\r
- }\r
-\r
- // And then keep tracking across the top-left black module to determine module size\r
- int moduleEnd = borderWidth;\r
- while (moduleEnd < minDimension && matrix.get(moduleEnd, moduleEnd)) {\r
- moduleEnd++;\r
- }\r
- if (moduleEnd == minDimension) {\r
- throw NotFoundException.getNotFoundInstance();\r
- }\r
-\r
- int moduleSize = moduleEnd - borderWidth;\r
-\r
- // And now find where the rightmost black module on the first row ends\r
- int rowEndOfSymbol = width - 1;\r
- while (rowEndOfSymbol >= 0 && !matrix.get(rowEndOfSymbol, borderWidth)) {\r
- rowEndOfSymbol--;\r
- }\r
- if (rowEndOfSymbol < 0) {\r
- throw NotFoundException.getNotFoundInstance();\r
- }\r
- rowEndOfSymbol++;\r
-\r
- // Make sure width of barcode is a multiple of module size\r
- if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {\r
- throw NotFoundException.getNotFoundInstance();\r
- }\r
- int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;\r
-\r
- // Push in the "border" by half the module width so that we start\r
- // sampling in the middle of the module. Just in case the image is a\r
- // little off, this will help recover.\r
- borderWidth += moduleSize >> 1;\r
-\r
- int sampleDimension = borderWidth + (dimension - 1) * moduleSize;\r
- if (sampleDimension >= width || sampleDimension >= height) {\r
- throw NotFoundException.getNotFoundInstance();\r
- }\r
-\r
- // Now just read off the bits\r
- BitMatrix bits = new BitMatrix(dimension);\r
- for (int y = 0; y < dimension; y++) {\r
- int iOffset = borderWidth + y * moduleSize;\r
- for (int x = 0; x < dimension; x++) {\r
- if (matrix.get(borderWidth + x * moduleSize, iOffset)) {\r
- bits.set(x, y);\r
- }\r
- }\r
- }\r
- return bits;\r
- }\r