转载

百度 OCR 文字识别服务正式对外开放

API调试工具: 登录后即可调试

接口地址: http://apis.baidu.com/apistore/idlocr/ocr

请求方法: POST

请求参数:
参数名 类型 必填 参数位置 描述 默认值
fromdevice
string bodyParam

来源,例如:android、iPhone、pc等

pc
clientip
string bodyParam
客户端出口IP
10.10.10.0
detecttype
string bodyParam

OCR接口类型,“LocateRecognize”;“Recognize”;“Locate”;“SingleCharRecognize”。LocateRecognize:整图文字检测、识别,以行为单位;Locate:整图文字行定位;Recognize:整图文字识别;SingleCharRecognize:单字图像识别

LocateRecognize

languagetype
string bodyParam

要检测的文字类型:目前支持 1. CHN_ENG(中英) 2. ENG 3.JAP(日文) 4.KOR(韩文) ,不填写这个字段默认为CHN_ENG

CHN_ENG
imagetype
string bodyParam

图片资源类型, 1.表示经过BASE64编码后的字串;2.图片原文件

1
image
string bodyParam

图片资源,目前仅支持jpg格式,原始图片size需小于300k

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k=

apikey
string header
API密钥
您自己的apikey
请求示例:

curl示例

php示例

python示例

java示例

C#示例

ObjectC示例

Swift示例

curl 'http://apis.baidu.com/apistore/idlocr/ocr' --data 'fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k=' -H 'apikey:您自己的apikey'
<?php     $ch = curl_init();     $url = 'http://apis.baidu.com/apistore/idlocr/ocr';     $header = array(         'Content-Type:application/x-www-form-urlencoded',         'apikey:您自己的apikey',     );     $data = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k=";     // 添加apikey到header     curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);     // 添加参数     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     // 执行HTTP请求     curl_setopt($ch , CURLOPT_URL , $url);     $res = curl_exec($ch);      var_dump(json_decode($res)); ?>
# -*- coding: utf-8 -*- import sys, urllib, urllib2, json  url = 'http://apis.baidu.com/apistore/idlocr/ocr'  data = {} data['fromdevice'] = "pc" data['clientip'] = "10.10.10.0" data['detecttype'] = "LocateRecognize" data['languagetype'] = "CHN_ENG" data['imagetype'] = "1" data['image'] = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k="  decoded_data = urllib.urlencode(data) req = urllib2.Request(url, data = decoded_data)  req.add_header("Content-Type", "application/x-www-form-urlencoded") req.add_header("apikey", "您自己的apikey")  resp = urllib2.urlopen(req) content = resp.read() if(content):     print(content)
String httpUrl = "http://apis.baidu.com/apistore/idlocr/ocr"; String httpArg = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k="; String jsonResult = request(httpUrl, httpArg); System.out.println(jsonResult);  /**  * @param urlAll  *            :请求接口  * @param httpArg  *            :参数  * @return 返回结果  */ public static String request(String httpUrl, String httpArg) {     BufferedReader reader = null;     String result = null;     StringBuffer sbf = new StringBuffer();      try {         URL url = new URL(httpUrl);         HttpURLConnection connection = (HttpURLConnection) url                 .openConnection();         connection.setRequestMethod("POST");         connection.setRequestProperty("Content-Type",                         "application/x-www-form-urlencoded");         // 填入apikey到HTTP header         connection.setRequestProperty("apikey",  "您自己的apikey");         connection.setDoOutput(true);         connection.getOutputStream().write(httpArg.getBytes("UTF-8"));         connection.connect();         InputStream is = connection.getInputStream();         reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));         String strRead = null;         while ((strRead = reader.readLine()) != null) {             sbf.append(strRead);             sbf.append("/r/n");         }         reader.close();         result = sbf.toString();     } catch (Exception e) {         e.printStackTrace();     }     return result; }
string url = "http://apis.baidu.com/apistore/idlocr/ocr"; string param = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k="; string result = request(url,param);  /// <summary> /// 发送HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="param">请求的参数</param> /// <returns>请求结果</returns> public static string request(string url, string param) {     string strURL = url + '?' + param;     System.Net.HttpWebRequest request;     request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);     request.Method = "POST";     // 添加header     request.Headers.Add("apikey", "您自己的apikey");     request.ContentType = "application/x-www-form-urlencoded";     string paraUrlCoded = param;     byte[] payload;     payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);     request.ContentLength = payload.Length;     Stream writer = request.GetRequestStream();     writer.Write(payload, 0, payload.Length);     writer.Close();     System.Net.HttpWebResponse response;     response = (System.Net.HttpWebResponse)request.GetResponse();     System.IO.Stream s;     s = response.GetResponseStream();     string StrDate = "";     string strValue = "";     StreamReader Reader = new StreamReader(s, Encoding.UTF8);     while ((StrDate = Reader.ReadLine()) != null)     {         strValue += StrDate + "/r/n";     }     return strValue; }
NSString *httpUrl = @"http://apis.baidu.com/apistore/idlocr/ocr"; NSString *httpArg = @"fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k="; [self request: httpUrl withHttpArg: httpArg];  -(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {     NSURL *url = [NSURL URLWithString: httpUrl];     NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];     [request setHTTPMethod: @"POST"];     [request addValue: @"您自己的apikey" forHTTPHeaderField: @"apikey"];     [request addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];     NSData *data = [HttpArg dataUsingEncoding: NSUTF8StringEncoding];     [request setHTTPBody: data];     [NSURLConnection sendAsynchronousRequest: request         queue: [NSOperationQueue mainQueue]         completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){             if (error) {                 NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);             } else {                 NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];                 NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];                 NSLog(@"HttpResponseCode:%ld", responseCode);                 NSLog(@"HttpResponseBody %@",responseString);             }         }]; }
var url = "http://apis.baidu.com/apistore/idlocr/ocr" var httpArg = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k=" request(url, httpArg: httpArg) func  request(httpUrl: String, httpArg: String) {     var req = NSMutableURLRequest(URL: NSURL(string: httpUrl)!)     req.timeoutInterval = 6     req.HTTPMethod = "POST"     req.addValue("您自己的apikey", forHTTPHeaderField: "apikey")     req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")     req.HTTPBody = NSString(string: httpArg).dataUsingEncoding(NSUTF8StringEncoding)     NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) {         (response, data, error) -> Void in         let res = response as! NSHTTPURLResponse         println(res.statusCode)         if let e = error{             println("请求失败")         }         if let d = data {             var content = NSString(data: d, encoding: NSUTF8StringEncoding)             println(content)         }     } }
JSON返回示例:
{  "errNum": "0",  "errMsg": "success",  "querySign": "3845925467,2370020290",  "retData": [   {    "rect": {     "left": "0",     "top": "0",     "width": "33",     "height": "31"    },    "word": "  8"   }  ] } errNum   : 标识处理是否成功, 0: 表示成功, 其它值, 表示失败 errMsg   : 错误类型说明 querySign: 本次请求用户传递原图或rl的唯一标示, 方便定位问题 retData  : 返回内容集合 rect  :  {left, right, width, height}该行文字所在的矩形区域坐标: left, right, width, height word  : 该行所识别出的文字 
正文到此结束
Loading...