lib/rtesseract/mixed.rb in rtesseract-1.0.5 vs lib/rtesseract/mixed.rb in rtesseract-1.1.0
- old
+ new
@@ -1,54 +1,51 @@
# encoding: UTF-8
class RTesseract
+ # Class to read an image from specified areas
class Mixed
- def initialize(src="", options={})
+ attr_reader :areas
+
+ def initialize(src = '', options = {})
@source = Pathname.new src
@options = options
- @value = ""
+ @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}
+ @value = ''
+ @areas << { :x => x, :y => y, :width => width, :height => height }
end
- def areas
- @areas
- end
-
def clear_areas
@areas = []
end
- #Convert parts of image to string
+ # Convert parts of image to string
def convert
- @value = ""
- @areas.each do |area|
- image = RTesseract.new(@source.to_s,@options.dup)
- image.crop!(area[:x].to_i, area[:y].to_i, area[:width].to_i, area[:height].to_i)
+ @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
- raise RTesseract::ConversionError
+ rescue => error
+ raise RTesseract::ConversionError.new(error)
end
- #Output value
+ # Output value
def to_s
- return @value if @value != ""
+ return @value if @value != ''
if @source.file?
convert
@value
else
- raise RTesseract::ImageNotSelectedError
+ fail RTesseract::ImageNotSelectedError.new(@source)
end
end
- #Remove spaces and break-lines
+ # Remove spaces and break-lines
def to_s_without_spaces
- to_s.gsub(" ","").gsub("\n","").gsub("\r","")
+ to_s.gsub(' ', '').gsub("\n", '').gsub("\r", '')
end
end
end
-