Committed C# port from Mohamad
[zxing.git] / csharp / qrcode / decoder / ErrorCorrectionLevel.cs
1 /*\r
2 * Copyright 2007 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 com.google.zxing;\r
18 using com.google.zxing.common;\r
19 \r
20 namespace com.google.zxing.qrcode.decoder\r
21 {\r
22     public sealed class ErrorCorrectionLevel\r
23     { \r
24           // No, we can't use an enum here. J2ME doesn't support it.\r
25           /**\r
26            * L = ~7% correction\r
27            */\r
28           public static ErrorCorrectionLevel L = new ErrorCorrectionLevel(0, 0x01, "L");\r
29           /**\r
30            * M = ~15% correction\r
31            */\r
32           public static ErrorCorrectionLevel M = new ErrorCorrectionLevel(1, 0x00, "M");\r
33           /**\r
34            * Q = ~25% correction\r
35            */\r
36           public static ErrorCorrectionLevel Q = new ErrorCorrectionLevel(2, 0x03, "Q");\r
37           /**\r
38            * H = ~30% correction\r
39            */\r
40           public static  ErrorCorrectionLevel H = new ErrorCorrectionLevel(3, 0x02, "H");\r
41 \r
42           private static  ErrorCorrectionLevel[] FOR_BITS = {M, L, H, Q};\r
43 \r
44           private int Ordinal;\r
45           private int bits;\r
46           private string name;\r
47 \r
48           private ErrorCorrectionLevel(int ordinal, int bits, string name) {\r
49             this.Ordinal = ordinal;\r
50             this.bits = bits;\r
51             this.name = name;\r
52           }\r
53 \r
54           public int ordinal() {\r
55               return Ordinal;\r
56           }\r
57 \r
58           public int getBits() {\r
59             return bits;\r
60           }\r
61 \r
62           public string getName() {\r
63             return name;\r
64           }\r
65 \r
66           public string toString() {\r
67             return name;\r
68           }\r
69 \r
70           /**\r
71            * @param bits int containing the two bits encoding a QR Code's error correction level\r
72            * @return {@link ErrorCorrectionLevel} representing the encoded error correction level\r
73            */\r
74           public static ErrorCorrectionLevel forBits(int bits) {\r
75             if (bits < 0 || bits >= FOR_BITS.Length) {\r
76               throw new ArgumentException();\r
77             }\r
78             return FOR_BITS[bits];\r
79           }\r
80     }\r
81 }