Rest of cosmetic changes -- simpler, black theme with easier-to-touch buttons and...
[zxing.git] / csharp / oned / EAN13Writer.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 WriterException = com.google.zxing.WriterException;\r
19 using ByteMatrix = com.google.zxing.common.ByteMatrix;\r
20 namespace com.google.zxing.oned\r
21 {\r
22         \r
23         \r
24         /// <summary> This object renders an EAN13 code as a ByteMatrix 2D array of greyscale\r
25         /// values.\r
26         /// \r
27         /// </summary>\r
28         /// <author>  aripollak@gmail.com (Ari Pollak)\r
29         /// </author>\r
30         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
31         /// </author>\r
32         public sealed class EAN13Writer:UPCEANWriter\r
33         {\r
34                 \r
35                 private const int codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3; // end guard\r
36                 \r
37                 public override ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints)\r
38                 {\r
39                         if (format != BarcodeFormat.EAN_13)\r
40                         {\r
41                                 throw new System.ArgumentException("Can only encode EAN_13, but got " + format);\r
42                         }\r
43                         \r
44                         return base.encode(contents, format, width, height, hints);\r
45                 }\r
46                 \r
47                 public override sbyte[] encode(System.String contents)\r
48                 {\r
49                         if (contents.Length != 13)\r
50                         {\r
51                                 throw new System.ArgumentException("Requested contents should be 13 digits long, but got " + contents.Length);\r
52                         }\r
53                         \r
54                         int firstDigit = System.Int32.Parse(contents.Substring(0, (1) - (0)));\r
55                         int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];\r
56                         sbyte[] result = new sbyte[codeWidth];\r
57                         int pos = 0;\r
58                         \r
59                         pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);\r
60                         \r
61                         // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded\r
62                         for (int i = 1; i <= 6; i++)\r
63                         {\r
64                                 int digit = System.Int32.Parse(contents.Substring(i, (i + 1) - (i)));\r
65                                 if ((parities >> (6 - i) & 1) == 1)\r
66                                 {\r
67                                         digit += 10;\r
68                                 }\r
69                                 pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], 0);\r
70                         }\r
71                         \r
72                         pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0);\r
73                         \r
74                         for (int i = 7; i <= 12; i++)\r
75                         {\r
76                                 int digit = System.Int32.Parse(contents.Substring(i, (i + 1) - (i)));\r
77                                 pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1);\r
78                         }\r
79                         pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);\r
80                         \r
81                         return result;\r
82                 }\r
83         }\r
84 }