Files
2016-03-01 21:59:26 +08:00

39 lines
1.5 KiB
Ruby

class Api::V1::OcrController < ApplicationController
skip_before_action :authenticate_user_from_token
BAIDU_KEY = 'a16b7587834498e95bef733ecbfa155d'
API_URL = 'http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid'
def index
# img = imgs.first.threshold(Magick::QuantumRange*0.35)
Magick::Image.read('/Users/twer/Desktop/61.jpg').first.minify.write('/Users/twer/Desktop/4.jpg')
image_base_str = Base64.strict_encode64(IO.read('/Users/twer/Desktop/4.jpg'))
response = RestClient::Request.execute(method: :post, url: API_URL,
timeout: 10,
headers: {apikey: BAIDU_KEY},
payload:
{fromdevice: 'pc',
clientip: '127.0.0.1',
detecttype: 'LocateRecognize',
languagetype: 'CHN_ENG',
imagetype: '1',
image: image_base_str
})
json = JSON.parse(response.body)
ap json
if json["retData"].length != 0
words = ""
json["retData"].each do |line|
words += line["word"]
end
render json: {words: words}
else
render json: {words: '识别失败'}, status: 400
end
end
end