181 |
ilm |
1 |
/*
|
|
|
2 |
* Copyright 2014 Robin Stuart
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
|
5 |
* in compliance with the License. You may obtain a copy of the License at
|
|
|
6 |
*
|
|
|
7 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
*
|
|
|
9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
11 |
* or implied. See the License for the specific language governing permissions and limitations under
|
|
|
12 |
* the License.
|
|
|
13 |
*/
|
|
|
14 |
package uk.org.okapibarcode.backend;
|
|
|
15 |
|
|
|
16 |
import static uk.org.okapibarcode.util.Arrays.positionOf;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* <p>
|
|
|
20 |
* Implements Codabar barcode symbology according to BS EN 798:1996.
|
|
|
21 |
*
|
|
|
22 |
* <p>
|
|
|
23 |
* Also known as NW-7, Monarch, ABC Codabar, USD-4, Ames Code and Code 27. Codabar can encode any
|
|
|
24 |
* length string starting and ending with the letters A-D and containing between these letters the
|
|
|
25 |
* numbers 0-9, dash (-), dollar ($), colon (:), slash (/), full stop (.) or plus (+). No check
|
|
|
26 |
* digit is generated.
|
|
|
27 |
*
|
|
|
28 |
* @author <a href="mailto:jakel2006@me.com">Robert Elliott</a>
|
|
|
29 |
*/
|
|
|
30 |
public class Codabar extends Symbol {
|
|
|
31 |
|
|
|
32 |
private static final String[] CODABAR_TABLE = { "11111221", "11112211", "11121121", "22111111", "11211211", "21111211", "12111121", "12112111", "12211111", "21121111", "11122111", "11221111",
|
|
|
33 |
"21112121", "21211121", "21212111", "11212121", "11221211", "12121121", "11121221", "11122211" };
|
|
|
34 |
|
|
|
35 |
private static final char[] CHARACTER_SET = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '$', ':', '/', '.', '+', 'A', 'B', 'C', 'D' };
|
|
|
36 |
|
|
|
37 |
/** Ratio of wide bar width to narrow bar width. */
|
|
|
38 |
private double moduleWidthRatio = 2;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Sets the ratio of wide bar width to narrow bar width. Valid values are usually between
|
|
|
42 |
* {@code 2} and {@code 3}. The default value is {@code 2}.
|
|
|
43 |
*
|
|
|
44 |
* @param moduleWidthRatio the ratio of wide bar width to narrow bar width
|
|
|
45 |
*/
|
|
|
46 |
public void setModuleWidthRatio(final double moduleWidthRatio) {
|
|
|
47 |
this.moduleWidthRatio = moduleWidthRatio;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Returns the ratio of wide bar width to narrow bar width.
|
|
|
52 |
*
|
|
|
53 |
* @return the ratio of wide bar width to narrow bar width
|
|
|
54 |
*/
|
|
|
55 |
public double getModuleWidthRatio() {
|
|
|
56 |
return this.moduleWidthRatio;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/** {@inheritDoc} */
|
|
|
60 |
@Override
|
|
|
61 |
protected void encode() {
|
|
|
62 |
|
|
|
63 |
if (!this.content.matches("[A-D]{1}[0-9:/\\$\\.\\+\u002D]+[A-D]{1}")) {
|
|
|
64 |
throw new OkapiException("Invalid characters in input");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
String horizontalSpacing = "";
|
|
|
68 |
|
|
|
69 |
final int l = this.content.length();
|
|
|
70 |
for (int i = 0; i < l; i++) {
|
|
|
71 |
horizontalSpacing += CODABAR_TABLE[positionOf(this.content.charAt(i), CHARACTER_SET)];
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
this.readable = this.content;
|
|
|
75 |
this.pattern = new String[] { horizontalSpacing };
|
|
|
76 |
this.row_count = 1;
|
|
|
77 |
this.row_height = new int[] { -1 };
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/** {@inheritDoc} */
|
|
|
81 |
@Override
|
|
|
82 |
protected double getModuleWidth(final int originalWidth) {
|
|
|
83 |
if (originalWidth == 1) {
|
|
|
84 |
return 1;
|
|
|
85 |
} else {
|
|
|
86 |
return this.moduleWidthRatio;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/** {@inheritDoc} */
|
|
|
91 |
@Override
|
|
|
92 |
protected int[] getCodewords() {
|
|
|
93 |
return getPatternAsCodewords(8);
|
|
|
94 |
}
|
|
|
95 |
}
|