Class: Escpos::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/escpos/image.rb

Constant Summary collapse

VERSION =
"0.0.12"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_or_path, options = {}) ⇒ Image

Returns a new instance of Image.



17
18
19
20
21
22
23
24
25
# File 'lib/escpos/image.rb', line 17

def initialize(image_or_path, options = {})
  @options = options

  processor_klass_name = options.fetch(:processor)
  processor_klass = ImageProcessors.const_get(processor_klass_name)
  @processor = processor_klass.new image_or_path, options

  @processor.process!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/escpos/image.rb', line 15

def options
  @options
end

#processorObject (readonly)

Returns the value of attribute processor.



15
16
17
# File 'lib/escpos/image.rb', line 15

def processor
  @processor
end

Instance Method Details

#to_escposObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/escpos/image.rb', line 27

def to_escpos
  bits = []
  mask = 0x80
  i = 0
  temp = 0

  0.upto(processor.image.height - 1) do |y|
    0.upto(processor.image.width - 1) do |x|
      px = processor.get_pixel(x, y)
      value = px >= 128 ? 255 : 0
      value = (value << 8) | value
      temp |= mask if value == 0
      mask = mask >> 1
      i = i + 1
      if i == 8
        bits << temp
        mask = 0x80
        i = 0
        temp = 0
      end
    end
  end

  [
    Escpos.sequence(IMAGE),
    [ processor.image.width / 8, processor.image.height ].pack("SS"),
    bits.pack("C*")
  ].join
end