Guest User

Untitled

a guest
Feb 20th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. # barcode/code39.rb
  2. #
  3. # Author:: Clinton R. Nixon <crnixon@gmail.com>
  4. # Copyright:: Copyright (c) 2005 Demand Publishing
  5. # License:: Distributes under the same terms as Ruby
  6. # Version:: 0.2
  7.  
  8. require_gem('rmagick')
  9. require 'rmagick'
  10. include Magick
  11.  
  12. # This class takes a string and generates a Code 39-based barcode
  13. # from that string.
  14. class Code39
  15.  
  16. # quick reference as to what's up with @@encoding:
  17. # - B => large solid bar
  18. # - b => small solid bar
  19. # - W => large space
  20. # - w => small space
  21. @@encoding = {
  22. '0' => "bwbWBwBwb",
  23. '1' => "BwbWbwbwB",
  24. '2' => "bwBWbwbwB",
  25. '3' => "BwBWbwbwb",
  26. '4' => "bwbWBwbwB",
  27. '5' => "BwbWBwbwb",
  28. '6' => "bwBWBwbwb",
  29. '7' => "bwbWbwBwB",
  30. '8' => "BwbWbwBwb",
  31. '9' => "bwBWbwBwb",
  32. 'A' => "BwbwbWbwB",
  33. 'B' => "bwBwbWbwB",
  34. 'C' => "BwBwbWbwb",
  35. 'D' => "bwbwBWbwB",
  36. 'E' => "BwbwBWbwb",
  37. 'F' => "bwBwBWbwb",
  38. 'G' => "bwbwbWBwB",
  39. 'H' => "BwbwbWBwb",
  40. 'I' => "bwBwbWBwb",
  41. 'J' => "bwbwBWBwb",
  42. 'K' => "BwbwbwbWB",
  43. 'L' => "bwBwbwbWB",
  44. 'M' => "BwBwbwbWb",
  45. 'N' => "bwbwBwbWB",
  46. 'O' => "BwbwBwbWb",
  47. 'P' => "bwBwBwbWb",
  48. 'Q' => "bwbwbwBWB",
  49. 'R' => "BwbwbwBWb",
  50. 'S' => "bwBwbwBWb",
  51. 'T' => "bwbwBwBWb",
  52. 'U' => "BWbwbwbwB",
  53. 'V' => "bWBwbwbwB",
  54. 'W' => "BWBwbwbwb",
  55. 'X' => "bWbwBwbwB",
  56. 'Y' => "BWbwBwbwb",
  57. 'Z' => "bWBwBwbwb",
  58. '-' => "bWbwbwBwB",
  59. '.' => "BWbwbwBwb",
  60. ' ' => "bWBwbwBwb",
  61. '$' => "bWbWbWbwb",
  62. '/' => "bWbWbwbWb",
  63. '+' => "bWbwbWbWb",
  64. '%' => "bwbWbWbWb",
  65. '*' => "bWbwBwBwb"
  66. }
  67.  
  68. # Takes a string, uppercases it, and wraps it in asterisks.
  69. # If any character is unavailable in Code 39 encoding,
  70. # throws a vicious error.
  71. def initialize(text)
  72. text = text.upcase
  73. text.split(//).each do |char|
  74. if not @@encoding.has_key?(char)
  75. raise "Unencodable string."
  76. end
  77. end
  78.  
  79. @text = "*#{text}*"
  80. end
  81.  
  82. # Outputs a string that represents the encoding.
  83. # Not necessarily useful externally, but
  84. # who knows?
  85. def to_s
  86. output = String.new()
  87. @text.split(//).each do |char|
  88. output += @@encoding[char] + " "
  89. end
  90. return output
  91. end
  92.  
  93.  
  94. # The overly complicated image generation code.
  95. # Your barcode image width and height should be in pixels.
  96. # The actual barcode will not be that width, instead
  97. # it will be a multiple of your text string (plus two
  98. # asterisks - @text) length * 14. White space will center
  99. # the rest.
  100. def to_img(filename, width, height)
  101. bar_code_width = width - (width % ( @text.length * 14 ) )
  102. letter_width = bar_code_width / @text.length
  103. small_bar_width = letter_width / 14
  104. large_bar_width = ((7.0 / 3.0) * small_bar_width.to_f).to_i
  105.  
  106. canvas = Image.new(width, height)
  107. gc = Draw.new
  108. gc.fill('black')
  109. gc.stroke('black')
  110. gc.stroke_width(-1)
  111. # Necessary to prevent massive blurry image nonsense.
  112. gc.stroke_antialias(false)
  113.  
  114. cur_x = (width - bar_code_width) / 2
  115.  
  116. self.to_s.split(//).each do |char|
  117. if char.upcase == "B"
  118. gc.rectangle(cur_x, 0, cur_x + (char == "B" ? large_bar_width : small_bar_width), height)
  119. end
  120. cur_x += char.match(/[BW]/) ? large_bar_width : small_bar_width
  121. end
  122. gc.draw(canvas)
  123. canvas.write(filename)
  124. end
  125. end
Add Comment
Please, Sign In to add comment