JAVA实现百度OCR文字识别功能

本文介绍并演示了如何使用百度的文字识别OCR服务。通过Java代码示例展示了如何将图片转换为Base64编码并发送到百度OCR接口进行文字识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/javagirlone/article/details/47807963

闲来无事,发现百度有一个OCR文字识别接口,感觉挺有意思的,拿来研究一下。       

百度服务简介:文字识别是百度自然场景OCR服务,依托百度业界领先的OCR算法,提供了整图文字检测、识别、整图文字识别、整图文字行定位和单字图像识别等功能。

不多说啦,直接看demo吧!偷笑

[java]  view plain  copy
 print ?
  1. package com.oa.test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9.   
  10. import com.oa.commons.util.BASE64;  
  11.   
  12. public class OCRTest {  
  13.   
  14.     public static String request(String httpUrl, String httpArg) {  
  15.         BufferedReader reader = null;  
  16.         String result = null;  
  17.         StringBuffer sbf = new StringBuffer();  
  18.   
  19.         try {  
  20.             URL url = new URL(httpUrl);  
  21.             HttpURLConnection connection = (HttpURLConnection) url  
  22.                     .openConnection();  
  23.             connection.setRequestMethod("POST");  
  24.             connection.setRequestProperty("Content-Type",  
  25.                     "application/x-www-form-urlencoded");  
  26.             // 填入apikey到HTTP header  
  27.             connection.setRequestProperty("apikey""您自己的apikey");  
  28.             connection.setDoOutput(true);  
  29.             connection.getOutputStream().write(httpArg.getBytes("UTF-8"));  
  30.             connection.connect();  
  31.             InputStream is = connection.getInputStream();  
  32.             reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));  
  33.             String strRead = null;  
  34.             while ((strRead = reader.readLine()) != null) {  
  35.                 sbf.append(strRead);  
  36.                 sbf.append("\r\n");  
  37.             }  
  38.             reader.close();  
  39.             result = sbf.toString();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         return result;  
  44.     }  
  45.   
  46.     <pre name="code" class="java">/** 
  47.      * @param args 
  48.      */  
  49.     public static void main(String[] args) {  
  50.         File file = new File("d:\\che4.jpg");  
  51.         String imageBase = BASE64.encodeImgageToBase64(file);  
  52.         imageBase = imageBase.replaceAll("\r\n","");  
  53.         imageBase = imageBase.replaceAll("\\+","%2B");  
  54.         String httpUrl = "http://apis.baidu.com/apistore/idlocr/ocr";  
  55.         String httpArg = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image="+imageBase;  
  56.         String jsonResult = request(httpUrl, httpArg);  
  57.         System.out.println("返回的结果--------->"+jsonResult);  
  58.   
  59.     }  

 
 
[java]  view plain  copy
 print ?
  1. /** 
  2.  * 将本地图片进行Base64位编码 
  3.  *  
  4.  * @param imgUrl 
  5.  *            图片的url路径,如d:\\中文.jpg 
  6.  * @return 
  7.  */  
  8. public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
  9.     // 其进行Base64编码处理  
  10.     byte[] data = null;  
  11.     // 读取图片字节数组  
  12.     try {  
  13.         InputStream in = new FileInputStream(imageFile);  
  14.         data = new byte[in.available()];  
  15.         in.read(data);  
  16.         in.close();  
  17.     } catch (IOException e) {  
  18.         e.printStackTrace();  
  19.     }  
  20.   
  21.     // 对字节数组Base64编码  
  22.     BASE64Encoder encoder = new BASE64Encoder();  
  23.     return encoder.encode(data);// 返回Base64编码过的字节数组字符串  
  24. }  
附件: (che4.jpg)

运行后结果:

{"errNum":"0","errMsg":"success","querySign":"2289891521,4081625058","retData":[{"rect":{"left":"32","top":"15","width":"418","height":"118"},"word":"\u8c6bC88888"},{"rect":{"left":"45","top":"137","width":"373","height":"18"},"word":"\u4e1c\u98ce\u672c\u7530\u6d1b\u9633\u952e\u901a\u5e97\u7535\u8bdd\uff1a03796358222"}]}

注意:将此结果放到 在线JSON校验格式化工具中(http://www.bejson.com/)会得到你想要的结果:

[plain]  view plain  copy
 print ?
  1. {  
  2.     "errNum": "0",  
  3.     "errMsg": "success",  
  4.     "querySign": "2289891521,4081625058",  
  5.     "retData": [  
  6.         {  
  7.             "rect": {  
  8.                 "left": "32",  
  9.                 "top": "15",  
  10.                 "width": "418",  
  11.                 "height": "118"  
  12.             },  
  13.             "word": "豫C88888"  
  14.         },  
  15.         {  
  16.             "rect": {  
  17.                 "left": "45",  
  18.                 "top": "137",  
  19.                 "width": "373",  
  20.                 "height": "18"  
  21.             },  
  22.             "word": "东风本田洛阳键通店电话:03796358222"  
  23.         }  
  24.     ]  
  25. }  
怎么样,感觉很神奇吧,感兴趣的试一下吧!吐舌头
最后,解释一下几个参数的含义:

apikey:API密钥 也就是您自己的apikey


fromdevice:来源,例如:Android、iPhone 默认是PC


clientip:客户端出口IP


detecttype:OCR接口类型


languagetype:要检测的文字类型


imagetype:图片资源类型


image:图片资源,目前仅支持jpg格式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值