Class: Barcode1DTools::Industrial2of5
- Defined in:
- lib/barcode1dtools/industrial2of5.rb
Overview
Barcode1DTools::Industrial2of5 - Create and decode bar patterns for Industrial 2 of 5. The value encoded is a number with digits 0-9. Internally, the value is treated as a string to preserve leading zeroes.
Use :checksum_included => true if you have already added a checksum and wish to have it validated, or :skip_checksum => false if you wish to have one added.
Industrial 2 of 5 is low-density and limited. It should not be used in any new applications.
Example
val = "3423"
bc = Barcode1DTools::Industrial2of5.new(val)
pattern = bc.
rle_pattern = bc.rle
width = bc.width
The object created is immutable.
Barcode1DTools::Industrial2of5 creates the patterns that you need to display Industrial 2 of 5 barcodes. It can also decode a simple w/n string.
Industrial2of5 characters consist of 3 bars and 2 spaces, with a narrow space between them. 2 of the bars/spaces in each symbol are wide.
Formats
There are three formats for the returned pattern:
bars - 1s and 0s specifying black lines and white spaces. Actual characters can be changed from “1” and 0“ with options :line_character and :space_character.
rle - Run-length-encoded version of the pattern. The first number is always a black line, with subsequent digits alternating between spaces and lines. The digits specify the width of each line or space.
wn - The native format for this barcode type. The string consists of a series of “w” and “n” characters. The first item is always a black line, with subsequent characters alternating between spaces and lines. A “wide” item is twice the width of a “narrow” item.
The “width” method will tell you the total end-to-end width, in units, of the entire barcode.
Rendering
The standard w/n ratio seems to be 2:1. There seem to be no real standards for display.
Constant Summary collapse
- CHAR_SEQUENCE =
Character sequence - 0-based offset in this string is character number
"0123456789"
- PATTERNS =
Patterns for making bar codes. Note that the position weights are 1, 2, 4, and 7 and the last bit is parity. The patterns are for the bars, all spaces are narrow.
{ '0'=> {'val'=>0 ,'wn'=>'nnnnwnwnn'}, '1'=> {'val'=>1 ,'wn'=>'wnnnnnnnw'}, '2'=> {'val'=>2 ,'wn'=>'nnwnnnnnw'}, '3'=> {'val'=>3 ,'wn'=>'wnwnnnnnn'}, '4'=> {'val'=>4 ,'wn'=>'nnnnwnnnw'}, '5'=> {'val'=>5 ,'wn'=>'wnnnwnnnn'}, '6'=> {'val'=>6 ,'wn'=>'nnwnwnnnn'}, '7'=> {'val'=>7 ,'wn'=>'nnnnnnwnw'}, '8'=> {'val'=>8 ,'wn'=>'wnnnnnwnn'}, '9'=> {'val'=>9 ,'wn'=>'nnwnnnwnn'} }
- GUARD_PATTERN_LEFT_WN =
Guard pattern for the left side.
'wnwnn'
- GUARD_PATTERN_RIGHT_WN =
Guard pattern for the right side.
'wnnnw'
- DEFAULT_OPTIONS =
{ :line_character => '1', :space_character => '0', :w_character => 'w', :n_character => 'n', :wn_ratio => '2', :skip_checksum => true }
Instance Attribute Summary
Attributes inherited from Barcode1D
#check_digit, #encoded_string, #options, #value
Class Method Summary collapse
-
.can_encode?(value) ⇒ Boolean
Returns true if the string is encodable in this symbology.
-
.decode(str, options = {}) ⇒ Object
Decode a string in rle or w/n format.
-
.generate_check_digit_for(value) ⇒ Object
Generates a check digit for a given value using the Luhn algorithm.
-
.validate_check_digit_for(value) ⇒ Object
Returns true if the final digit if the given string is a valid check digit for the rest of the string.
Instance Method Summary collapse
-
#bars ⇒ Object
Returns 1s and 0s (for “black” and “white”).
-
#initialize(value, options = {}) ⇒ Industrial2of5
constructor
Create a new Industrial2of5 object with the given value.
-
#rle ⇒ Object
Returns a run-length-encoded string representation.
-
#width ⇒ Object
Returns the total unit width of the bar code.
-
#wn ⇒ Object
Returns a string of “w” or “n” (“wide” and “narrow”).
Methods inherited from Barcode1D
bar_pair, bars_to_rle, rle_to_bars, rle_to_wn, wn_pair, wn_to_rle
Constructor Details
#initialize(value, options = {}) ⇒ Industrial2of5
Create a new Industrial2of5 object with the given value. Options are :line_character, :space_character, :w_character, :n_character, :checksum_included, and :skip_checksum.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 178 def initialize(value, = {}) @options = DEFAULT_OPTIONS.merge() # Can we encode this value? raise UnencodableCharactersError unless self.class.can_encode?(value) @value = value.to_s if @options[:skip_checksum] @encoded_string = value.to_s @value = value.to_s @check_digit = nil elsif @options[:checksum_included] @encoded_string = value.to_s raise ChecksumError unless self.class.validate_check_digit_for(@encoded_string) md = @encoded_string.match(/^(\d+?)(\d)$/) @value, @check_digit = md[1], md[2].to_i else @value = value.to_s @check_digit = self.class.generate_check_digit_for(@value) @encoded_string = "#{@value}#{@check_digit}" end end |
Class Method Details
.can_encode?(value) ⇒ Boolean
Returns true if the string is encodable in this symbology. Industrial2of5 can encode digits.
103 104 105 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 103 def can_encode?(value) value.to_s =~ /\A[0-9]+\z/ end |
.decode(str, options = {}) ⇒ Object
Decode a string in rle or w/n format. This will return a Industrial2of5 object.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 125 def decode(str, = {}) if str =~ /[^1-3]/ && str =~ /[^wn]/ raise UnencodableCharactersError, "Pattern must be rle or wn" end # ensure a wn string if str =~ /[1-3]/ str = str.tr('123','nww') end if str.reverse =~ /\A#{GUARD_PATTERN_LEFT_WN}n.*?#{GUARD_PATTERN_RIGHT_WN}\z/ str.reverse! end # Note that every other character is an "n" unless str =~ /\A([wn]n)+[wn]\z/ && str =~ /\A#{GUARD_PATTERN_LEFT_WN}n(.*?)#{GUARD_PATTERN_RIGHT_WN}\z/ raise UnencodableCharactersError, "Start/stop pattern is not detected." end wn_pattern = $1 # Each pattern is 5 bars and 4 spaces, with a space between. unless wn_pattern.size % 10 == 0 raise UnencodableCharactersError, "Wrong number of bars." end decoded_string = '' wn_pattern.scan(/(.{9})n/).each do |chunk| chunk = chunk.first found = false PATTERNS.each do |char,hsh| if chunk == hsh['wn'] decoded_string += char found = true break; end end raise UndecodableCharactersError, "Invalid sequence: #{chunk}" unless found end Industrial2of5.new(decoded_string, ) end |
.generate_check_digit_for(value) ⇒ Object
Generates a check digit for a given value using the Luhn algorithm.
108 109 110 111 112 113 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 108 def generate_check_digit_for(value) raise UnencodableCharactersError unless self.can_encode?(value) mult = 3 value = value.reverse.split('').inject(0) { |a,c| mult = 4 - mult ; a + c.to_i * mult } (10 - (value % 10)) % 10 end |
.validate_check_digit_for(value) ⇒ Object
Returns true if the final digit if the given string is a valid check digit for the rest of the string.
117 118 119 120 121 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 117 def validate_check_digit_for(value) raise UnencodableCharactersError unless self.can_encode?(value) md = value.match(/^(\d+?)(\d)$/) self.generate_check_digit_for(md[1]) == md[2].to_i end |
Instance Method Details
#bars ⇒ Object
Returns 1s and 0s (for “black” and “white”)
214 215 216 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 214 def @bars ||= self.class.(self.rle, @options) end |
#rle ⇒ Object
Returns a run-length-encoded string representation
209 210 211 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 209 def rle @rle ||= self.class.wn_to_rle(self.wn, @options) end |
#width ⇒ Object
Returns the total unit width of the bar code
219 220 221 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 219 def width @width ||= rle.split('').inject(0) { |a,c| a + c.to_i } end |
#wn ⇒ Object
Returns a string of “w” or “n” (“wide” and “narrow”).
204 205 206 |
# File 'lib/barcode1dtools/industrial2of5.rb', line 204 def wn @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s) end |