Module: BaiduOcr::FileRead

Defined in:
lib/baidu_ocr/file_read.rb

Class Method Summary collapse

Class Method Details

.read(image) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/baidu_ocr/file_read.rb', line 10

def read(image)
  # file_contents = open('local-file.jpg') { |f| f.read }
  # web_contents = open('http://www.xxx.com.jpg') {|f| f.read }
  tmpfile = Tempfile.new(["ocr", ".jpg"])
  begin
    # can not save image extension like '.jpg'
    # 百度是靠文件名来判断的,暂时采用temfile来自定义文件名
    # http://stackoverflow.com/questions/9940633/is-it-possible-to-have-open-uri-maintain-the-extension
    puts "loading file from #{image}"
    stream = open(image, 'rb').read
  rescue Errno::ENOENT => e
    raise NotFound, "#{e.message}"
  end
  tmpfile.write(stream)
  file = open(tmpfile)
  tmpfile.unlink

  file
end