Sha256: 09512efc401a5ed3aebfdbd700b438f3e061373fbf9812e4a244f3675fcb7c20

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# encoding: UTF-8
class RTesseract
  # Class to read an image from specified areas
  class Mixed
    attr_reader :areas

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

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

    def clear_areas
      @areas = []
    end

    # Convert parts of image to string
    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

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

    # Remove spaces and break-lines
    def to_s_without_spaces
      to_s.gsub(' ', '').gsub("\n", '').gsub("\r", '')
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rtesseract-1.2.2 lib/rtesseract/mixed.rb
rtesseract-1.2.1 lib/rtesseract/mixed.rb
rtesseract-1.2.0 lib/rtesseract/mixed.rb
rtesseract-1.1.0 lib/rtesseract/mixed.rb