This repository was archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathBarcodeEAN.java
More file actions
149 lines (122 loc) · 4.18 KB
/
BarcodeEAN.java
File metadata and controls
149 lines (122 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* BarCode Coder Library (BCC Library)
* BCCL Version 2.0.1
* Porting : Barcode Java
* HOUREZ Jonathan
* Date : January 8, 2013
*
*
* Author : DEMONTE Jean-Baptiste (firejocker)
* HOUREZ Jonathan
* Contact : jbdemonte @ gmail.com
* Web site: http://barcode-coder.com/
* dual licence : http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
* http://www.gnu.org/licenses/gpl.html
*
* Managed :
*
* standard 2 of 5 (std25)
* interleaved 2 of 5 (int25)
* ean 8 (ean8)
* ean 13 (ean13)
* code 11 (code11)
* code 39 (code39)
* code 93 (code93)
* code 128 (code128)
* codabar (codabar)
* msi (msi)
* datamatrix (datamatrix)
*
*/
package com.barcode_coder.java_barcode;
public class BarcodeEAN extends Barcode1D{
public final static String[] types = new String[]{"ean8","ean13"};
private static String[][] encoding = new String[][]{
{"0001101", "0100111", "1110010"},
{"0011001", "0110011", "1100110"},
{"0010011", "0011011", "1101100"},
{"0111101", "0100001", "1000010"},
{"0100011", "0011101", "1011100"},
{"0110001", "0111001", "1001110"},
{"0101111", "0000101", "1010000"},
{"0111011", "0010001", "1000100"},
{"0110111", "0001001", "1001000"},
{"0001011", "0010111", "1110100"}};
private static String[] first = new String[]{"000000","001011","001101","001110","010011",
"011001","011100","010101","010110","011010"};
private String type;
public BarcodeEAN(String code){
super(code);
this.type = "ean8";
}
public BarcodeEAN(String code, String type){
super(code);
this.type = type;
}
public String getType(){
return this.type;
}
public void setType(String type){
this.type = type;
}
public static String compute(String code, String type){
int len = type.equals("ean13") ? 12 : 7;
if (code.length() < len)
return "";
code = code.substring(0, len);
if (!code.matches("[0-9]{"+len+"}"))
return "";
int sum = 0;
boolean odd = true;
for(int i=len-1; i>-1; i--){
sum += (odd ? 3 : 1) * Integer.parseInt(""+code.charAt(i));
odd = ! odd;
}
return code + (((10 - sum % 10) % 10));
}
public String getDigit(){
String code = new String(this.getCode());
// Check len (12 for ean13, 7 for ean8)
int len = (type.equals("ean8")) ? 7 : 12;
if (code.length() < len || !code.matches("[0-9]*") || !(this.type.equals("ean8") || this.type.equals("ean13")))
this.setResult("");
else
{
StringBuilder result = new StringBuilder("");
// get checksum
code = code.substring(0, len);
code = BarcodeEAN.compute(code, this.type);
// process analyse
result.append("101"); // start
if (type.equals("ean8")){
// process left part
for(int i=0; i<4; i++){
result.append(BarcodeEAN.encoding[Integer.parseInt(""+code.charAt(i))][0]);
}
// center guard bars
result.append("01010");
// process right part
for(int i=4; i<8; i++){
result.append(BarcodeEAN.encoding[Integer.parseInt(""+code.charAt(i))][2]);
}
} else { // ean13
// extract first digit and get sequence
String seg = BarcodeEAN.first[ Integer.parseInt(""+code.charAt(0)) ];
// process left part
for(int i=1; i<7; i++){
result.append(BarcodeEAN.encoding[Integer.parseInt(""+code.charAt(i))][Integer.parseInt(""+seg.charAt(i-1))]);
}
// center guard bars
result.append("01010");
// process right part
for(int i=7; i<13; i++){
result.append(BarcodeEAN.encoding[Integer.parseInt(""+code.charAt(i))][2]);
}
} // ean13
result.append("101"); // stop
this.setResult(result.toString());
this.setComputedCode(code);
}
return this.getResult();
}
}