diff --git a/app/controllers/api/v1/ocr_controller.rb b/app/controllers/api/v1/ocr_controller.rb index 07a8e2b..37305cb 100644 --- a/app/controllers/api/v1/ocr_controller.rb +++ b/app/controllers/api/v1/ocr_controller.rb @@ -5,28 +5,11 @@ class Api::V1::OcrController < ApplicationController API_URL = 'http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid' def index - img = Magick::Image.read('/Users/twer/Desktop/1.jpg') - ap img - image_base_str = Base64.strict_encode64(IO.read('/Users/twer/Desktop/1.jpg')) - # conn = Faraday.new(:url => API_URL) do |faraday| - # faraday.request :url_encoded - # faraday.request :multipart - # faraday.response :logger - # faraday.adapter Faraday.default_adapter - # end - # response = conn.post do |req| - # req.url '/idl_baidu/baiduocrpay/idlocrpaid' - # req.headers['apikey'] = BAIDU_KEY - # req.body = {fromdevice: 'pc', - # clientip: '10.10.10.0', - # detecttype: 'LocateRecognize', - # languagetype: 'CHN_ENG', - # imagetype: '2', - # # image: 'http://apistore.baidu.com/idlapi/img_demo_data/ocr/LocateRecognize//62.jpg' - # image: Faraday::UploadIO.new('/Users/twer/Desktop/1.jpg', 'image/jpeg') - # } - # end - url = 'http://apistore.baidu.com/idlapi/img_demo_data/ocr/LocateRecognize//62.jpg' + # 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}, @@ -40,7 +23,16 @@ class Api::V1::OcrController < ApplicationController }) json = JSON.parse(response.body) ap json - render text: 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