Class: RTesseract::Mixed

Inherits:
Object
  • Object
show all
Defined in:
lib/rtesseract/mixed.rb

Overview

Class to read an image from specified areas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src = '', options = {}) {|_self| ... } ⇒ Mixed

Returns a new instance of Mixed.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
# File 'lib/rtesseract/mixed.rb', line 7

def initialize(src = '', options = {})
  @source  = Pathname.new src
  @options = options
  @value   = ''
  @areas = options.delete(:areas) || []
  yield self if block_given?
end

Instance Attribute Details

#areasObject (readonly)

Returns the value of attribute areas.



5
6
7
# File 'lib/rtesseract/mixed.rb', line 5

def areas
  @areas
end

Instance Method Details

#area(x, y, width, height) ⇒ Object



15
16
17
18
# File 'lib/rtesseract/mixed.rb', line 15

def area(x, y, width, height)
  @value = ''
  @areas << { :x => x,  :y => y, :width => width, :height => height }
end

#clear_areasObject



20
21
22
# File 'lib/rtesseract/mixed.rb', line 20

def clear_areas
  @areas = []
end

#convertObject

Convert parts of image to string



25
26
27
28
29
30
31
32
33
# File 'lib/rtesseract/mixed.rb', line 25

def convert
  @value = []
  @areas.each_with_object(RTesseract.new(@source.to_s, @options.dup)) do |area, image|
    image.crop!(area[:x], area[:y], area[:width], area[:height])
    @value << image.to_s
  end
rescue => error
  raise RTesseract::ConversionError.new(error)
end

#to_sObject

Output value



36
37
38
39
40
41
42
43
44
# File 'lib/rtesseract/mixed.rb', line 36

def to_s
  return @value if @value != ''
  if @source.file?
    convert
    @value.join
  else
    fail RTesseract::ImageNotSelectedError.new(@source)
  end
end

#to_s_without_spacesObject

Remove spaces and break-lines



47
48
49
# File 'lib/rtesseract/mixed.rb', line 47

def to_s_without_spaces
  to_s.gsub(' ', '').gsub("\n", '').gsub("\r", '')
end