New C# port from Suraj Supekar
[zxing.git] / csharp / multi / qrcode / QRCodeMultiReader.cs
1 /*\r
2 * Copyright 2009 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 using BarcodeFormat = com.google.zxing.BarcodeFormat;\r
18 using BinaryBitmap = com.google.zxing.BinaryBitmap;\r
19 using ReaderException = com.google.zxing.ReaderException;\r
20 using Result = com.google.zxing.Result;\r
21 using ResultMetadataType = com.google.zxing.ResultMetadataType;\r
22 using ResultPoint = com.google.zxing.ResultPoint;\r
23 using DecoderResult = com.google.zxing.common.DecoderResult;\r
24 using DetectorResult = com.google.zxing.common.DetectorResult;\r
25 using MultipleBarcodeReader = com.google.zxing.multi.MultipleBarcodeReader;\r
26 using MultiDetector = com.google.zxing.multi.qrcode.detector.MultiDetector;\r
27 using QRCodeReader = com.google.zxing.qrcode.QRCodeReader;\r
28 namespace com.google.zxing.multi.qrcode\r
29 {\r
30         \r
31         /// <summary> This implementation can detect and decode multiple QR Codes in an image.\r
32         /// \r
33         /// </summary>\r
34         /// <author>  Sean Owen\r
35         /// </author>\r
36         /// <author>  Hannes Erven\r
37         /// </author>\r
38         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
39         /// </author>\r
40 \r
41         public sealed class QRCodeMultiReader:QRCodeReader, MultipleBarcodeReader\r
42         {\r
43                 \r
44                 //UPGRADE_NOTE: Final was removed from the declaration of 'EMPTY_RESULT_ARRAY '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
45                 private static readonly Result[] EMPTY_RESULT_ARRAY = new Result[0];\r
46                 \r
47                 public Result[] decodeMultiple(BinaryBitmap image)\r
48                 {\r
49                         return decodeMultiple(image, null);\r
50                 }\r
51                 \r
52                 public Result[] decodeMultiple(BinaryBitmap image, System.Collections.Hashtable hints)\r
53                 {\r
54                         System.Collections.ArrayList results = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
55                         DetectorResult[] detectorResult = new MultiDetector(image.BlackMatrix).detectMulti(hints);\r
56                         for (int i = 0; i < detectorResult.Length; i++)\r
57                         {\r
58                                 try\r
59                                 {\r
60                                         DecoderResult decoderResult = Decoder.decode(detectorResult[i].Bits);\r
61                                         ResultPoint[] points = detectorResult[i].Points;\r
62                                         Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE);\r
63                                         if (decoderResult.ByteSegments != null)\r
64                                         {\r
65                                                 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.ByteSegments);\r
66                                         }\r
67                                         if (decoderResult.ECLevel != null)\r
68                                         {\r
69                                                 result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.ECLevel.ToString());\r
70                                         }\r
71                                         results.Add(result);\r
72                                 }\r
73                                 catch (ReaderException re)\r
74                                 {\r
75                                         // ignore and continue \r
76                                 }\r
77                         }\r
78                         if ((results.Count == 0))\r
79                         {\r
80                                 return EMPTY_RESULT_ARRAY;\r
81                         }\r
82                         else\r
83                         {\r
84                                 Result[] resultArray = new Result[results.Count];\r
85                                 for (int i = 0; i < results.Count; i++)\r
86                                 {\r
87                                         resultArray[i] = (Result) results[i];\r
88                                 }\r
89                                 return resultArray;\r
90                         }\r
91                 }\r
92         }\r
93 }