Class: Barby::RmagickOutputter

Inherits:
Outputter show all
Defined in:
lib/barby/outputter/rmagick_outputter.rb

Overview

Renders images from barcodes using RMagick

Registers the to_png, to_gif, to_jpg and to_image methods

Instance Attribute Summary collapse

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#initialize, register

Constructor Details

This class inherits a constructor from Barby::Outputter

Instance Attribute Details

#heightObject

The height of the barcode in px For 2D barcodes this is the number of “lines” * ydim



81
82
83
# File 'lib/barby/outputter/rmagick_outputter.rb', line 81

def height
  @height
end

#marginObject

The margin of each edge surrounding the barcode in pixels



106
107
108
# File 'lib/barby/outputter/rmagick_outputter.rb', line 106

def margin
  @margin
end

#xdimObject

X dimension. 1X == 1px



96
97
98
# File 'lib/barby/outputter/rmagick_outputter.rb', line 96

def xdim
  @xdim
end

#ydimObject

Y dimension. Only for 2D codes



101
102
103
# File 'lib/barby/outputter/rmagick_outputter.rb', line 101

def ydim
  @ydim
end

Instance Method Details

#full_heightObject

The height of the image. This is the height of the barcode + the top and bottom margin



118
119
120
# File 'lib/barby/outputter/rmagick_outputter.rb', line 118

def full_height
  height + (margin * 2)
end

#full_widthObject

The full width of the image. This is the width of the barcode + the left and right margin



112
113
114
# File 'lib/barby/outputter/rmagick_outputter.rb', line 112

def full_width
  width + (margin * 2)
end

#lengthObject

Number of modules (xdims) on the x axis



91
92
93
# File 'lib/barby/outputter/rmagick_outputter.rb', line 91

def length
  barcode.two_dimensional? ? encoding.first.length : encoding.length
end

#to_blob(format, *a) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/barby/outputter/rmagick_outputter.rb', line 32

def to_blob(format, *a)
  img = to_image(*a)
  blob = img.to_blob{|i| i.format = format }
  
  #Release the memory used by RMagick explicitly. Ruby's GC
  #isn't aware of it and can't clean it up automatically
  img.destroy! if img.respond_to?(:destroy!)
  
  blob
end

#to_gif(*a) ⇒ Object

Returns a string containint a GIF image



23
24
25
# File 'lib/barby/outputter/rmagick_outputter.rb', line 23

def to_gif(*a)
  to_blob('gif', *a)
end

#to_image(opts = {}) ⇒ Object

Returns an instance of Magick::Image



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/barby/outputter/rmagick_outputter.rb', line 44

def to_image(opts={})
  with_options opts do
    canvas = Magick::Image.new(full_width, full_height)
    bars = Magick::Draw.new

    x = margin
    y = margin

    if barcode.two_dimensional?
      encoding.each do |line|
        line.split(//).map{|c| c == '1' }.each do |bar|
          if bar
            bars.rectangle(x, y, x+(xdim-1), y+(ydim-1))
          end
          x += xdim
        end
        x = margin
        y += ydim
      end
    else
      booleans.each do |bar|
        if bar
          bars.rectangle(x, y, x+(xdim-1), y+(height-1))
        end
        x += xdim
      end
    end

    bars.draw(canvas)

    canvas
  end
end

#to_jpg(*a) ⇒ Object

Returns a string containing a JPEG image



28
29
30
# File 'lib/barby/outputter/rmagick_outputter.rb', line 28

def to_jpg(*a)
  to_blob('jpg', *a)
end

#to_png(*a) ⇒ Object

Returns a string containing a PNG image



18
19
20
# File 'lib/barby/outputter/rmagick_outputter.rb', line 18

def to_png(*a)
  to_blob('png', *a)
end

#widthObject

The width of the barcode in px



86
87
88
# File 'lib/barby/outputter/rmagick_outputter.rb', line 86

def width
  length * xdim
end