我有這樣的java類:將int []二進制數組轉換爲java中的圖像。
import java.util.HashMap;
public class Code39 implements LinearBarcode {
private String code;
private int[] bars;
public Code39(String code) {
this.code = code.toUpperCase().trim();
bars = new int[(code.length() + 2) * 12];
this.buildSequence();
}
@Override
public void buildSequence() {
String encoded = "*" + code + "*";
int p = 0;
for (int i = 0; i < encoded.length(); i++) {
int[] sequence = mapSequence(encoded.charAt(i), i);
System.arraycopy(sequence, 0, bars, p, sequence.length);
p += sequence.length;
}
}
@Override
public int[] mapSequence(char c, int pos) {
HashMap<Character, int[]> sequence = new HashMap<Character, int[]>();
sequence.put('0', new int[]{1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1});
sequence.put('1', new int[]{1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1});
sequence.put('2', new int[]{1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1});
sequence.put('3', new int[]{1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1});
sequence.put('4', new int[]{1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1});
sequence.put('5', new int[]{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1});
sequence.put('6', new int[]{1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1});
sequence.put('7', new int[]{1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1});
sequence.put('8', new int[]{1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1});
sequence.put('9', new int[]{1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1});
sequence.put('A', new int[]{1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1});
sequence.put('B', new int[]{1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1});
sequence.put('C', new int[]{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1});
sequence.put('D', new int[]{1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1});
sequence.put('E', new int[]{1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1});
sequence.put('F', new int[]{1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1});
sequence.put('G', new int[]{1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1});
sequence.put('H', new int[]{1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1});
sequence.put('I', new int[]{1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1});
sequence.put('J', new int[]{1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1});
sequence.put('K', new int[]{1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1});
sequence.put('L', new int[]{1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1});
sequence.put('M', new int[]{1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1});
sequence.put('N', new int[]{1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1});
sequence.put('O', new int[]{1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1});
sequence.put('P', new int[]{1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1});
sequence.put('Q', new int[]{1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1});
sequence.put('R', new int[]{1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1});
sequence.put('S', new int[]{1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1});
sequence.put('T', new int[]{1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1});
sequence.put('U', new int[]{1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1});
sequence.put('V', new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1});
sequence.put('W', new int[]{1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1});
sequence.put('X', new int[]{1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1});
sequence.put('Y', new int[]{1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1});
sequence.put('Z', new int[]{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1});
sequence.put('-', new int[]{1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1});
sequence.put('.', new int[]{1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1});
sequence.put(' ', new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1});
sequence.put('$', new int[]{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1});
sequence.put('/', new int[]{1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1});
sequence.put('+', new int[]{1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1});
sequence.put('%', new int[]{1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1});
sequence.put('*', new int[]{1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1});
return sequence.get(c);
}
@Override
public int[] barcode() {
return bars;
}
@Override
public String code() {
return code;
}
@Override
public int calculateCheckDigit() {
return 0;
}
}
它會產生一個Code38維條形碼。它返回int [] bars數組。如何將該數組轉換爲條形碼圖像。我不想使用任何外部的罐子/庫。
如果任何人有任何更好的想法來生成條形碼圖像,也將受到歡迎。
如果我想將它轉換成一個字節[]數組? OR輸入/輸出流 – Kyri33
@ user3446535,與byte []數組一樣 - 只是遍歷int []數組並轉換類型(或更改Code39實現以將條形保存爲byte [])。 至於輸入/輸出流 - 我無法得到,你想要做什麼。 – xSAVIKx