diff --git a/Gemfile b/Gemfile index 2aaa712..c144a89 100644 --- a/Gemfile +++ b/Gemfile @@ -82,6 +82,7 @@ gem 'font-awesome-rails', '~> 4.4' gem "sentry-raven" #, :github => "getsentry/raven-ruby" gem "paranoia", "~> 2.0" +gem 'faraday' group :development do gem 'capistrano' diff --git a/Gemfile.lock b/Gemfile.lock index 600475f..45fc634 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -421,6 +421,7 @@ DEPENDENCIES coffee-rails (~> 4.1.0) devise devise-bootstrap-views + faraday font-awesome-rails (~> 4.4) groupdate houston diff --git a/app/controllers/api/v1/ocr_controller.rb b/app/controllers/api/v1/ocr_controller.rb new file mode 100644 index 0000000..fcdd082 --- /dev/null +++ b/app/controllers/api/v1/ocr_controller.rb @@ -0,0 +1,44 @@ +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 + 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' + 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 + render text: json + end + +end diff --git a/config/routes.rb b/config/routes.rb index 0260544..d1650bb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,6 +124,7 @@ Rails.application.routes.draw do get :most_loved_posts_in_month end + get '/ocr', to: 'ocr#index' end end end