1 58 package org.krysalis.barcode.impl; 59 60 import org.krysalis.barcode.BarGroup; 61 import org.krysalis.barcode.ChecksumMode; 62 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 63 64 69 public class Interleaved2Of5LogicImpl { 70 71 private static final byte[][] CHARSET = {{1, 1, 2, 2, 1}, 72 {2, 1, 1, 1, 2}, 73 {1, 2, 1, 1, 2}, 74 {2, 2, 1, 1, 1}, 75 {1, 1, 2, 1, 2}, 76 {2, 1, 2, 1, 1}, 77 {1, 2, 2, 1, 1}, 78 {1, 1, 1, 2, 2}, 79 {2, 1, 1, 2, 1}, 80 {1, 2, 1, 2, 1}}; 81 82 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 83 84 88 public Interleaved2Of5LogicImpl(ChecksumMode mode) { 89 this.checksumMode = mode; 90 } 91 92 96 public ChecksumMode getChecksumMode() { 97 return this.checksumMode; 98 } 99 100 108 public static char calcChecksum(String msg, int oddMultiplier, int evenMultiplier) { 109 int oddsum = 0; 110 int evensum = 0; 111 for (int i = 0; i < msg.length(); i++) { 112 if (i % 2 == 0) { 113 evensum += Character.digit(msg.charAt(i), 10); 114 } else { 115 oddsum += Character.digit(msg.charAt(i), 10); 116 } 117 } 118 int check = 10 - ((evensum * oddMultiplier + oddsum * evenMultiplier) % 10); 119 if (check >= 10) check = 0; 120 return Character.forDigit(check, 10); 121 } 122 123 131 public static char calcChecksum(String msg) { 132 return calcChecksum(msg, 3, 1); 133 } 134 135 140 public static boolean validateChecksum(String msg) { 141 char actual = msg.charAt(msg.length() - 1); 142 char expected = calcChecksum(msg.substring(0, msg.length() - 1)); 143 return (actual == expected); 144 } 145 146 private int widthAt(char ch, int index) { 147 if (Character.isDigit(ch)) { 148 int digit = Character.digit(ch, 10); 149 int width = CHARSET[digit][index]; 150 return width; 151 } else { 152 throw new IllegalArgumentException ("Invalid character '" + ch 153 + " (" + Character.getNumericValue(ch) 154 + ")'. Expected a digit."); 155 } 156 } 157 158 private void encodeGroup(ClassicBarcodeLogicHandler logic, String group) { 159 if (group.length() != 2) { 160 throw new IllegalArgumentException ("Parameter group must have two characters"); 161 } 162 163 logic.startBarGroup(BarGroup.MSG_CHARACTER, group); 164 for (int index = 0; index < 5; index++) { 165 logic.addBar(true, widthAt(group.charAt(0), index)); 166 logic.addBar(false, widthAt(group.charAt(1), index)); 167 } 168 logic.endBarGroup(); 169 } 170 171 private String handleChecksum(String msg) { 172 if (getChecksumMode() == ChecksumMode.CP_ADD) { 173 return msg + calcChecksum(msg); 174 } else if (getChecksumMode() == ChecksumMode.CP_CHECK) { 175 if (!validateChecksum(msg)) { 176 throw new IllegalArgumentException ("Message '" 177 + msg 178 + "' has a bad checksum. Expected: " 179 + calcChecksum(msg.substring(0, msg.length() - 1))); 180 } 181 return msg; 182 } else if (getChecksumMode() == ChecksumMode.CP_IGNORE) { 183 return msg; 184 } else if (getChecksumMode() == ChecksumMode.CP_AUTO) { 185 return msg; } else { 187 throw new UnsupportedOperationException ( 188 "Unknown checksum mode: " + getChecksumMode()); 189 } 190 } 191 192 197 public void generateBarcodeLogic(ClassicBarcodeLogicHandler logic, String msg) { 198 String s = handleChecksum(msg); 200 201 if ((s.length() % 2) != 0) { 203 s = "0" + s; 204 } 205 206 logic.startBarcode(msg); 207 208 logic.startBarGroup(BarGroup.START_CHARACTER, null); 210 logic.addBar(true, 1); 211 logic.addBar(false, 1); 212 logic.addBar(true, 1); 213 logic.addBar(false, 1); 214 logic.endBarGroup(); 215 216 int i = 0; 218 do { 219 encodeGroup(logic, s.substring(i, i + 2)); 220 i += 2; 221 } while (i < s.length()); 222 223 logic.startBarGroup(BarGroup.STOP_CHARACTER, null); 225 logic.addBar(true, 2); 226 logic.addBar(false, 1); 227 logic.addBar(true, 1); 228 logic.endBarGroup(); 229 230 logic.endBarcode(); 231 } 232 233 } | Popular Tags |