Issue 521, avoid an NPE
[zxing.git] / csharp / oned / EAN8Writer.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         /// <summary> This object renders an EAN8 code as a ByteMatrix 2D array of greyscale\r
24         /// values.\r
25         /// \r
26         /// </summary>\r
27         /// <author>  aripollak@gmail.com (Ari Pollak)\r
28         /// </author>\r
29         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
30         /// </author>\r
31         public sealed class EAN8Writer:UPCEANWriter\r
32         {\r
33                 \r
34                 private const int codeWidth = 3 + (7 * 4) + 5 + (7 * 4) + 3; // end guard\r
35                 \r
36                 public override ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints)\r
37                 {\r
38                         if (format != BarcodeFormat.EAN_8)\r
39                         {\r
40                                 throw new System.ArgumentException("Can only encode EAN_8, but got " + format);\r
41                         }\r
42                         \r
43                         return base.encode(contents, format, width, height, hints);\r
44                 }\r
45                 \r
46                 /// <returns> a byte array of horizontal pixels (0 = white, 1 = black) \r
47                 /// </returns>\r
48                 public override sbyte[] encode(System.String contents)\r
49                 {\r
50                         if (contents.Length != 8)\r
51                         {\r
52                                 throw new System.ArgumentException("Requested contents should be 8 digits long, but got " + contents.Length);\r
53                         }\r
54                         \r
55                         sbyte[] result = new sbyte[codeWidth];\r
56                         int pos = 0;\r
57                         \r
58                         pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);\r
59                         \r
60                         for (int i = 0; i <= 3; i++)\r
61                         {\r
62                                 int digit = System.Int32.Parse(contents.Substring(i, (i + 1) - (i)));\r
63                                 pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 0);\r
64                         }\r
65                         \r
66                         pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0);\r
67                         \r
68                         for (int i = 4; i <= 7; i++)\r
69                         {\r
70                                 int digit = System.Int32.Parse(contents.Substring(i, (i + 1) - (i)));\r
71                                 pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1);\r
72                         }\r
73                         pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1);\r
74                         \r
75                         return result;\r
76                 }\r
77         }\r
78 }