Created
May 30, 2015 15:55
-
-
Save SingularityMatrix/265390ba64d08544b58f to your computer and use it in GitHub Desktop.
Create and annotate barcodes in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'barby' | |
require 'barby/barcode/code_128' | |
require 'barby/outputter/png_outputter' | |
require 'mini_magick' | |
require 'benchmark' | |
# Build a sample barcode | |
def build_barcode(prod, supplier_code, batch_no) | |
bar_code = prod + "-" + supplier_code + "-"+ batch_no | |
bar_code_image = bar_code.delete("/") # Clear all /'s in the code if any .. | |
path = "#{Rails.root}/public/barcodes/#{bar_code_image}.png" | |
end | |
# Create a barcode image object like so .. | |
@barcode_object = Barby::Code128B.new(bar_code) | |
# Write the barcode image object to a file => barcode png image | |
File.open(path, 'w') { |f| f.write @barcode_object.to_png(:margin => 35, :xdim => 2,:ydim => 2, :height => 100) } | |
# Annotate text to barcode image using MiniMagick | |
def annotate_barcode(path) | |
img = MiniMagick::Image.open(path) | |
img.combine_options do |c| | |
c.font "#{Rails.root}/public/fonts/Calibri.ttf" | |
c.fill "black" | |
c.pointsize 20 | |
c.gravity "center" | |
c.draw "text 0,65 '#{bar_code}'" | |
end | |
img.write(path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can pass more options for annotation. See the
mini_magick
usage documentation here https://github.com/minimagick/minimagick#usage