diff --git a/java/AppletBarcodeCoder.java b/java/AppletBarcodeCoder.java new file mode 100644 index 0000000..2e68c5f --- /dev/null +++ b/java/AppletBarcodeCoder.java @@ -0,0 +1,173 @@ +/* + * BarCode Coder Library (BCC Library) + * BCCL Version 2.0.1 + * Porting : Barcode Java + * HOUREZ Jonathan + * Date : June 5, 2011 + * + * + * 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) + * + */ + + +/* + * This is an example of Applet + */ + +package com.barcode_coder.java_barcode; + +import java.awt.*; +import java.applet.*; +import java.awt.event.*; +import javax.swing.JPanel; + +public class AppletBarcodeCoder extends Applet implements ActionListener +{ + private static final long serialVersionUID = 1L; + + private Button generateButton; + private TextField codeField; + private CheckboxGroup radioGroup; + private Checkbox radioEAN8, radioEAN13; + private Checkbox radioStandard2of5, radioInterleaved2of5; + private Checkbox radioCode11, radioCode39, radioCode93, radioCode128; + private Checkbox radioCodabar, radioMSI; + private Checkbox radioDatamatrix; + + private String hri = "", digit = ""; + private boolean barcode2D = true; + private int width = 0; + + public void init() + { + setLayout(new BorderLayout()); + + generateButton = new Button("Generate"); + codeField = new TextField("12345670",10); + BorderLayout bl1 = new BorderLayout(); + JPanel jp1 = new JPanel(); + jp1.setLayout(bl1); + jp1.add(new Label("Veuillez saisir le code : "), BorderLayout.WEST); + jp1.add(codeField, BorderLayout.CENTER); + jp1.add(generateButton, BorderLayout.EAST); + add(jp1, BorderLayout.NORTH); + + radioGroup = new CheckboxGroup(); + radioEAN8 = new BarcodeCheckBox("EAN8", radioGroup, true, BarcodeType.EAN8); + radioEAN13 = new BarcodeCheckBox("EAN13", radioGroup, false, BarcodeType.EAN13); + radioStandard2of5 = new BarcodeCheckBox("Standard 2 of 5", radioGroup, false, BarcodeType.Standard2of5); + radioInterleaved2of5 = new BarcodeCheckBox("Interleaved 2 of 5", radioGroup, false, BarcodeType.Interleaved2of5); + radioCode11 = new BarcodeCheckBox("Code 11", radioGroup, false, BarcodeType.Code11); + radioCode39 = new BarcodeCheckBox("Code 39", radioGroup, false, BarcodeType.Code39); + radioCode93 = new BarcodeCheckBox("Code 93", radioGroup, false, BarcodeType.Code93); + radioCode128 = new BarcodeCheckBox("Code 128", radioGroup, false, BarcodeType.Code128); + radioCodabar = new BarcodeCheckBox("Codabar", radioGroup, false, BarcodeType.Codabar); + radioMSI = new BarcodeCheckBox("MSI", radioGroup, false, BarcodeType.MSI); + radioDatamatrix = new BarcodeCheckBox("Datamatrix", radioGroup, false, BarcodeType.Datamatrix); + GridLayout gl2 = new GridLayout(12,1); + JPanel jp2 = new JPanel(); + jp2.setLayout(gl2); + jp2.add(new Label("Type de code-barres :")); + jp2.add(radioEAN8); + jp2.add(radioEAN13); + jp2.add(radioStandard2of5); + jp2.add(radioInterleaved2of5); + jp2.add(radioCode11); + jp2.add(radioCode39); + jp2.add(radioCode93); + jp2.add(radioCode128); + jp2.add(radioCodabar); + jp2.add(radioMSI); + jp2.add(radioDatamatrix); + add(jp2, BorderLayout.SOUTH); + + generateButton.addActionListener(this); + } + + public void paint(Graphics g) + { + System.out.println("Repaint"); + if (!barcode2D){ + for (int i = 0; i barcodeWidth ? stringW : barcodeWidth; + } + + img = img.getSubimage(0, 0, barcodeWidth, barcodeHeight); + + try{ + if ((res.equals("png") || res.equals("jpg") || res.equals("gif")) && file.endsWith(res)) + ImageIO.write(img,res,new File(file)); + else + return false; + + return true; + } + catch (Exception e) { return false; } + + } + return false; + } + + public abstract boolean is2D(); + public abstract int getWidth(); + public abstract String getDigit(); + +} diff --git a/java/Barcode11.java b/java/Barcode11.java new file mode 100644 index 0000000..cca0a1a --- /dev/null +++ b/java/Barcode11.java @@ -0,0 +1,94 @@ +/* + * 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 Barcode11 extends Barcode1D{ + private static String[] encoding = new String[] { + "101011", "1101011", "1001011", "1100101", + "1011011", "1101101", "1001101", "1010011", + "1101001", "110101", "101101" }; + + public Barcode11(String code){ + super(code); + } + + public String getDigit(){ + String code = new String(this.getCode()); + + if (!code.matches("[0-9\\-]*")) + this.setResult(""); + else{ + StringBuilder result = new StringBuilder(""); + char intercharacter = '0'; + + result.append("1011001" + intercharacter); + + int len = code.length(); + for(int i=0; i-1; i--){ + weightC = weightC == 10 ? 1 : weightC + 1; + weightK = weightK == 10 ? 1 : weightK + 1; + + int index = code.charAt(i) == '-' ? 10 : Integer.parseInt(""+code.charAt(i)); + + weightSumC += weightC * index; + weightSumK += weightK * index; + } + + int c = weightSumC % 11; + weightSumK += c; + int k = weightSumK % 11; + + result.append(Barcode11.encoding[c] + intercharacter); + + if (len >= 10){ + result.append(Barcode11.encoding[k] + intercharacter); + } + + result.append("1011001"); + + this.setResult(result.toString()); + this.setComputedCode(code); + } + return this.getResult(); + } +} + + diff --git a/java/Barcode128.java b/java/Barcode128.java new file mode 100644 index 0000000..489c46a --- /dev/null +++ b/java/Barcode128.java @@ -0,0 +1,162 @@ +/* + * 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 Barcode128 extends Barcode1D{ + private static String[] encoding = new String[] { + "11011001100", "11001101100", "11001100110", "10010011000", + "10010001100", "10001001100", "10011001000", "10011000100", + "10001100100", "11001001000", "11001000100", "11000100100", + "10110011100", "10011011100", "10011001110", "10111001100", + "10011101100", "10011100110", "11001110010", "11001011100", + "11001001110", "11011100100", "11001110100", "11101101110", + "11101001100", "11100101100", "11100100110", "11101100100", + "11100110100", "11100110010", "11011011000", "11011000110", + "11000110110", "10100011000", "10001011000", "10001000110", + "10110001000", "10001101000", "10001100010", "11010001000", + "11000101000", "11000100010", "10110111000", "10110001110", + "10001101110", "10111011000", "10111000110", "10001110110", + "11101110110", "11010001110", "11000101110", "11011101000", + "11011100010", "11011101110", "11101011000", "11101000110", + "11100010110", "11101101000", "11101100010", "11100011010", + "11101111010", "11001000010", "11110001010", "10100110000", + "10100001100", "10010110000", "10010000110", "10000101100", + "10000100110", "10110010000", "10110000100", "10011010000", + "10011000010", "10000110100", "10000110010", "11000010010", + "11001010000", "11110111010", "11000010100", "10001111010", + "10100111100", "10010111100", "10010011110", "10111100100", + "10011110100", "10011110010", "11110100100", "11110010100", + "11110010010", "11011011110", "11011110110", "11110110110", + "10101111000", "10100011110", "10001011110", "10111101000", + "10111100010", "11110101000", "11110100010", "10111011110", + "10111101110", "11101011110", "11110101110", "11010000100", + "11010010000", "11010011100", "11000111010"}; + + private boolean crc; + + public Barcode128(String code){ + super(code); + this.crc = false; + } + public Barcode128(String code, boolean crc){ + super(code); + this.crc = crc; + } + public void setCRC(boolean crc){ + this.crc = crc; + } + public boolean getCRC(){ + return this.crc; + } + + public String getDigit(){ + String code = new String(this.getCode().toUpperCase()); + if (!code.matches("[ !\\\"#\\$%&'\\(\\)\\*\\+,\\-\\./0-9:;<=>\\?@A-Z\\[\\\\\\]\\^_`a-z\\{\\|\\}~]*")) + this.setResult(""); + else{ + StringBuilder result = new StringBuilder(""); + String tableB = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + int sum = 0; + int isum = 0; + int value = 0; + + // check each characters + int len = code.length(); + for(int i=0; i 1; + + for(int i=0; i<3 && i 5) || ((i + j - 1 == len) && (j > 3)); + + if (tableCActivated){ + result.append(Barcode128.encoding[99]); // C table + isum ++; + sum += isum * 99; + } + // 2 min for table C so need table B + } else if ((i == len - 1) || (""+code.charAt(i)).matches("[^0-9]") || (""+code.charAt(i+1)).matches("[^0-9]") ) { //todo : verifier le JS : len - 1!!! XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + tableCActivated = false; + result.append(Barcode128.encoding[100]); // B table + isum ++; + sum += isum * 100; + } + + if (tableCActivated) { + value = Integer.parseInt(code.substring(i, i+2)); + i += 2; + } else { + value = tableB.indexOf(code.charAt(i)); + i++; + } + result.append(Barcode128.encoding[value]); + isum ++; + sum += isum * value; + } + + // Add CRC + result.append(Barcode128.encoding[sum % 103]); + + // Stop + result.append(Barcode128.encoding[106]); + + // Termination bar + result.append("11"); + + this.setResult(result.toString()); + this.setComputedCode(code); + } + return this.getResult(); + } +} + diff --git a/java/Barcode1D.java b/java/Barcode1D.java new file mode 100644 index 0000000..05dd19e --- /dev/null +++ b/java/Barcode1D.java @@ -0,0 +1,49 @@ +/* + * 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 abstract class Barcode1D extends Barcode{ + public Barcode1D(String code){ + super(code); + } + public boolean is2D(){ + return false; + } + public int getWidth(){ + String result = this.getResult(); + if (result == null) + return 0; + else + return result.length(); + } + public abstract String getDigit(); +} diff --git a/java/Barcode2of5.java b/java/Barcode2of5.java new file mode 100644 index 0000000..e8b7787 --- /dev/null +++ b/java/Barcode2of5.java @@ -0,0 +1,151 @@ +/* + * 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 Barcode2of5 extends Barcode1D{ + public final static String[] types = new String[]{"int25","std25"}; + private static String[] encoding = new String[] { + "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", + "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" }; + private String type; + private boolean crc; + + public Barcode2of5(String code){ + super(code); + this.type = "int25"; + } + public Barcode2of5(String code, String type){ + super(code); + this.type = type; + this.crc = false; + } + public Barcode2of5(String code, boolean crc){ + super(code); + this.type = "int25"; + this.crc = crc; + } + public Barcode2of5(String code, String type, boolean crc){ + super(code); + this.type = type; + this.crc = crc; + } + + public String getType(){ + return this.type; + } + public void setType(String type){ + this.type = type; + } + public void setCRC(boolean crc){ + this.crc = crc; + } + public boolean getCRC(){ + return this.crc; + } + + + public static String compute(String code, boolean crc, String type){ + if (! crc) { + if ((code.length()) % 2 == 1) code = "0" + code; + } else { + if ( (type.equals("int25")) && (code.length() % 2 == 0) ) + code = "0" + code; + boolean odd = true; + int sum = 0; + for(int i=code.length()-1; i>-1; i--){ + int v = Integer.parseInt(""+code.charAt(i)); + sum += (odd ? 3 * v : v); + odd = ! odd; + } + code += ""+ ((10 - sum % 10) % 10); + } + return code; + } + + public String getDigit(){ + String code = new String(this.getCode()); + code = Barcode2of5.compute(code, crc, type); + if (!code.matches("[0-9]*") || !(this.type.equals("int25") || this.type.equals("std25"))) + this.setResult(""); + else{ + StringBuilder result = new StringBuilder(""); + + if (type.equals("int25")) { // Interleaved 2 of 5 + // start + result.append("1010"); + + // digits + CRC + int end = code.length() / 2; + for(int i=0; i 0) result.append(intercharacter); + result.append(Barcode39.encoding[index]); + } + + this.setResult(result.toString()); + this.setComputedCode(code); + } + return this.getResult(); + } +} + diff --git a/java/Barcode93.java b/java/Barcode93.java new file mode 100644 index 0000000..e0a7f84 --- /dev/null +++ b/java/Barcode93.java @@ -0,0 +1,123 @@ +/* + * 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 Barcode93 extends Barcode1D{ + private static String[] encoding = new String[] { + "100010100", "101001000", "101000100", "101000010", + "100101000", "100100100", "100100010", "101010000", + "100010010", "100001010", "110101000", "110100100", + "110100010", "110010100", "110010010", "110001010", + "101101000", "101100100", "101100010", "100110100", + "100011010", "101011000", "101001100", "101000110", + "100101100", "100010110", "110110100", "110110010", + "110101100", "110100110", "110010110", "110011010", + "101101100", "101100110", "100110110", "100111010", + "100101110", "111010100", "111010010", "111001010", + "101101110", "101110110", "110101110", "100100110", + "111011010", "111010110", "100110010", "101011110"}; + + private boolean crc; + + public Barcode93(String code){ + super(code); + this.crc = false; + } + public Barcode93(String code, boolean crc){ + super(code); + this.crc = crc; + } + public void setCRC(boolean crc){ + this.crc = crc; + } + public boolean getCRC(){ + return this.crc; + } + + public String getDigit(){ + String code = new String(this.getCode().toUpperCase()); + if (!code.matches("[0-9A-Z\\-\\. \\$/\\+%]*")) + this.setResult(""); + else{ + StringBuilder result = new StringBuilder(""); + String table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%____*"; // _ => ($), (%), (/) et (+) + + // start : * + result.append(Barcode93.encoding[47]); + + int len = code.length(); + for(int i=0; i-1; i--){ + weightC = weightC == 20 ? 1 : weightC + 1; + weightK = weightK == 15 ? 1 : weightK + 1; + + int index = table.indexOf(code.charAt(i)); + + weightSumC += weightC * index; + weightSumK += weightK * index; + } + + int c = weightSumC % 47; + weightSumK += c; + int k = weightSumK % 47; + + result.append(Barcode93.encoding[c]); + result.append(Barcode93.encoding[k]); + } + + // stop : * + result.append(Barcode93.encoding[47]); + + // Terminaison bar + result.append('1'); + + this.setResult(result.toString()); + this.setComputedCode(code); + } + return this.getResult(); + } +} \ No newline at end of file diff --git a/java/BarcodeCodabar.java b/java/BarcodeCodabar.java new file mode 100644 index 0000000..4c4eee1 --- /dev/null +++ b/java/BarcodeCodabar.java @@ -0,0 +1,78 @@ +/* + * BarCode Coder Library (BCC Library) + * BCCL Version 2.0.1 + * Porting : Barcode Java + * HOUREZ Jonathan + * Date : June 5, 2011 + * + * + * 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 BarcodeCodabar extends Barcode1D{ + private static String[] encoding = new String[] { + "101010011", "101011001", "101001011", "110010101", + "101101001", "110101001", "100101011", "100101101", + "100110101", "110100101", "101001101", "101100101", + "1101011011", "1101101011", "1101101101", "1011011011", + "1011001001", "1010010011", "1001001011", "1010011001"}; + + public BarcodeCodabar(String code){ + super(code); + } + + public String getDigit(){ + String code = new String(this.getCode()); + + if (!code.matches("[0-9\\-\\$:/\\.\\+]*")) + this.setResult(""); + else{ + StringBuilder result = new StringBuilder(""); + String table = "0123456789-$:/.+"; + char intercharacter = '0'; + + // add start : A->D : arbitrary choose A + result.append(BarcodeCodabar.encoding[16] + intercharacter); + + int len = code.length(); + for(int i=0; iD : arbitrary choose A + result.append(BarcodeCodabar.encoding[16]); + + this.setResult(result.toString()); + this.setComputedCode(code); + } + return this.getResult(); + } +} + diff --git a/java/BarcodeDatamatrix.java b/java/BarcodeDatamatrix.java new file mode 100644 index 0000000..0be87a0 --- /dev/null +++ b/java/BarcodeDatamatrix.java @@ -0,0 +1,558 @@ +/* + * 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 BarcodeDatamatrix extends Barcode { + + private static int lengthRows[] = {10, 12, 14, 16, 18, 20, 22, 24, 26, + 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144, + 8, 8, 12, 12, 16, 16 }; + + private static int lengthCols[] = {10, 12, 14, 16, 18, 20, 22, 24, 26, + 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144, + 18, 32, 26, 36, 36, 48 }; + + private static int dataCWCount[] = {3, 5, 8, 12, 18, 22, 30, 36, + 44, 62, 86, 114, 144, 174, 204, 280, 368, 456, 576, 696, 816, 1050, + 1304, 1558, 5, 10, 16, 22, 32, 49 }; + + private static int solomonCWCount[] = {5, 7, 10, 12, 14, 18, 20, 24, 28, + 36, 42, 48, 56, 68, 84, 112, 144, 192, 224, 272, 336, 408, 496, 620, + 7, 11, 14, 18, 24, 28 }; + + private static int dataRegionRows[] = { 8, 10, 12, 14, 16, 18, 20, 22, + 24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22, + 6, 6, 10, 10, 14, 14 }; + + private static int dataRegionCols[] = { 8, 10, 12, 14, 16, 18, 20, 22, + 24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22, + 16, 14, 24, 16, 16, 22 }; + + private static int regionRows[] = { 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6, + 1, 1, 1, 1, 1, 1 }; + + private static int regionCols[] = { 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6, + 1, 2, 1, 2, 2, 2 }; + + private static int interleavedBlocks[] = { 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 4, 6, 6, 8, 8, + 1, 1, 1, 1, 1, 1 }; + + private int[] logTab = new int[]{-255, 255, 1, 240, 2, 225, 241, 53, 3, + 38, 226, 133, 242, 43, 54, 210, 4, 195, 39, 114, 227, 106, 134, 28, + 243, 140, 44, 23, 55, 118, 211, 234, 5, 219, 196, 96, 40, 222, 115, + 103, 228, 78, 107, 125, 135, 8, 29, 162, 244, 186, 141, 180, 45, 99, + 24, 49, 56, 13, 119, 153, 212, 199, 235, 91, 6, 76, 220, 217, 197, + 11, 97, 184, 41, 36, 223, 253, 116, 138, 104, 193, 229, 86, 79, 171, + 108, 165, 126, 145, 136, 34, 9, 74, 30, 32, 163, 84, 245, 173, 187, + 204, 142, 81, 181, 190, 46, 88, 100, 159, 25, 231, 50, 207, 57, 147, + 14, 67, 120, 128, 154, 248, 213, 167, 200, 63, 236, 110, 92, 176, 7, + 161, 77, 124, 221, 102, 218, 95, 198, 90, 12, 152, 98, 48, 185, 179, + 42, 209, 37, 132, 224, 52, 254, 239, 117, 233, 139, 22, 105, 27, 194, + 113, 230, 206, 87, 158, 80, 189, 172, 203, 109, 175, 166, 62, 127, + 247, 146, 66, 137, 192, 35, 252, 10, 183, 75, 216, 31, 83, 33, 73, + 164, 144, 85, 170, 246, 65, 174, 61, 188, 202, 205, 157, 143, 169, 82, + 72, 182, 215, 191, 251, 47, 178, 89, 151, 101, 94, 160, 123, 26, 112, + 232, 21, 51, 238, 208, 131, 58, 69, 148, 18, 15, 16, 68, 17, 121, 149, + 129, 19, 155, 59, 249, 70, 214, 250, 168, 71, 201, 156, 64, 60, 237, + 130, 111, 20, 93, 122, 177, 150}; + + private int[] aLogTab = new int[]{1, 2, 4, 8, 16, 32, 64, 128, 45, 90, + 180, 69, 138, 57, 114, 228, 229, 231, 227, 235, 251, 219, 155, 27, 54, + 108, 216, 157, 23, 46, 92, 184, 93, 186, 89, 178, 73, 146, 9, 18, 36, + 72, 144, 13, 26, 52, 104, 208, 141, 55, 110, 220, 149, 7, 14, 28, 56, + 112, 224, 237, 247, 195, 171, 123, 246, 193, 175, 115, 230, 225, 239, + 243, 203, 187, 91, 182, 65, 130, 41, 82, 164, 101, 202, 185, 95, 190, + 81, 162, 105, 210, 137, 63, 126, 252, 213, 135, 35, 70, 140, 53, 106, + 212, 133, 39, 78, 156, 21, 42, 84, 168, 125, 250, 217, 159, 19, 38, 76, + 152, 29, 58, 116, 232, 253, 215, 131, 43, 86, 172, 117, 234, 249, 223, + 147, 11, 22, 44, 88, 176, 77, 154, 25, 50, 100, 200, 189, 87, 174, 113, + 226, 233, 255, 211, 139, 59, 118, 236, 245, 199, 163, 107, 214, 129, + 47, 94, 188, 85, 170, 121, 242, 201, 191, 83, 166, 97, 194, 169, 127, + 254, 209, 143, 51, 102, 204, 181, 71, 142, 49, 98, 196, 165, 103, 206, + 177, 79, 158, 17, 34, 68, 136, 61, 122, 244, 197, 167, 99, 198, 161, + 111, 222, 145, 15, 30, 60, 120, 240, 205, 183, 67, 134, 33, 66, 132, + 37, 74, 148, 5, 10, 20, 40, 80, 160, 109, 218, 153, 31, 62, 124, 248, + 221, 151, 3, 6, 12, 24, 48, 96, 192, 173, 119, 238, 241, 207, 179, 75, + 150, 1}; + + private boolean square; + private int width; + + public BarcodeDatamatrix(String code, boolean square){ + super(code); + this.square = square; + } + + public BarcodeDatamatrix(String code){ + super(code); + this.square = true; + } + + public boolean is2D(){ + return true; + } + public int getWidth(){ + return this.width; + } + + public String getDigit(){ + String code = this.getCode(); + byte[][] matrix = this.getDigit(code); + StringBuilder result = new StringBuilder(""); + if (matrix != null){ + for (int i=0; i= 0) && (assigned[row][col]!=1)){ + PatternShapeStandard(datamatrix, assigned, codeWordsBits[chr], row, col, totalRows, totalCols); + chr++; + } + row -= 2; + col += 2; + } while ((row >= 0) && (col < totalCols)); + + row += 1; + col += 3; + + // Go down and left in the datamatrix + do { + if((row >= 0) && (col < totalCols) && (assigned[row][col]!=1)){ + PatternShapeStandard(datamatrix, assigned, codeWordsBits[chr], row, col, totalRows, totalCols); + chr++; + } + + row += 2; + col -= 2; + + } while ((row < totalRows) && (col >=0)); + + row += 3; + col += 1; + + } while ((row < totalRows) || (col < totalCols)); + } + + private void PatternShapeStandard(byte[][] datamatrix, + byte[][] assigned, + byte[] bits, + short row, + short col, + short totalRows, + short totalCols){ + placeBitInDatamatrix(datamatrix, assigned, bits[0], (byte) 1, (short)(row-2), (short)(col-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[1], (byte) 2, (short)(row-2), (short)(col-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[2], (byte) 3, (short)(row-1), (short)(col-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[3], (byte) 4, (short)(row-1), (short)(col-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[4], (byte) 5, (short)(row-1), (short)(col), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[5], (byte) 6, (short) row, (short)(col-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[6], (byte) 7, (short) row, (short)(col-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[7], (byte) 8, (short) row, (short) col, totalRows, totalCols); +} + + private void PatternShapeSpecial1(byte[][] datamatrix, + byte[][] assigned, + byte[] bits, + short totalRows, + short totalCols){ + placeBitInDatamatrix(datamatrix, assigned, bits[0], (byte) 1, (short)(totalRows-1), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[1], (byte) 2, (short)(totalRows-1), (short) 1, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[2], (byte) 3, (short)(totalRows-1), (short) 2, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[3], (byte) 4, (short) 0, (short)(totalCols-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[4], (byte) 5, (short) 0, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[5], (byte) 6, (short) 1, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[6], (byte) 7, (short) 2, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[7], (byte) 8, (short) 3, (short)(totalCols-1), totalRows, totalCols); + } + + private void PatternShapeSpecial2(byte[][] datamatrix, + byte[][] assigned, + byte[] bits, + short totalRows, + short totalCols){ + placeBitInDatamatrix(datamatrix, assigned, bits[0], (byte) 1, (short)(totalRows-3), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[1], (byte) 2, (short)(totalRows-2), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[2], (byte) 3, (short)(totalRows-1), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[3], (byte) 4, (short) 0, (short)(totalCols-4), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[4], (byte) 5, (short) 0, (short)(totalCols-3), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[5], (byte) 6, (short) 0, (short)(totalCols-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[6], (byte) 7, (short) 0, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[7], (byte) 8, (short) 1, (short)(totalCols-1), totalRows, totalCols); + } + + private void PatternShapeSpecial3(byte[][] datamatrix, + byte[][] assigned, + byte[] bits, + short totalRows, + short totalCols){ + placeBitInDatamatrix(datamatrix, assigned, bits[0], (byte) 1, (short)(totalRows-3), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[1], (byte) 2, (short)(totalRows-2), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[2], (byte) 3, (short)(totalRows-1), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[3], (byte) 4, (short) 0, (short)(totalCols-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[4], (byte) 5, (short) 0, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[5], (byte) 6, (short) 1, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[6], (byte) 7, (short) 2, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[7], (byte) 8, (short) 3, (short)(totalCols-1), totalRows, totalCols); + } + + private void PatternShapeSpecial4(byte[][] datamatrix, + byte[][] assigned, + byte[] bits, + short totalRows, + short totalCols){ + placeBitInDatamatrix(datamatrix, assigned, bits[0], (byte) 1, (short)(totalRows-1), (short) 0, totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[1], (byte) 2, (short)(totalRows-1), (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[2], (byte) 3, (short) 0, (short)(totalCols-3), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[3], (byte) 4, (short) 0, (short)(totalCols-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[4], (byte) 5, (short) 0, (short)(totalCols-1), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[5], (byte) 6, (short) 1, (short)(totalCols-3), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[6], (byte) 7, (short) 1, (short)(totalCols-2), totalRows, totalCols); + placeBitInDatamatrix(datamatrix, assigned, bits[7], (byte) 8, (short) 1, (short)(totalCols-1), totalRows, totalCols); + } + + private void placeBitInDatamatrix(byte[][] datamatrix, + byte[][] assigned, + byte bit, + byte position, + short row, + short col, + short totalRows, + short totalCols){ + + if (row < 0) { + row += totalRows; + col += 4 - ((totalRows+4)%8); + } + if (col < 0) { + col += totalCols; + row += 4 - ((totalCols+4)%8); + } + + if (assigned[row][col] != 1) { + datamatrix[row][col] = bit; + assigned[row][col] = 1; + } + } + + + private int[] calculSolFactorTable(int solomonCWCount){ + int[] g = new int[solomonCWCount+1]; + + for (int i=0; i<=solomonCWCount; i++){ + g[i] = 1; + } + + for(int i = 1; i <= solomonCWCount; i++) { + for(int j = i - 1; j >= 0; j--) { + g[j] = champGaloisDoub(g[j], i); + if(j > 0){ + g[j] = champGaloisSum(g[j], g[j-1]); + } + } + } + + return g; + } + + + private void addReedSolomonCW(int nSolomonCW, + int[] coeffTab, + int nDataCW, + int[] dataTab, + int blocks){ + + int temp = 0; + int errorBlocks = nSolomonCW / blocks; + int[] correctionCW = new int[errorBlocks]; + + for(int k = 0; k < blocks; k++) { + for (int i=0; i=0; j--){ + + if (temp == 0) correctionCW[j] = 0; + else{ + correctionCW[j] = champGaloisMult(temp, coeffTab[j]); + } + + if (j>0) { + correctionCW[j] = champGaloisSum(correctionCW[j-1], correctionCW[j]); + } + } + } + + int j = nDataCW + k; + for (int i=errorBlocks-1; i>=0; i--){ + dataTab[j] = correctionCW[i]; + j=j+blocks; + } + } + } + + + private int champGaloisMult(int a, int b){ + if(a == 0 || b == 0) + return 0; + else + return aLogTab[(logTab[a] + logTab[b]) % 255]; + } + + + private int champGaloisDoub(int a, int b) + { + if (a == 0) + return 0; + else if (b == 0) + return a; + else + return aLogTab[(logTab[a] + b) % 255]; + } + + + private int champGaloisSum(int a, int b) + { + return a ^ b; + } + + + private void addPadCW(int[] tab, int from, int to){ + if (from >= to) return; + tab[from] = 129; + + int r; + for (int i=from+1; i1558) && square) + return -1; + if ((dataCodeWordsCount<1 || dataCodeWordsCount>49) && !square) + return -1; + int n=0; + if (!square) n = 24; + while (dataCWCount[n] < dataCodeWordsCount){ + n++; + } + return n; + } + + + private int[] encodeDataCodeWordsASCII(String text) { + int textLength = text.length(); + int[] dataCodeWords = new int[2000]; // 2000 ? + int n = 0; + + for (int i=0; i= 128) { + dataCodeWords[n] = 235; + c = c - 127; + n++; + } + else if ((c>=48 && c<=57) && (i+1=48 && text.charAt(i+1)<=57)) { + c = ((c - 48) * 10) + (((int) text.charAt(i+1))-48); + c += 130; + i++; + } + else c++; + + dataCodeWords[n] = c; + n++; + } + + int[] toReturn = new int[n]; + System.arraycopy(dataCodeWords, 0, toReturn, 0, n); + + return toReturn; + } + + private byte[][] addFinderPattern(byte[][] datamatrix, int rowsRegion, int colsRegion, int rowsRegionCW, int colsRegionCW){ // Add the finder pattern + int totalRowsCW = (rowsRegionCW+2) * rowsRegion; + int totalColsCW = (colsRegionCW+2) * colsRegion; + + byte[][] datamatrixTemp = new byte[totalRowsCW+2][totalColsCW+2]; + + for (int i=0; i ? + } + } + } + for (int j=0; j-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(); + } + +} + + + + diff --git a/java/BarcodeFactory.java b/java/BarcodeFactory.java new file mode 100644 index 0000000..215877d --- /dev/null +++ b/java/BarcodeFactory.java @@ -0,0 +1,83 @@ +/* + * 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 BarcodeFactory { + + private BarcodeFactory(){} + + public static Barcode createBarcode(BarcodeType barcodeType, String code) { + return createBarcode(barcodeType, code, true); + } + + public static Barcode createBarcode(BarcodeType barcodeType, String code, boolean crc) { + Barcode barcode = null; + + switch (barcodeType) { + case EAN8: + barcode = new BarcodeEAN(code, "ean8"); + break; + case EAN13: + barcode = new BarcodeEAN(code, "ean13"); + break; + case Standard2of5: + barcode = new Barcode2of5(code, "std25", crc); + break; + case Interleaved2of5: + barcode = new Barcode2of5(code, "int25", crc); + break; + case Code11: + barcode = new Barcode11(code); + break; + case Code39: + barcode = new Barcode39(code); + break; + case Code93: + barcode = new Barcode93(code, crc); + break; + case Code128: + barcode = new Barcode128(code, crc); + break; + case Codabar: + barcode = new BarcodeCodabar(code); + break; + case MSI: + barcode = new BarcodeMSI(code, crc); + break; + case Datamatrix: + barcode = new BarcodeDatamatrix(code); + break; + } + + return barcode; + } +} diff --git a/java/BarcodeMSI.java b/java/BarcodeMSI.java new file mode 100644 index 0000000..754b974 --- /dev/null +++ b/java/BarcodeMSI.java @@ -0,0 +1,147 @@ +/* + * 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 BarcodeMSI extends Barcode1D { + private static String[] encoding = new String[] { + "100100100100", "100100100110", "100100110100", "100100110110", + "100110100100", "100110100110", "100110110100", "100110110110", + "110100100100", "110100100110"}; + private boolean crc; + + public BarcodeMSI(String code){ + super(code); + this.crc = true; + } + public BarcodeMSI(String code, boolean crc){ + super(code); + this.crc = crc; + } + public void setCRC(boolean crc){ + this.crc = crc; + } + public boolean getCRC(){ + return this.crc; + } + + private static String compute(String code, boolean crc){ + if (crc){ + code = BarcodeMSI.computeMod10(code); + } + return code; + } + + @SuppressWarnings("unused") + private static String compute(String code, String[] crc){ + if (crc[0] == "mod10"){ + code = BarcodeMSI.computeMod10(code); + } else if (crc[0] == "mod11"){ + code = BarcodeMSI.computeMod11(code); + } + if (crc[1] == "mod10"){ + code = BarcodeMSI.computeMod10(code); + } else if (crc[1] == "mod11"){ + code = BarcodeMSI.computeMod11(code); + } + return code; + } + + private static String computeMod10(String code){ + int len = code.length(); + int toPart1 = len % 2; + int n1 = 0; + int sum = 0; + for(int i=0; i-1; i--){ + sum += weight * Integer.parseInt(""+code.charAt(i)); + weight = weight == 7 ? 2 : weight + 1; + } + return code + ( ""+ ((11 - sum % 11) % 11)); + } + + public String getDigit(){ + return this.getDigit(this.crc); + } + + public String getDigit(boolean crc){ + String code = new String(this.getCode()); + if (!code.matches("[0-9]*")) + this.setResult(""); + else + { + StringBuilder result = new StringBuilder(""); + + code = BarcodeMSI.compute(code, crc); + + // start + result.append("110"); + // digits + int len = code.length(); + for(int i=0; i