Class | Barby::EAN13 |
In: |
lib/barby/barcode/ean_13.rb
|
Parent: | Barcode1D |
EAN-13, aka UPC-A, barcodes are the ones you can see at your local supermarket, in your house and, well, everywhere..
To use this for a UPC barcode, just add a 0 to the front
LEFT_ENCODINGS_ODD | = | { 0 => '0001101', 1 => '0011001', 2 => '0010011', 3 => '0111101', 4 => '0100011', 5 => '0110001', 6 => '0101111', 7 => '0111011', 8 => '0110111', 9 => '0001011' | ||
LEFT_ENCODINGS_EVEN | = | { 0 => '0100111', 1 => '0110011', 2 => '0011011', 3 => '0100001', 4 => '0011101', 5 => '0111001', 6 => '0000101', 7 => '0010001', 8 => '0001001', 9 => '0010111' | ||
RIGHT_ENCODINGS | = | { 0 => '1110010', 1 => '1100110', 2 => '1101100', 3 => '1000010', 4 => '1011100', 5 => '1001110', 6 => '1010000', 7 => '1000100', 8 => '1001000', 9 => '1110100' | ||
LEFT_PARITY_MAPS | = | { 0 => [:odd, :odd, :odd, :odd, :odd, :odd], #UPC-A 1 => [:odd, :odd, :even, :odd, :even, :even], 2 => [:odd, :odd, :even, :even, :odd, :even], 3 => [:odd, :odd, :even, :even, :even, :odd], 4 => [:odd, :even, :odd, :odd, :even, :even], 5 => [:odd, :even, :even, :odd, :odd, :even], 6 => [:odd, :even, :even, :even, :odd, :odd], 7 => [:odd, :even, :odd, :even, :odd, :even], 8 => [:odd, :even, :odd, :even, :even, :odd], 9 => [:odd, :even, :even, :odd, :even, :odd] | Describes whether the left-hand encoding should use LEFT_ENCODINGS_ODD or LEFT_ENCODINGS_EVEN, based on the first digit in the number system (and the barcode as a whole) | |
START | = | '101' | These are the lines that "stick down" in the graphical representation | |
CENTER | = | '01010' | ||
STOP | = | '101' | ||
FORMAT | = | /^\d{12}$/ | EAN-13 barcodes have 12 digits + check digit |
data | [RW] |
# File lib/barby/barcode/ean_13.rb, line 59 59: def initialize(data) 60: self.data = data 61: raise ArgumentError, 'data not valid' unless valid? 62: end
Mod10
# File lib/barby/barcode/ean_13.rb, line 136 136: def checksum 137: mod = weighted_sum % 10 138: mod.zero? ? 0 : 10-mod 139: end
# File lib/barby/barcode/ean_13.rb, line 141 141: def checksum_encoding 142: RIGHT_ENCODINGS[checksum] 143: end
# File lib/barby/barcode/ean_13.rb, line 95 95: def data_with_checksum 96: data + checksum.to_s 97: end
# File lib/barby/barcode/ean_13.rb, line 118 118: def encoding 119: start_encoding+left_encoding+center_encoding+right_encoding+stop_encoding 120: end
# File lib/barby/barcode/ean_13.rb, line 110 110: def left_encoding 111: left_encodings.join 112: end
# File lib/barby/barcode/ean_13.rb, line 100 100: def left_encodings 101: left_parity_map.zip(left_numbers).map do |parity,number| 102: parity == :odd ? LEFT_ENCODINGS_ODD[number] : LEFT_ENCODINGS_EVEN[number] 103: end 104: end
Numbers that are encoded to the left of the center The first digit is not included
# File lib/barby/barcode/ean_13.rb, line 80 80: def left_numbers 81: numbers[1,6] 82: end
# File lib/barby/barcode/ean_13.rb, line 90 90: def numbers_with_checksum 91: numbers + [checksum] 92: end
# File lib/barby/barcode/ean_13.rb, line 73 73: def odd_and_even_numbers 74: alternater = false 75: numbers.reverse.partition{ alternater = !alternater } 76: end
# File lib/barby/barcode/ean_13.rb, line 114 114: def right_encoding 115: right_encodings.join 116: end
# File lib/barby/barcode/ean_13.rb, line 106 106: def right_encodings 107: right_numbers.map{|n| RIGHT_ENCODINGS[n] } 108: end
Is this a UPC-A barcode? UPC barcodes are EAN codes that start with 0
# File lib/barby/barcode/ean_13.rb, line 158 158: def upc? 159: numbers.first.zero? 160: end