java调用百度的文字识别接口

package com.*.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baidu.aip.ocr.AipOcr;

@Controller
public class OcrController {
	
	public net.sf.json.JSONObject OcrCheck()throws Exception {
		ResponseResult result = new ResponseResult();
		HashMap options = new HashMap();
		Map resultMap=new HashMap();
		net.sf.json.JSONObject jsonObj = new net.sf.json.JSONObject();
		try {
			//初始化客户端
			AipOcr client=authService.Ocrinit();
			
			//参数为图片外链
			String image ="c:\\Users\\admin\\Desktop\\图片\\4.jpg";
//			String image = request.getParameter("img");
			------------进行文字识别 -------------
			options.put("language_type", "CHN_ENG");
			//高清识别每日有时间限制
			JSONObject res = client.basicAccurateGeneral(image, options);
			//通用识别
			//JSONObject res = client.basicGeneral(image, options);
			JSONArray jsonArray=res.getJSONArray("words_result");
			if(jsonArray.length()>0){
			  	for(int i=0;i>>>>>>>>>>"+resultMap);
			  }
			jsonObj.put("result",resultMap);
			jsonObj.put("status","0");
			System.out.println(jsonObj);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("status","1");
			jsonObj.put("msg","系统异常,请稍后再试");
		}
		return jsonObj;
	}
	
	public static void main(String[] args) throws Exception {
		net.sf.json.JSONObject json = new OcrController().OcrCheck();
		System.out.println(json);
	}
	
}

说明:要引入百度的图像识别的jar包,maven依赖:

                
   com.baidu.aip
   java-sdk
   4.2.0

 
  

你可能感兴趣的:(java)