2 * Copyright 2009 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 using BarcodeFormat = com.google.zxing.BarcodeFormat;
\r
18 using Writer = com.google.zxing.Writer;
\r
19 using WriterException = com.google.zxing.WriterException;
\r
20 using ByteMatrix = com.google.zxing.common.ByteMatrix;
\r
21 namespace com.google.zxing.oned
\r
24 /// <summary> <p>Encapsulates functionality and implementation that is common to UPC and EAN families
\r
25 /// of one-dimensional barcodes.</p>
\r
28 /// <author> aripollak@gmail.com (Ari Pollak)
\r
30 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
32 public abstract class UPCEANWriter : Writer
\r
35 public virtual ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height)
\r
37 return encode(contents, format, width, height, null);
\r
40 public virtual ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints)
\r
42 if (contents == null || contents.Length == 0)
\r
44 throw new System.ArgumentException("Found empty contents");
\r
47 if (width < 0 || height < 0)
\r
49 throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
\r
52 sbyte[] code = encode(contents);
\r
53 return renderResult(code, width, height);
\r
56 /// <returns> a byte array of horizontal pixels (0 = white, 1 = black)
\r
58 private static ByteMatrix renderResult(sbyte[] code, int width, int height)
\r
60 int inputWidth = code.Length;
\r
61 // Add quiet zone on both sides
\r
62 int fullWidth = inputWidth + (UPCEANReader.START_END_PATTERN.Length << 1);
\r
63 int outputWidth = System.Math.Max(width, fullWidth);
\r
64 int outputHeight = System.Math.Max(1, height);
\r
66 int multiple = outputWidth / fullWidth;
\r
67 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
\r
69 ByteMatrix output = new ByteMatrix(outputWidth, outputHeight);
\r
70 sbyte[][] outputArray = output.Array;
\r
72 sbyte[] row = new sbyte[outputWidth];
\r
74 // a. Write the white pixels at the left of each row
\r
75 for (int x = 0; x < leftPadding; x++)
\r
77 row[x] = (sbyte) SupportClass.Identity(255);
\r
80 // b. Write the contents of this row of the barcode
\r
81 int offset = leftPadding;
\r
82 for (int x = 0; x < inputWidth; x++)
\r
84 // Redivivus.in Java to c# Porting update
\r
86 // type cased 0 with sbyte
\r
87 sbyte value_Renamed = (code[x] == 1) ? (sbyte)0 : (sbyte)SupportClass.Identity(255);
\r
88 for (int z = 0; z < multiple; z++)
\r
90 row[offset + z] = value_Renamed;
\r
95 // c. Write the white pixels at the right of each row
\r
96 offset = leftPadding + (inputWidth * multiple);
\r
97 for (int x = offset; x < outputWidth; x++)
\r
99 row[x] = (sbyte) SupportClass.Identity(255);
\r
102 // d. Write the completed row multiple times
\r
103 for (int z = 0; z < outputHeight; z++)
\r
105 Array.Copy(row, 0, outputArray[z], 0, outputWidth);
\r
112 /// <summary> Appends the given pattern to the target array starting at pos.
\r
115 /// <param name="startColor">starting color - 0 for white, 1 for black
\r
117 /// <returns> the number of elements added to target.
\r
119 protected internal static int appendPattern(sbyte[] target, int pos, int[] pattern, int startColor)
\r
121 if (startColor != 0 && startColor != 1)
\r
123 throw new System.ArgumentException("startColor must be either 0 or 1, but got: " + startColor);
\r
126 sbyte color = (sbyte) startColor;
\r
128 for (int i = 0; i < pattern.Length; i++)
\r
130 for (int j = 0; j < pattern[i]; j++)
\r
132 target[pos] = color;
\r
136 color ^= 1; // flip color after each segment
\r
141 /// <returns> a byte array of horizontal pixels (0 = white, 1 = black)
\r
143 public abstract sbyte[] encode(System.String contents);
\r