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:



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

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.



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

def areas
  @areas
end

Instance Method Details

#area(points) ⇒ Object

Add areas



17
18
19
20
# File 'lib/rtesseract/mixed.rb', line 17

def area(points)
  @value = ''
  @areas << points
end

#clear_areasObject

Clear areas



23
24
25
# File 'lib/rtesseract/mixed.rb', line 23

def clear_areas
  @areas = []
end

#convertObject

Convert parts of image to string



28
29
30
31
32
33
34
35
36
# File 'lib/rtesseract/mixed.rb', line 28

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

#to_sObject

Output value



39
40
41
42
43
44
45
46
47
# File 'lib/rtesseract/mixed.rb', line 39

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



50
51
52
# File 'lib/rtesseract/mixed.rb', line 50

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