# BarcodeEncodings.java Sample

This topic inludes the source code for the BarcodeEncodings.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/TaglibExtDevKit/TldxHandlers/TDK/

## Sample Source Code

`001 package TDK; 002 003 import java.util.ArrayList; 004 005 public class BarcodeEncodings 006 { 007 008 static class Slice implements java.io.Serializable 009 { 010 public int relStartX; 011 public int relStartY; 012 public int width; 013 public int height; 014 public String color; 015 016 public Slice() { } 017 public Slice(int startX, int startY, int w, int h, String c) 018 { 019 relStartX = startX; 020 relStartY = startY; 021 width = w; 022 height = h; 023 color = c; 024 } 025 } 026 027 static class tokenEncoding implements java.io.Serializable 028 { 029 public static final char EMPTY_CHAR = '#'; 030 031 public char token; 032 public ArrayList slices; 033 034 public tokenEncoding(char c) 035 { 036 token = c; 037 slices = new ArrayList(); 038 } 039 040 public void add(Slice s) 041 { 042 slices.add(s); 043 } 044 } 045 046 // DEPRECATED. doesn't seem to be marshalled quite right. 047 public static class BarcodeException extends Throwable 048 { 049 050 String _message; 051 public BarcodeException(String m) 052 { 053 _message = m; 054 } 055 public String getError() 056 { 057 return _message; 058 } 059 } 060 061 public interface Encoding extends java.io.Serializable 062 { 063 public static final String BLACK = "#000000"; 064 public static final String WHITE = "#FFFFFF"; 065 066 public abstract ArrayList encode(); 067 public boolean verifyInput(String src) throws Exception; 068 } 069 070 public static class code39Encoding implements Encoding 071 { 072 public String src; 073 public ArrayList encoding; 074 075 public static final int WIDE = 0; 076 public static final int NORMAL = 1; 077 public static final int NORMAL_BAR_WIDTH = 1; 078 public static final int WIDE_BAR_WIDTH = 3; 079 public static final int DEFAULT_HEIGHT = 60; 080 081 public code39Encoding(String s) 082 { 083 src = s; 084 encoding = new ArrayList(); 085 } 086 087 public boolean verifyInput(String src) throws Exception 088 { 089 if(src == null) 090 { 091 throw new Exception("generateCode39(): expected non-empty input."); 092 } 093 return true; 094 } 095 096 public ArrayList encode() 097 { 098 src = src.toUpperCase(); 099 for(int i = 0; i < src.length(); i++) 100 { 101 char c = src.charAt(i); 102 tokenEncoding slices = new tokenEncoding(c); 103 104 switch(c) 105 { 106 case 'A': 107 108 slices.add(processBlackSlice(WIDE)); 109 slices.add(processBlackSlice(WIDE)); 110 slices.add(processWhiteSlice(NORMAL)); 111 slices.add(processBlackSlice(NORMAL)); 112 slices.add(processWhiteSlice(NORMAL)); 113 slices.add(processBlackSlice(NORMAL)); 114 slices.add(processWhiteSlice(WIDE)); 115 slices.add(processBlackSlice(NORMAL)); 116 slices.add(processWhiteSlice(NORMAL)); 117 slices.add(processBlackSlice(WIDE)); 118 break; 119 120 case 'B': 121 122 slices.add(processBlackSlice(NORMAL)); 123 slices.add(processWhiteSlice(NORMAL)); 124 slices.add(processBlackSlice(WIDE)); 125 slices.add(processWhiteSlice(NORMAL)); 126 slices.add(processBlackSlice(NORMAL)); 127 slices.add(processWhiteSlice(WIDE)); 128 slices.add(processBlackSlice(NORMAL)); 129 slices.add(processWhiteSlice(NORMAL)); 130 slices.add(processBlackSlice(WIDE)); 131 break; 132 133 case 'C': 134 135 slices.add(processBlackSlice(WIDE)); 136 slices.add(processWhiteSlice(NORMAL)); 137 slices.add(processBlackSlice(WIDE)); 138 slices.add(processWhiteSlice(NORMAL)); 139 slices.add(processBlackSlice(NORMAL)); 140 slices.add(processWhiteSlice(WIDE)); 141 slices.add(processBlackSlice(NORMAL)); 142 slices.add(processWhiteSlice(NORMAL)); 143 slices.add(processBlackSlice(NORMAL)); 144 break; 145 146 147 case 'D': 148 149 slices.add(processBlackSlice(NORMAL)); 150 slices.add(processWhiteSlice(NORMAL)); 151 slices.add(processBlackSlice(NORMAL)); 152 slices.add(processWhiteSlice(NORMAL)); 153 slices.add(processBlackSlice(WIDE)); 154 slices.add(processWhiteSlice(WIDE)); 155 slices.add(processBlackSlice(NORMAL)); 156 slices.add(processWhiteSlice(NORMAL)); 157 slices.add(processBlackSlice(WIDE)); 158 break; 159 160 161 case 'E': 162 163 slices.add(processBlackSlice(WIDE)); 164 slices.add(processWhiteSlice(NORMAL)); 165 slices.add(processBlackSlice(NORMAL)); 166 slices.add(processWhiteSlice(NORMAL)); 167 slices.add(processBlackSlice(WIDE)); 168 slices.add(processWhiteSlice(WIDE)); 169 slices.add(processBlackSlice(NORMAL)); 170 slices.add(processWhiteSlice(NORMAL)); 171 slices.add(processBlackSlice(NORMAL)); 172 break; 173 174 case 'F': 175 176 slices.add(processBlackSlice(NORMAL)); 177 slices.add(processWhiteSlice(NORMAL)); 178 slices.add(processBlackSlice(WIDE)); 179 slices.add(processWhiteSlice(NORMAL)); 180 slices.add(processBlackSlice(WIDE)); 181 slices.add(processWhiteSlice(WIDE)); 182 slices.add(processBlackSlice(NORMAL)); 183 slices.add(processWhiteSlice(NORMAL)); 184 slices.add(processBlackSlice(NORMAL)); 185 break; 186 187 case 'G': 188 189 slices.add(processBlackSlice(NORMAL)); 190 slices.add(processWhiteSlice(NORMAL)); 191 slices.add(processBlackSlice(NORMAL)); 192 slices.add(processWhiteSlice(NORMAL)); 193 slices.add(processBlackSlice(NORMAL)); 194 slices.add(processWhiteSlice(WIDE)); 195 slices.add(processBlackSlice(WIDE)); 196 slices.add(processWhiteSlice(NORMAL)); 197 slices.add(processBlackSlice(WIDE)); 198 break; 199 200 case 'H': 201 202 slices.add(processBlackSlice(WIDE)); 203 slices.add(processWhiteSlice(NORMAL)); 204 slices.add(processBlackSlice(NORMAL)); 205 slices.add(processWhiteSlice(NORMAL)); 206 slices.add(processBlackSlice(NORMAL)); 207 slices.add(processWhiteSlice(WIDE)); 208 slices.add(processBlackSlice(WIDE)); 209 slices.add(processWhiteSlice(NORMAL)); 210 slices.add(processBlackSlice(NORMAL)); 211 break; 212 213 case 'I': 214 215 slices.add(processBlackSlice(NORMAL)); 216 slices.add(processWhiteSlice(NORMAL)); 217 slices.add(processBlackSlice(WIDE)); 218 slices.add(processWhiteSlice(NORMAL)); 219 slices.add(processBlackSlice(NORMAL)); 220 slices.add(processWhiteSlice(WIDE)); 221 slices.add(processBlackSlice(WIDE)); 222 slices.add(processWhiteSlice(NORMAL)); 223 slices.add(processBlackSlice(NORMAL)); 224 break; 225 226 case 'J': 227 228 slices.add(processBlackSlice(NORMAL)); 229 slices.add(processWhiteSlice(NORMAL)); 230 slices.add(processBlackSlice(NORMAL)); 231 slices.add(processWhiteSlice(NORMAL)); 232 slices.add(processBlackSlice(WIDE)); 233 slices.add(processWhiteSlice(WIDE)); 234 slices.add(processBlackSlice(WIDE)); 235 slices.add(processWhiteSlice(NORMAL)); 236 slices.add(processBlackSlice(NORMAL)); 237 break; 238 239 case 'K': 240 241 slices.add(processBlackSlice(WIDE)); 242 slices.add(processWhiteSlice(NORMAL)); 243 slices.add(processBlackSlice(NORMAL)); 244 slices.add(processWhiteSlice(NORMAL)); 245 slices.add(processBlackSlice(NORMAL)); 246 slices.add(processWhiteSlice(NORMAL)); 247 slices.add(processBlackSlice(NORMAL)); 248 slices.add(processWhiteSlice(WIDE)); 249 slices.add(processBlackSlice(WIDE)); 250 break; 251 252 case 'L': 253 254 slices.add(processBlackSlice(NORMAL)); 255 slices.add(processWhiteSlice(NORMAL)); 256 slices.add(processBlackSlice(WIDE)); 257 slices.add(processWhiteSlice(NORMAL)); 258 slices.add(processBlackSlice(NORMAL)); 259 slices.add(processWhiteSlice(NORMAL)); 260 slices.add(processBlackSlice(NORMAL)); 261 slices.add(processWhiteSlice(WIDE)); 262 slices.add(processBlackSlice(WIDE)); 263 break; 264 265 case 'M': 266 267 slices.add(processBlackSlice(WIDE)); 268 slices.add(processWhiteSlice(NORMAL)); 269 slices.add(processBlackSlice(WIDE)); 270 slices.add(processWhiteSlice(NORMAL)); 271 slices.add(processBlackSlice(NORMAL)); 272 slices.add(processWhiteSlice(NORMAL)); 273 slices.add(processBlackSlice(NORMAL)); 274 slices.add(processWhiteSlice(WIDE)); 275 slices.add(processBlackSlice(NORMAL)); 276 break; 277 278 case 'N': 279 280 slices.add(processBlackSlice(NORMAL)); 281 slices.add(processWhiteSlice(NORMAL)); 282 slices.add(processBlackSlice(NORMAL)); 283 slices.add(processWhiteSlice(NORMAL)); 284 slices.add(processBlackSlice(WIDE)); 285 slices.add(processWhiteSlice(NORMAL)); 286 slices.add(processBlackSlice(NORMAL)); 287 slices.add(processWhiteSlice(WIDE)); 288 slices.add(processBlackSlice(WIDE)); 289 break; 290 291 case 'O': 292 293 slices.add(processBlackSlice(WIDE)); 294 slices.add(processWhiteSlice(NORMAL)); 295 slices.add(processBlackSlice(NORMAL)); 296 slices.add(processWhiteSlice(NORMAL)); 297 slices.add(processBlackSlice(WIDE)); 298 slices.add(processWhiteSlice(NORMAL)); 299 slices.add(processBlackSlice(NORMAL)); 300 slices.add(processWhiteSlice(WIDE)); 301 slices.add(processBlackSlice(NORMAL)); 302 break; 303 304 case 'P': 305 306 slices.add(processBlackSlice(NORMAL)); 307 slices.add(processWhiteSlice(NORMAL)); 308 slices.add(processBlackSlice(WIDE)); 309 slices.add(processWhiteSlice(NORMAL)); 310 slices.add(processBlackSlice(WIDE)); 311 slices.add(processWhiteSlice(NORMAL)); 312 slices.add(processBlackSlice(NORMAL)); 313 slices.add(processWhiteSlice(WIDE)); 314 slices.add(processBlackSlice(NORMAL)); 315 break; 316 317 case 'Q': 318 319 slices.add(processBlackSlice(NORMAL)); 320 slices.add(processWhiteSlice(NORMAL)); 321 slices.add(processBlackSlice(NORMAL)); 322 slices.add(processWhiteSlice(NORMAL)); 323 slices.add(processBlackSlice(NORMAL)); 324 slices.add(processWhiteSlice(NORMAL)); 325 slices.add(processBlackSlice(WIDE)); 326 slices.add(processWhiteSlice(WIDE)); 327 slices.add(processBlackSlice(WIDE)); 328 break; 329 330 case 'R': 331 332 slices.add(processBlackSlice(WIDE)); 333 slices.add(processWhiteSlice(NORMAL)); 334 slices.add(processBlackSlice(NORMAL)); 335 slices.add(processWhiteSlice(NORMAL)); 336 slices.add(processBlackSlice(NORMAL)); 337 slices.add(processWhiteSlice(NORMAL)); 338 slices.add(processBlackSlice(WIDE)); 339 slices.add(processWhiteSlice(WIDE)); 340 slices.add(processBlackSlice(NORMAL)); 341 break; 342 343 case 'S': 344 345 slices.add(processBlackSlice(NORMAL)); 346 slices.add(processWhiteSlice(NORMAL)); 347 slices.add(processBlackSlice(WIDE)); 348 slices.add(processWhiteSlice(NORMAL)); 349 slices.add(processBlackSlice(NORMAL)); 350 slices.add(processWhiteSlice(NORMAL)); 351 slices.add(processBlackSlice(WIDE)); 352 slices.add(processWhiteSlice(WIDE)); 353 slices.add(processBlackSlice(NORMAL)); 354 break; 355 356 case 'T': 357 358 slices.add(processBlackSlice(NORMAL)); 359 slices.add(processWhiteSlice(NORMAL)); 360 slices.add(processBlackSlice(NORMAL)); 361 slices.add(processWhiteSlice(NORMAL)); 362 slices.add(processBlackSlice(WIDE)); 363 slices.add(processWhiteSlice(NORMAL)); 364 slices.add(processBlackSlice(WIDE)); 365 slices.add(processWhiteSlice(WIDE)); 366 slices.add(processBlackSlice(NORMAL)); 367 break; 368 369 case 'U': 370 371 slices.add(processBlackSlice(WIDE)); 372 slices.add(processWhiteSlice(WIDE)); 373 slices.add(processBlackSlice(NORMAL)); 374 slices.add(processWhiteSlice(NORMAL)); 375 slices.add(processBlackSlice(NORMAL)); 376 slices.add(processWhiteSlice(NORMAL)); 377 slices.add(processBlackSlice(NORMAL)); 378 slices.add(processWhiteSlice(NORMAL)); 379 slices.add(processBlackSlice(WIDE)); 380 break; 381 382 case 'V': 383 384 slices.add(processBlackSlice(NORMAL)); 385 slices.add(processWhiteSlice(WIDE)); 386 slices.add(processBlackSlice(WIDE)); 387 slices.add(processWhiteSlice(NORMAL)); 388 slices.add(processBlackSlice(NORMAL)); 389 slices.add(processWhiteSlice(NORMAL)); 390 slices.add(processBlackSlice(NORMAL)); 391 slices.add(processWhiteSlice(NORMAL)); 392 slices.add(processBlackSlice(WIDE)); 393 break; 394 395 case 'W': 396 397 slices.add(processBlackSlice(WIDE)); 398 slices.add(processWhiteSlice(WIDE)); 399 slices.add(processBlackSlice(WIDE)); 400 slices.add(processWhiteSlice(NORMAL)); 401 slices.add(processBlackSlice(NORMAL)); 402 slices.add(processWhiteSlice(NORMAL)); 403 slices.add(processBlackSlice(NORMAL)); 404 slices.add(processWhiteSlice(NORMAL)); 405 slices.add(processBlackSlice(NORMAL)); 406 break; 407 408 case 'X': 409 410 slices.add(processBlackSlice(NORMAL)); 411 slices.add(processWhiteSlice(WIDE)); 412 slices.add(processBlackSlice(NORMAL)); 413 slices.add(processWhiteSlice(NORMAL)); 414 slices.add(processBlackSlice(WIDE)); 415 slices.add(processWhiteSlice(NORMAL)); 416 slices.add(processBlackSlice(NORMAL)); 417 slices.add(processWhiteSlice(NORMAL)); 418 slices.add(processBlackSlice(WIDE)); 419 break; 420 421 422 case 'Y': 423 424 slices.add(processBlackSlice(WIDE)); 425 slices.add(processWhiteSlice(WIDE)); 426 slices.add(processBlackSlice(NORMAL)); 427 slices.add(processWhiteSlice(NORMAL)); 428 slices.add(processBlackSlice(WIDE)); 429 slices.add(processWhiteSlice(NORMAL)); 430 slices.add(processBlackSlice(NORMAL)); 431 slices.add(processWhiteSlice(NORMAL)); 432 slices.add(processBlackSlice(NORMAL)); 433 break; 434 435 case 'Z': 436 437 slices.add(processBlackSlice(NORMAL)); 438 slices.add(processWhiteSlice(WIDE)); 439 slices.add(processBlackSlice(WIDE)); 440 slices.add(processWhiteSlice(NORMAL)); 441 slices.add(processBlackSlice(WIDE)); 442 slices.add(processWhiteSlice(NORMAL)); 443 slices.add(processBlackSlice(NORMAL)); 444 slices.add(processWhiteSlice(NORMAL)); 445 slices.add(processBlackSlice(NORMAL)); 446 break; 447 448 449 case '0': 450 451 slices.add(processBlackSlice(NORMAL)); 452 slices.add(processWhiteSlice(NORMAL)); 453 slices.add(processBlackSlice(NORMAL)); 454 slices.add(processWhiteSlice(WIDE)); 455 slices.add(processBlackSlice(WIDE)); 456 slices.add(processWhiteSlice(NORMAL)); 457 slices.add(processBlackSlice(NORMAL)); 458 slices.add(processWhiteSlice(WIDE)); 459 slices.add(processBlackSlice(NORMAL)); 460 break; 461 462 case '1': 463 464 slices.add(processBlackSlice(WIDE)); 465 slices.add(processWhiteSlice(NORMAL)); 466 slices.add(processBlackSlice(NORMAL)); 467 slices.add(processWhiteSlice(WIDE)); 468 slices.add(processBlackSlice(NORMAL)); 469 slices.add(processWhiteSlice(NORMAL)); 470 slices.add(processBlackSlice(NORMAL)); 471 slices.add(processWhiteSlice(NORMAL)); 472 slices.add(processBlackSlice(WIDE)); 473 break; 474 475 case '2': 476 477 slices.add(processBlackSlice(NORMAL)); 478 slices.add(processWhiteSlice(NORMAL)); 479 slices.add(processBlackSlice(WIDE)); 480 slices.add(processWhiteSlice(WIDE)); 481 slices.add(processBlackSlice(NORMAL)); 482 slices.add(processWhiteSlice(NORMAL)); 483 slices.add(processBlackSlice(NORMAL)); 484 slices.add(processWhiteSlice(NORMAL)); 485 slices.add(processBlackSlice(WIDE)); 486 break; 487 488 case '3': 489 490 slices.add(processBlackSlice(WIDE)); 491 slices.add(processWhiteSlice(NORMAL)); 492 slices.add(processBlackSlice(WIDE)); 493 slices.add(processWhiteSlice(WIDE)); 494 slices.add(processBlackSlice(NORMAL)); 495 slices.add(processWhiteSlice(NORMAL)); 496 slices.add(processBlackSlice(NORMAL)); 497 slices.add(processWhiteSlice(NORMAL)); 498 slices.add(processBlackSlice(NORMAL)); 499 break; 500 501 case '4': 502 slices.add(processBlackSlice(NORMAL)); 503 slices.add(processWhiteSlice(NORMAL)); 504 slices.add(processBlackSlice(NORMAL)); 505 slices.add(processWhiteSlice(WIDE)); 506 slices.add(processBlackSlice(WIDE)); 507 slices.add(processWhiteSlice(NORMAL)); 508 slices.add(processBlackSlice(NORMAL)); 509 slices.add(processWhiteSlice(NORMAL)); 510 slices.add(processBlackSlice(WIDE)); 511 break; 512 513 case '5': 514 515 slices.add(processBlackSlice(WIDE)); 516 slices.add(processWhiteSlice(NORMAL)); 517 slices.add(processBlackSlice(NORMAL)); 518 slices.add(processWhiteSlice(WIDE)); 519 slices.add(processBlackSlice(WIDE)); 520 slices.add(processWhiteSlice(NORMAL)); 521 slices.add(processBlackSlice(NORMAL)); 522 slices.add(processWhiteSlice(NORMAL)); 523 slices.add(processBlackSlice(NORMAL)); 524 break; 525 526 case '6': 527 528 slices.add(processBlackSlice(NORMAL)); 529 slices.add(processWhiteSlice(NORMAL)); 530 slices.add(processBlackSlice(WIDE)); 531 slices.add(processWhiteSlice(WIDE)); 532 slices.add(processBlackSlice(WIDE)); 533 slices.add(processWhiteSlice(NORMAL)); 534 slices.add(processBlackSlice(NORMAL)); 535 slices.add(processWhiteSlice(NORMAL)); 536 slices.add(processBlackSlice(NORMAL)); 537 break; 538 539 case '7': 540 541 slices.add(processBlackSlice(NORMAL)); 542 slices.add(processWhiteSlice(NORMAL)); 543 slices.add(processBlackSlice(NORMAL)); 544 slices.add(processWhiteSlice(WIDE)); 545 slices.add(processBlackSlice(NORMAL)); 546 slices.add(processWhiteSlice(NORMAL)); 547 slices.add(processBlackSlice(WIDE)); 548 slices.add(processWhiteSlice(NORMAL)); 549 slices.add(processBlackSlice(WIDE)); 550 break; 551 552 case '8': 553 554 slices.add(processBlackSlice(WIDE)); 555 slices.add(processWhiteSlice(NORMAL)); 556 slices.add(processBlackSlice(NORMAL)); 557 slices.add(processWhiteSlice(WIDE)); 558 slices.add(processBlackSlice(NORMAL)); 559 slices.add(processWhiteSlice(NORMAL)); 560 slices.add(processBlackSlice(WIDE)); 561 slices.add(processWhiteSlice(NORMAL)); 562 slices.add(processBlackSlice(NORMAL)); 563 break; 564 565 case '9': 566 567 slices.add(processBlackSlice(NORMAL)); 568 slices.add(processWhiteSlice(NORMAL)); 569 slices.add(processBlackSlice(WIDE)); 570 slices.add(processWhiteSlice(WIDE)); 571 slices.add(processBlackSlice(NORMAL)); 572 slices.add(processWhiteSlice(NORMAL)); 573 slices.add(processBlackSlice(WIDE)); 574 slices.add(processWhiteSlice(NORMAL)); 575 slices.add(processBlackSlice(NORMAL)); 576 break; 577 } 578 579 encoding.add(slices); 580 581 } // end for 582 583 584 return encoding; 585 } 586 587 public Slice processBlackSlice(int type) 588 { 589 return processBlackSlice(type, DEFAULT_HEIGHT); 590 } 591 592 public Slice processBlackSlice(int type, int height) 593 { 594 if(type == NORMAL) 595 { 596 return new Slice(0, 0, NORMAL_BAR_WIDTH, height, BLACK); 597 } 598 else if(type == WIDE) 599 { 600 return new Slice(0, 0, WIDE_BAR_WIDTH, height, BLACK); 601 } else 602 { 603 return new Slice(0, 0, NORMAL_BAR_WIDTH, height, WHITE); 604 } 605 } 606 607 public Slice processWhiteSlice(int type) 608 { 609 return processWhiteSlice(type, DEFAULT_HEIGHT); 610 } 611 612 public Slice processWhiteSlice(int type, int height) 613 { 614 if(type == NORMAL) 615 { 616 return new Slice(0, 0, NORMAL_BAR_WIDTH, height, WHITE); 617 } 618 else if(type == WIDE) 619 { 620 return new Slice(0, 0, WIDE_BAR_WIDTH, height, WHITE); 621 } 622 else 623 { 624 return new Slice(0, 0, NORMAL_BAR_WIDTH, height, WHITE); 625 } 626 } 627 } 628 629 public static class UPCEncoding implements Encoding 630 { 631 public String src; 632 public ArrayList encoding; 633 634 public static final int DEFAULT_START_X = 0; 635 public static final int DEFAULT_START_Y = 0; 636 public static final int DEFAULT_HEIGHT = 60; 637 public static final int ENCODING_MID = 5; 638 public static final int ENCODING_END = 10; 639 640 public UPCEncoding(String s) 641 { 642 src = s; 643 encoding = new ArrayList(); 644 } 645 646 public boolean verifyInput(String src) throws Exception 647 { 648 if(src == null) 649 { 650 throw new Exception("generateUPC(): expected non-empty input."); 651 } 652 653 // pre-process and verify that this is a sequence of digits. 654 if(src.length() != 10) 655 { 656 throw new Exception("generateUPC(): expected input containing 10 digits."); 657 } 658 659 for(int i = 0; i < src.length(); i++) 660 { 661 char c = src.charAt(i); 662 if(!Character.isDigit(c)) 663 { 664 throw new Exception("generateUPC(): expected input containing all digits."); 665 } 666 } 667 668 return true; 669 } 670 671 public ArrayList encode() 672 { 673 tokenEncoding slices; 674 675 // logic for first five digits. 676 for(int i = 0; i < ENCODING_MID && i < src.length(); i++) 677 { 678 char c = src.charAt(i); 679 slices = new tokenEncoding(c); 680 switch(c) 681 { 682 case '0': 683 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 684 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 685 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 686 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 687 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 688 break; 689 case '1': 690 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 691 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 692 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 693 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 694 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 695 break; 696 case '2': 697 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 698 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 699 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 700 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 701 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 702 break; 703 case '3': 704 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 705 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 706 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 707 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 708 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 709 break; 710 case '4': 711 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 712 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 713 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 714 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 715 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 716 717 break; 718 case '5': 719 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 720 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 721 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 722 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 723 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 724 break; 725 case '6': 726 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 727 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 728 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 729 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 730 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 731 break; 732 case '7': 733 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 734 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 735 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 736 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 737 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 738 break; 739 case '8': 740 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 741 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 742 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 743 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 744 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 745 break; 746 case '9': 747 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 748 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 749 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 750 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 751 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 752 break; 753 default: 754 break; 755 } 756 757 encoding.add(slices); 758 } 759 760 // insert middle bars. 761 slices = new tokenEncoding(tokenEncoding.EMPTY_CHAR); 762 763 slices.add(processBlackSlice(DEFAULT_START_X, -10, 1, 80)); 764 slices.add(processWhiteSlice(DEFAULT_START_X, -10, 1, 80)); 765 slices.add(processBlackSlice(DEFAULT_START_X, -10, 1, 80)); 766 slices.add(processWhiteSlice(DEFAULT_START_X, -10, 1, 80)); 767 768 encoding.add(slices); 769 770 // switch to right-parity. 771 772 // logic for last five digits. 773 for(int i = ENCODING_MID; i < ENCODING_END && i < src.length(); i++) 774 { 775 char c = src.charAt(i); 776 slices = new tokenEncoding(c); 777 switch(c) 778 { 779 case '0': 780 781 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 782 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 783 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 784 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 785 break; 786 case '1': 787 788 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 789 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 790 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 791 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 792 break; 793 case '2': 794 795 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 796 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 797 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 798 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 799 break; 800 case '3': 801 802 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 803 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 5)); 804 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 805 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 806 break; 807 case '4': 808 809 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 810 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 811 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 812 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 813 814 break; 815 case '5': 816 817 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 818 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 819 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 820 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 821 break; 822 case '6': 823 824 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 825 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 826 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 827 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 5)); 828 break; 829 case '7': 830 831 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 832 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 833 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 834 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 835 break; 836 case '8': 837 838 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 839 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 840 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 841 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 4)); 842 break; 843 case '9': 844 845 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 846 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 2)); 847 slices.add(processBlackSlice(DEFAULT_START_X, DEFAULT_START_Y, 1)); 848 slices.add(processWhiteSlice(DEFAULT_START_X, DEFAULT_START_Y, 3)); 849 break; 850 default: 851 break; 852 } 853 854 encoding.add(slices); 855 } 856 857 return encoding; 858 } 859 860 public Slice processBlackSlice(int relStartX, int relStartY, int width) 861 { 862 return processBlackSlice(relStartX, relStartY, width, DEFAULT_HEIGHT); 863 } 864 865 public Slice processBlackSlice(int relStartX, int relStartY, int width, int height) 866 { 867 return new Slice(relStartX, relStartY, width, height, BLACK); 868 } 869 870 public Slice processWhiteSlice(int relStartX, int relStartY, int width) 871 { 872 return processWhiteSlice(relStartX, relStartY, width, DEFAULT_HEIGHT); 873 } 874 875 public Slice processWhiteSlice(int relStartX, int relStartY, int width, int height) 876 { 877 return new Slice(relStartX, relStartY, width, height, WHITE); 878 } 879 } 880 881 public static ArrayList generateCode39(String src) throws Exception 882 { 883 code39Encoding encoding = new code39Encoding(src); 884 encoding.verifyInput(src); 885 ArrayList arr = encoding.encode(); 886 return arr; 887 } 888 889 /* 890 public ArrayList generateUPC(String src) throws Exception 891 { 892 UPCEncoding encoding = new UPCEncoding(src); 893 encoding.verifyInput(src); 894 return encoding.encode(); 895 } 896 */ 897 }`