--- /dev/null
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using com.google.zxing;\r
+using com.google.zxing.common;\r
+using com.google.zxing.datamatrix.decoder;\r
+using com.google.zxing.datamatrix.detector;\r
+\r
+namespace com.google.zxing.datamatrix\r
+{\r
+ public sealed class DataMatrixReader\r
+ {\r
+ private static ResultPoint[] NO_POINTS = new ResultPoint[0];\r
+ private Decoder decoder = new Decoder();\r
+\r
+ /**\r
+ * Locates and decodes a Data Matrix code in an image.\r
+ *\r
+ * @return a String representing the content encoded by the Data Matrix code\r
+ * @throws ReaderException if a Data Matrix code cannot be found, or cannot be decoded\r
+ */\r
+ public Result decode(MonochromeBitmapSource image) {\r
+ return decode(image, null);\r
+ }\r
+\r
+ public Result decode(MonochromeBitmapSource image, System.Collections.Hashtable hints)\r
+ {\r
+ DecoderResult decoderResult;\r
+ ResultPoint[] points;\r
+ if (hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE)) {\r
+ BitMatrix bits = extractPureBits(image);\r
+ decoderResult = decoder.decode(bits);\r
+ points = NO_POINTS;\r
+ } else {\r
+ DetectorResult detectorResult = new Detector(image).detect();\r
+ decoderResult = decoder.decode(detectorResult.getBits());\r
+ points = detectorResult.getPoints();\r
+ }\r
+ Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX);\r
+ if (decoderResult.getByteSegments() != null) {\r
+ result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());\r
+ }\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image\r
+ * which contains only an unrotated, unskewed, image of a Data Matrix code, 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(MonochromeBitmapSource image) {\r
+ // Now need to determine module size in pixels\r
+\r
+ int height = image.getHeight();\r
+ int width = image.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 && !image.isBlack(borderWidth, borderWidth)) {\r
+ borderWidth++;\r
+ }\r
+ if (borderWidth == minDimension) {\r
+ throw new ReaderException();\r
+ }\r
+\r
+ // And then keep tracking across the top-left black module to determine module size\r
+ int moduleEnd = borderWidth + 1;\r
+ while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {\r
+ moduleEnd++;\r
+ }\r
+ if (moduleEnd == width) {\r
+ throw new ReaderException();\r
+ }\r
+\r
+ int moduleSize = moduleEnd - borderWidth;\r
+\r
+ // And now find where the bottommost black module on the first column ends\r
+ int columnEndOfSymbol = height - 1;\r
+ while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {\r
+ columnEndOfSymbol--;\r
+ }\r
+ if (columnEndOfSymbol < 0) {\r
+ throw new ReaderException();\r
+ }\r
+ columnEndOfSymbol++;\r
+\r
+ // Make sure width of barcode is a multiple of module size\r
+ if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {\r
+ throw new ReaderException();\r
+ }\r
+ int dimension = (columnEndOfSymbol - 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 new ReaderException();\r
+ }\r
+\r
+ // Now just read off the bits\r
+ BitMatrix bits = new BitMatrix(dimension);\r
+ for (int i = 0; i < dimension; i++) {\r
+ int iOffset = borderWidth + i * moduleSize;\r
+ for (int j = 0; j < dimension; j++) {\r
+ if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {\r
+ bits.set(i, j);\r
+ }\r
+ }\r
+ }\r
+ return bits;\r
+ }\r
+ }\r
+}\r