百度OCR文字识别的小例子

首先说下,执行结果很令人不满意。。。


下面是官网的API说明

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

请求方法 :POST

请求参数(header) :

参数名

类型

必填

参数位置

描述

默认值

apikey

string

header

API密钥

您自己的apikey


请求参数(bodyParam) :

参数名

类型

必填

参数位置

描述

默认值

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编码后的字串,然后需要经过urlencode处理(特别重要);2.图片原文件

1

image

string

bodyParam

图片资源,目前仅支持jpg格式,原始



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

import sun.misc.BASE64Encoder;


public class Request {

	public static void main(String[] args){
		String httpUrl = "http://apis.baidu.com/apistore/idlocr/ocr";
		String imagePath="图片的路径";
		String str=encodeImgageToBase64(imagePath);
		str = str.replace("\r\n", "");
		str= URLEncoder.encode(str, "utf-8");//很重要的,刚开始就是因为没有加,所以怎么看结果怎么不合理
		String httpArg = "fromdevice=pc&clientip=172.0.0.1&detecttype=LocateRecognize&"+
		"languagetype=CHN_ENG&imagetype=1"+
		"&image="+str;
		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;
	}
	
	
	
	/** 
	 * 将本地图片进行Base64位编码 
	 *  
	 * @param imgUrl 
	 *            图片的url路径,如d:\\中文.jpg 
	 * @return 
	 */  
	public static String encodeImgageToBase64(String imagePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
	    // 其进行Base64编码处理  
	    byte[] data = null;  
	    // 读取图片字节数组  
	    try {  
	    	File imageFile = new File(imagePath);
	        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编码过的字节数组字符串  
	} 
	

}


你可能感兴趣的:(学习笔记,百度OCR,java)