New C# port from Suraj Supekar
[zxing.git] / csharp / datamatrix / DataMatrixReader.cs
diff --git a/csharp/datamatrix/DataMatrixReader.cs b/csharp/datamatrix/DataMatrixReader.cs
new file mode 100755 (executable)
index 0000000..25f2dbb
--- /dev/null
@@ -0,0 +1,171 @@
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using BarcodeFormat = com.google.zxing.BarcodeFormat;\r
+using DecodeHintType = com.google.zxing.DecodeHintType;\r
+using BinaryBitmap = com.google.zxing.BinaryBitmap;\r
+using Reader = com.google.zxing.Reader;\r
+using ReaderException = com.google.zxing.ReaderException;\r
+using Result = com.google.zxing.Result;\r
+using ResultPoint = com.google.zxing.ResultPoint;\r
+using ResultMetadataType = com.google.zxing.ResultMetadataType;\r
+using BitMatrix = com.google.zxing.common.BitMatrix;\r
+using DecoderResult = com.google.zxing.common.DecoderResult;\r
+using DetectorResult = com.google.zxing.common.DetectorResult;\r
+using Decoder = com.google.zxing.datamatrix.decoder.Decoder;\r
+using Detector = com.google.zxing.datamatrix.detector.Detector;\r
+namespace com.google.zxing.datamatrix\r
+{\r
+       \r
+       /// <summary> This implementation can detect and decode Data Matrix codes in an image.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  bbrown@google.com (Brian Brown)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class DataMatrixReader : Reader\r
+       {\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'NO_POINTS '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private static readonly ResultPoint[] NO_POINTS = new ResultPoint[0];\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'decoder '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private Decoder decoder = new Decoder();\r
+               \r
+               /// <summary> Locates and decodes a Data Matrix code in an image.\r
+               /// \r
+               /// </summary>\r
+               /// <returns> a String representing the content encoded by the Data Matrix code\r
+               /// </returns>\r
+               /// <throws>  ReaderException if a Data Matrix code cannot be found, or cannot be decoded </throws>\r
+               public Result decode(BinaryBitmap image)\r
+               {\r
+                       return decode(image, null);\r
+               }\r
+               \r
+               public Result decode(BinaryBitmap image, System.Collections.Hashtable hints)\r
+               {\r
+                       DecoderResult decoderResult;\r
+                       ResultPoint[] points;\r
+                       if (hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE))\r
+                       {\r
+                               BitMatrix bits = extractPureBits(image.BlackMatrix);\r
+                               decoderResult = decoder.decode(bits);\r
+                               points = NO_POINTS;\r
+                       }\r
+                       else\r
+                       {\r
+                               DetectorResult detectorResult = new Detector(image.BlackMatrix).detect();\r
+                               decoderResult = decoder.decode(detectorResult.Bits);\r
+                               points = detectorResult.Points;\r
+                       }\r
+                       Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.DATAMATRIX);\r
+                       if (decoderResult.ByteSegments != null)\r
+                       {\r
+                               result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.ByteSegments);\r
+                       }\r
+                       if (decoderResult.ECLevel != null)\r
+                       {\r
+                               result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.ECLevel.ToString());\r
+                       }\r
+                       return result;\r
+               }\r
+               \r
+               /// <summary> 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
+               /// </summary>\r
+               private static BitMatrix extractPureBits(BitMatrix image)\r
+               {\r
+                       // Now need to determine module size in pixels\r
+                       \r
+                       int height = image.Height;\r
+                       int width = image.Width;\r
+                       int minDimension = System.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.get_Renamed(borderWidth, borderWidth))\r
+                       {\r
+                               borderWidth++;\r
+                       }\r
+                       if (borderWidth == minDimension)\r
+                       {\r
+                               throw ReaderException.Instance;\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.get_Renamed(moduleEnd, borderWidth))\r
+                       {\r
+                               moduleEnd++;\r
+                       }\r
+                       if (moduleEnd == width)\r
+                       {\r
+                               throw ReaderException.Instance;\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.get_Renamed(borderWidth, columnEndOfSymbol))\r
+                       {\r
+                               columnEndOfSymbol--;\r
+                       }\r
+                       if (columnEndOfSymbol < 0)\r
+                       {\r
+                               throw ReaderException.Instance;\r
+                       }\r
+                       columnEndOfSymbol++;\r
+                       \r
+                       // Make sure width of barcode is a multiple of module size\r
+                       if ((columnEndOfSymbol - borderWidth) % moduleSize != 0)\r
+                       {\r
+                               throw ReaderException.Instance;\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
+                       {\r
+                               throw ReaderException.Instance;\r
+                       }\r
+                       \r
+                       // Now just read off the bits\r
+                       BitMatrix bits = new BitMatrix(dimension);\r
+                       for (int i = 0; i < dimension; i++)\r
+                       {\r
+                               int iOffset = borderWidth + i * moduleSize;\r
+                               for (int j = 0; j < dimension; j++)\r
+                               {\r
+                                       if (image.get_Renamed(borderWidth + j * moduleSize, iOffset))\r
+                                       {\r
+                                               bits.set_Renamed(j, i);\r
+                                       }\r
+                               }\r
+                       }\r
+                       return bits;\r
+               }\r
+       }\r
+}
\ No newline at end of file