Class: Escper::Img
- Inherits:
-
Object
- Object
- Escper::Img
- Defined in:
- lib/escper/image.rb
Instance Method Summary collapse
- #convert(img = nil) ⇒ Object
- #crop(image) ⇒ Object
-
#initialize(data, type) ⇒ Img
constructor
A new instance of Img.
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data, type) ⇒ Img
Returns a new instance of Img.
3 4 5 6 7 8 9 10 11 |
# File 'lib/escper/image.rb', line 3 def initialize(data, type) if type == :file @image = convert(Magick::Image.read(data).first) elsif type == :blob @image = convert(Magick::Image.from_blob(data).first) elsif type == :obj @image = convert(data) end end |
Instance Method Details
#convert(img = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/escper/image.rb', line 13 def convert(img=nil) if img.nil? and @image @image = @image.quantize 2, Magick::GRAYColorspace @image = crop(@image) return @image else img = img.quantize 2, Magick::GRAYColorspace img = crop(img) return img end end |
#crop(image) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/escper/image.rb', line 25 def crop(image) @x = (image.columns / 8.0).round @y = (image.rows / 8.0).round @x = 1 if @x == 0 @y = 1 if @y == 0 image = image.extent @x * 8, @y * 8 return image end |
#to_a ⇒ Object
34 35 36 37 |
# File 'lib/escper/image.rb', line 34 def to_a colorarray = @image.export_pixels return colorarray end |
#to_s ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/escper/image.rb', line 39 def to_s colorarray = self.to_a bits = [] mask = 0x80 i = 0 temp = 0 (@x * @y * 8 * 3 * 8).times do |j| next unless (j % 3).zero? temp |= mask if colorarray[j] == 0 # put 1 in place if black mask = mask >> 1 # shift mask i += 3 if i == 24 bits << temp mask = 0x80 i = 0 temp = 0 end end result = bits.pack("C*") p_cmd = "\x1D\x76\x30\x00" hdr = [@x, (@y*8)].pack("SS") escpos = '' escpos.encode! 'ASCII-8BIT' escpos += p_cmd + hdr + result return escpos end |