使用百度API识别图片文字

1、注册百度账号  https://login.bce.baidu.com/

2、定位到 产品服务 / 文字识别 - 概览 页面

3、创建一个应用

4、下载相关的SDK包,在工程项目中引用。如果是maven工程,直接在pom.xml文件中添加依赖包

        <dependency>

                <groupId>com.baidu.aip</groupId>

                <artifactId>java-sdk</artifactId>

                <version>4.6.1</version>

        </dependency>

 

5、编写测试代码

public class BaiduAPITest {
    public static final String APP_ID = "你的应用的 AppID";
    public static final String API_KEY = "你的应用的 API Key";
    public static final String SECRET_KEY = "你的应用的 Secret Key";
	
    public static void main(String[] args) {
    	String imagePath = "doc\\pic2.jpg";
    	System.out.println(generalString(imagePath, true));
	}
    
    /**
     * 识别图片上的文本内容,转成文字字符串返回
     * @param imagePath 图片文件的路径
     */
    private static String generalString(String imagePath, boolean isNewline){
    	try{
    		HashMap<String, String> options = new HashMap<String, String>();
            options.put("language_type", "CHN_ENG"); //CHN_ENG:中英文混合, ENG:英文
            options.put("detect_direction", "true"); //是否检测图像朝向,默认不检测,即:false
            options.put("detect_language", "true"); //是否检测语言,默认不检测。
            options.put("probability", "false"); //是否返回识别结果中每一行的置信度
            
            //通用文字识别
        	JSONObject jsonObject = getAipOcr().basicGeneral(imagePath, options); 
        	String result = mergeString(jsonObject, isNewline);
        	return result;
        }catch(Exception ex){
        	ex.printStackTrace();
        }
    	
    	return "";
    }
    
    /**
     * AipOcr最好单例模式使用
     */
    private static AipOcr getAipOcr(){
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }
    
    /**
     * 从返回的JSONObject对象中取出需要的文字内容,组装成一个大文本内容
     * @param jsonObject JSONObject对象
     * @param isNewline 每个识别结果字符串的后面是否增加换行符
     */
    private static String mergeString(JSONObject jsonObject, boolean isNewline){
    	if(jsonObject == null){
    		return "";
    	}
    	
    	if(jsonObject.has("words_result") && jsonObject.has("words_result_num")){
    		int wordsResultNum = jsonObject.getInt("words_result_num");
    		if(wordsResultNum > 0){
    			StringBuilder sb = new StringBuilder();
    			
    			JSONArray jsonArray = jsonObject.getJSONArray("words_result");
    			int len = jsonArray.length();
    			for(Iterator<Object> it=jsonArray.iterator(); it.hasNext();){
    				JSONObject obj = (JSONObject)it.next();
    				if(isNewline){
    					sb.append(obj.get("words") + "\n");
    				}else{
    					sb.append(obj.get("words"));
    				}
    			}
    			
    			return sb.toString();
    		}
    	}else{
    		return jsonObject.toString();
    	}
    	
    	return null;
    }
}

 

6、准备一张jpg图片文件



 

7、执行测试代码,查看效果

//接口返回的内容
{
    "words_result": [
        {"words": "命令:yum- y install libpng12"},
        {"words": "做完这些我们就可以直接使用Tess4进行图片识别了,目前我只试过字母和数字的"},
        {"words": "子,有点渣,请诸位大神不要吐槽,是直接传入图片ur地址解析的"},
        {"words": "public static String imagetotel(String imgurl)"},
        {"words": "Itesseract instance=new Tesseracto);"},
        {"words": "URL url=new URL(imgurl);"}
    ],
    "direction": 0,
    "words_result_num": 6,
    "language": 3,
    "log_id": 5228016013525318579
}

//处理后的内容
命令:yum- y install libpng12
做完这些我们就可以直接使用Tess4进行图片识别了,目前我只试过字母和数字的
子,有点渣,请诸位大神不要吐槽,是直接传入图片ur地址解析的
public static String imagetotel(String imgurl)
Itesseract instance=new Tesseracto);
URL url=new URL(imgurl);

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值