JAVA實現百度OCR文字識別功能

閒來無事,發現百度有一個OCR文字識別接口,感覺挺有意思的,拿來研究一下。       

百度服務簡介:文字識別是百度自然場景OCR服務,依託百度業界領先的OCR算法,提供了整圖文字檢測、識別、整圖文字識別、整圖文字行定位和單字圖像識別等功能。

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

package com.oa.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.oa.commons.util.BASE64;

public class OCRTest {

	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;
	}

	<pre name="code" class="java">/**
	 * @param args
	 */
	public static void main(String[] args) {
		File file = new File("d:\\che4.jpg");
		String imageBase = BASE64.encodeImgageToBase64(file);
		imageBase = imageBase.replaceAll("\r\n","");
		imageBase = imageBase.replaceAll("\\+","%2B");
		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="+imageBase;
		String jsonResult = request(httpUrl, httpArg);
		System.out.println("返回的結果--------->"+jsonResult);

	}


	/**
	 * 將本地圖片進行Base64位編碼
	 * 
	 * @param imgUrl
	 *            圖片的url路徑,如d:\\中文.jpg
	 * @return
	 */
	public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
		// 其進行Base64編碼處理
		byte[] data = null;
		// 讀取圖片字節數組
		try {
			InputStream in = new FileInputStream(imageFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 對字節數組Base64編碼
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);// 返回Base64編碼過的字節數組字符串
	}
附件:(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/)會得到你想要的結果:

{
    "errNum": "0",
    "errMsg": "success",
    "querySign": "2289891521,4081625058",
    "retData": [
        {
            "rect": {
                "left": "32",
                "top": "15",
                "width": "418",
                "height": "118"
            },
            "word": "豫C88888"
        },
        {
            "rect": {
                "left": "45",
                "top": "137",
                "width": "373",
                "height": "18"
            },
            "word": "東風本田洛陽鍵通店電話:03796358222"
        }
    ]
}
怎麼樣,感覺很神奇吧,感興趣的試一下吧!吐舌頭
最後,解釋一下幾個參數的含義:

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


fromdevice:來源,例如:android、iPhone 默認是PC


clientip:客戶端出口IP


detecttype:OCR接口類型


languagetype:要檢測的文字類型


imagetype:圖片資源類型


image:圖片資源,目前僅支持jpg格式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章