Sha256: 2f5f49e3e7920bc1a1ad3b0833a8520017fde5335e6c2e7dd554adeedc2df9a9

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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(_points)
      @value = ''
      @areas << _points # { 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) # area[:x], area[:y], area[:width], area[:height])
        @value << image.to_s
      end
    rescue => error
      raise RTesseract::ConversionError.new(error), error, caller
    end

    # Output value
    def to_s
      return @value if @value != ''
      if @source.file?
        convert
        @value.join
      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

1 entries across 1 versions & 1 rubygems

Version Path
rtesseract-2.0.0 lib/rtesseract/mixed.rb