Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public class Code39Controller {
  2. // Determines if the check digit should be generated
  3. // If true, scanners must be enabled to use it
  4. public Boolean shouldCheckDigit { get; set; }
  5. // The source string to use. Currently only supports
  6. // the characters in the "keys" string. Do not use '*'.
  7. public String sourceCodeValue { get; set; }
  8.  
  9. // The index for supported characters.
  10. static String keys = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*';
  11.  
  12. // The binary representation of each character, 16 bits each.
  13. static String[] values = new String[] {
  14. '1010001110111010', '1110100010101110', '1011100010101110', '1110111000101010',
  15. '1010001110101110', '1110100011101010', '1011100011101010', '1010001011101110',
  16. '1110100010111010', '1011100010111010', '1110101000101110', '1011101000101110',
  17. '1110111010001010', '1010111000101110', '1110101110001010', '1011101110001010',
  18. '1010100011101110', '1110101000111010', '1011101000111010', '1010111000111010',
  19. '1110101010001110', '1011101010001110', '1110111010100010', '1010111010001110',
  20. '1110101110100010', '1011101110100010', '1010101110001110', '1110101011100010',
  21. '1011101011100010', '1010111011100010', '1110001010101110', '1000111010101110',
  22. '1110001110101010', '1000101110101110', '1110001011101010', '1000111011101010',
  23. '1000101011101110', '1110001010111010', '1000111010111010', '1000100010001010',
  24. '1000100010100010', '1000101000100010', '1010001000100010', '1000101110111010' };
  25.  
  26. // Renders the barcode on the screen
  27. public String[] getBarCodeBars() {
  28. return generateCode39(sourceCodeValue, shouldCheckDigit).split('');
  29. }
  30.  
  31. // Returns a string in case we also want to debug the output.
  32. String generateCode39(String source, Boolean checkDigit) {
  33. // Output
  34. String[] result = new String[0];
  35.  
  36. Integer index, // Temp variable
  37. total = 0; // Checksum calculation
  38.  
  39. // Avoid System.NullPointerException
  40. source = source == null? '': source;
  41. // Start character is *
  42. result.add(values[keys.indexOf('*')]);
  43. // For each character in source
  44. for(String sourceChar: source.toUpperCase().split('')) {
  45. // Valid character, add to checksum and output bits
  46. if((index = keys.indexOf(sourceChar)) > -1) {
  47. result.add(values[index]);
  48. total += index;
  49. }
  50. }
  51. // Add the check digit
  52. if(checkDigit) {
  53. result.add(values[Math.mod(total, 43)]);
  54. }
  55. // Add the stop character
  56. result.add(values[keys.indexOf('*')]);
  57. // Join as string
  58. return String.join(result,'');
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement