반응형
이 소스코드는 도서에서만 적용이 되는 것입니다. 물론 다른 바코드의 경우에도 EAN-13을 만족한다면 몇 가지 상수 값만 변환하면 바로 사용이 가능합니다. 여기에서는 따로 구현을 하지 않았습니다. 만약 다른 바코드로 사용을 하신다면 11번 줄과 31번 줄에 있는 데이터 값을 해당하는 값에 맞게 변환을 해주셔야 합니다.
사용방법은 Barcode개체를 생성할 때 인자로 바코드의 바이너리 데이터를 주기만 하면 되며, getData 메서드를 호출하면 그 바이너리 데이터를 변환해서 우리가 알아볼 수 있는 데이터로 확인이 가능합니다.
최적화를 신경쓰지 않고 만든 소스 코드라 약간 난잡해보일 수 있습니다. 역시 변수명을 정하는 것이 가장 어려운 문제인 것 같네요......
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 | class Barcode { private static final int [] CODE_L = { 0xd , 0x19 , 0x13 , 0x3d , 0x23 , 0x31 , 0x2f , 0x3b , 0x37 , 0xb }; private static final int [] CODE_G = { 0x27 , 0x33 , 0x1b , 0x21 , 0x1d , 0x39 , 0x5 , 0x11 , 0x9 , 0x17 }; private static final int [] CODE_R = { 0x72 , 0x66 , 0x6c , 0x42 , 0x5c , 0x4e , 0x50 , 0x44 , 0x48 , 0x74 }; private static final int L_INDEX = 0 ; private static final int G_INDEX = 1 ; private static final int R_INDEX = 2 ; private static final int [] INDEX = { 0 , 1 , 1 , 0 , 1 , 0 , 2 , 2 , 2 , 2 , 2 , 2 }; // Can be changed. private static final int SPLIT_LEN = 7 ; private String binary_str = "" ; public Barcode(String str){ this .binary_str = str; } private int [] convertBinary(){ int len = INDEX.length; int [] result = new int [len]; for ( int i= 0 ; i<len; i++){ result[i] = Integer.parseInt( this .binary_str.substring(SPLIT_LEN*i, SPLIT_LEN*(i+ 1 )), 2 ); } return result; } public String getData(){ byte [] result = new byte [ 13 ]; result[ 0 ] = '9' ; // Can be changed. int [] binary_data = this .convertBinary(); for ( int i= 0 ; i<INDEX.length; i++){ int []code_arr = null ; byte ch = 0 ; switch (INDEX[i]){ case L_INDEX: code_arr = CODE_L; break ; case G_INDEX: code_arr = CODE_G; break ; case R_INDEX: code_arr = CODE_R; break ; } for ( int j= 0 ; j< 10 ; j++){ if (code_arr[j]==binary_data[i]){ ch = ( byte )( '0' +j); break ; } if (ch== 0 ){ System.err.print( "Cannot find!" ); } } result[i+ 1 ] = ch; } return new String(result); } public static void main (String[] args) { Barcode t = new Barcode( "{Barcode binary}" ); System.out.print(t.getData()); } } |
반응형
'컴퓨터 > Java' 카테고리의 다른 글
MongoDB에서 Between 쿼리 사용하기 (0) | 2024.04.23 |
---|---|
[프로그래밍, 자료구조] 큐(Queue) (2) | 2014.08.25 |
2048 ver 1.1 (0) | 2014.06.04 |
Fibonacci number (0) | 2014.06.04 |
2048 ver 1.0 (0) | 2014.06.04 |