【百度OCR 封装篇】SDK调用方式,API调用方式,自定义模版,表格文字识别

全部代码下载地址:https://download.csdn.net/download/qq_33333654/11914449

SDK调用所需单例:

package com.jeeplus.ocr;

import com.baidu.aip.ocr.AipOcr;

/**
 * @ProjectName: 百度ocr单例 目前是iocr,做文字识别
 * @Package: com.jeeplus.ocr
 * @ClassName: AipOcrSingleton
 * @Author: MC
 * @Description: ${description}
 * @Date: 2019/10/23 0023 10:20
 * @Version: 1.0
 */
public class AipOcrSingleton {
    public static volatile AipOcr aipOcr = null;
    //设置APPID/AK/SK
    public static final String APP_ID = "17593750";
    public static final String API_KEY = "VExogNuAiDslahMNe2uRn5IB";
    public static final String SECRET_KEY = "zILi6zsRwgKa1dTmbv2Rw8uG1oPGyI9A";
    public static final int CONNECTION_TIME_OUT = 2000;//建立连接的超时时间(单位:毫秒)
    public static final int SOCKET_TIME_OUT = 6000;//通过打开的连接传输数据的超时时间(单位:毫秒)
    private AipOcrSingleton(){}

    public static AipOcr getAipOcr(){
        if(aipOcr == null){
            synchronized (AipOcrSingleton.class){
                if(aipOcr == null){
                    aipOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
                    // 可选:设置网络连接参数
                    aipOcr.setConnectionTimeoutInMillis(CONNECTION_TIME_OUT);
                    aipOcr.setSocketTimeoutInMillis(SOCKET_TIME_OUT);
                    //setHttpProxy 设置http代理服务器
                    //setSocketProxy 设置socket代理服务器 (http和socket类型代理服务器只能二选一)
                }
            }
        }
        return aipOcr;
    }
}

 API调用所需httputil

public static String ocrPost(String requestUrl, String accessToken, String params)
        throws Exception {
    String contentType = "application/x-www-form-urlencoded";
    return HttpUtil.ocrPost(requestUrl, accessToken, contentType, params);
}

public static String ocrPost(String requestUrl, String accessToken, String contentType, String params)
        throws Exception {
    String encoding = "UTF-8";
    if (requestUrl.contains("nlp")) {
        encoding = "GBK";
    }
    return HttpUtil.ocrPost(requestUrl, accessToken, contentType, params, encoding);
}

public static String ocrPost(String requestUrl, String accessToken, String contentType, String params, String encoding)
        throws Exception {
    String url = requestUrl + "?access_token=" + accessToken;
    return HttpUtil.ocrPostGeneralUrl(url, contentType, params, encoding);
}

public static String ocrPostGeneralUrl(String generalUrl, String contentType, String params, String encoding)
        throws Exception {
    URL url = new URL(generalUrl);
    // 打开和URL之间的连接
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    // 设置通用的请求属性
    connection.setRequestProperty("Content-Type", contentType);
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setDoInput(true);

    // 得到请求的输出流对象
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    out.write(params.getBytes(encoding));
    out.flush();
    out.close();

    // 建立实际的连接
    connection.connect();
    // 获取所有响应头字段
    Map<String, List<String>> headers = connection.getHeaderFields();
    // 遍历所有的响应头字段
    for (String key : headers.keySet()) {
        System.err.println(key + "--->" + headers.get(key));
    }
    // 定义 BufferedReader输入流来读取URL的响应
    BufferedReader in = null;
    in = new BufferedReader(
            new InputStreamReader(connection.getInputStream(), encoding));
    String result = "";
    String getLine;
    while ((getLine = in.readLine()) != null) {
        result += getLine;
    }
    in.close();
    System.err.println("result:" + result);
    return result;
}

封装大全:

AipOcrUtil
package com.jeeplus.ocr;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Base64Util;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.HttpUtil;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.common.Constants;
import com.jeeplus.ocr.bean.AccessTokenResultBean;
import com.jeeplus.ocr.bean.AipOcrBasicGeneralResultBean;
import com.jeeplus.ocr.bean.AipOcrCustomResultBean;
import com.jeeplus.ocr.bean.AipOcrExcleRequestResultBean;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

/**
 * @ProjectName: Hematopathy
 * @Package: com.jeeplus.ocr
 * @ClassName: AipOcrUtil
 * @Author: MC
 * @Description: ${description}
 * @Date: 2019/10/23 0023 10:48
 * @Version: 1.0
 */
public class AipOcrUtil {

    /**
     * @Method 通用文字识别
     * @Author MC
     path:本地文件路径
    options:请求API参数,可为空
     * @Return
     * @Date 2019/10/23 0023 11:36
     */
    public static AipOcrBasicGeneralResultBean basicGeneralByPath(String path, HashMap<String, String> options){
        if(checkFileIsImg(path)){
            if(options == null || options.isEmpty()){
                options = new HashMap<String, String>();
            }
            options.put("language_type", "CHN_ENG");
            options.put("detect_direction", "true");
            options.put("detect_language", "true");
            options.put("probability", "true");
            AipOcr client = AipOcrSingleton.getAipOcr();
            JSONObject res = client.basicGeneral(path, options);
            String resStr = res.toString();
            AipOcrBasicGeneralResultBean bean = JSON.parseObject(resStr, AipOcrBasicGeneralResultBean.class);
            return bean;
        }
        return null;
    }

    /**
     * @Method 通用文字识别
     * @Author MC
    url:网络图片地址
    options:请求API参数,可为空
     * @Return
     * @Date 2019/10/23 0023 11:36
     */
    public static AipOcrBasicGeneralResultBean basicGeneralByURL(String url, HashMap<String, String> options){
        if(!StringUtils.isEmpty(url)){
            if(options == null || options.isEmpty()){
                options = new HashMap<String, String>();
            }
            options.put("language_type", "CHN_ENG");
            options.put("detect_direction", "true");
            options.put("detect_language", "true");
            options.put("probability", "true");
            AipOcr client = AipOcrSingleton.getAipOcr();
            JSONObject res = client.basicGeneralUrl(url, options);
            String resStr = res.toString();
            AipOcrBasicGeneralResultBean bean = JSON.parseObject(resStr, AipOcrBasicGeneralResultBean.class);
            return bean;
        }
        return null;
    }


    /**
     * @Method 根据模版ID或者分类器ID识别本地图片
     * @Author MC
    path:本地图片路径
    templateSign:指定模版ID
    classifierId:指定分类器ID
    模版ID与分类ID至少一个不为空
     * @Return
     * @Date 2019/10/23 0023 13:59
     */
    public static AipOcrCustomResultBean customByPath(String path,String templateSign,String classifierId){
        if(checkFileIsImg(path)){
            if(StringUtils.isEmpty(templateSign) && StringUtils.isEmpty(classifierId)){
                return null;
            }
            HashMap<String, String> options = new HashMap<String, String>();
            if(!StringUtils.isEmpty(templateSign)){
                options.put("templateSign", templateSign);
            }
            if(!StringUtils.isEmpty(classifierId)){
                options.put("classifierId", classifierId);
            }
            AipOcr client = AipOcrSingleton.getAipOcr();
            JSONObject res = client.custom(path, options);
            String resStr = res.toString();
            AipOcrCustomResultBean bean = JSON.parseObject(resStr, AipOcrCustomResultBean.class);
            return bean;
        }
        return null;
    }

    /**
     * @Method 使用API调用方式进行自定义模版或者分类器的文字识别
     * @Author MC
     ak:app_key 必传
    sk:secret_key 必传
    filePath:本地文件路径 要求为图片格式 必传
    templateSign:自定义模版ID
    classifierParams:自定义分类器ID
    templateSign和classifierParams至少传递一个
     * @Return
     * @Date 2019/10/24 0024 11:34
     */
    public static AipOcrCustomResultBean customByApiUrl(String ak, String sk,String filePath,String templateSign,String classifierParams) throws Exception {
        if(StringUtils.isEmpty(ak) || StringUtils.isEmpty(sk) || StringUtils.isEmpty(filePath)){
            return null;
        }
        if(StringUtils.isEmpty(templateSign) && StringUtils.isEmpty(classifierParams)){
            return null;
        }
        if(checkFileIsImg(filePath)){
            String imgStr = imgFile2Bytes2Encode(filePath);
            String param = "";
            if(!StringUtils.isEmpty(classifierParams)){
                param = "classifierId="+classifierParams+"&image=" + URLEncoder.encode(imgStr, "UTF-8");
            }else{
                param = "templateSign="+templateSign+"&image=" + URLEncoder.encode(imgStr, "UTF-8");
            }
            AccessTokenResultBean auth = getAuth(ak, sk);
            if(auth == null || !StringUtils.isEmpty(auth.getError())){
                return null;
            }

            String accessToken = auth.getAccess_token();
            // 请求模板识别
            String result = HttpUtil.ocrPost(Constants.OCR_API_URL_GET_RECOGNISE, accessToken, param);
            AipOcrCustomResultBean bean = JSON.parseObject(result, AipOcrCustomResultBean.class);
            return bean;
        }
        return null;
    }

    /**
     * @Method 同步表格文字识别,获取excle下载地址
     * @Author MC

     * @Return
     * @Date 2019/10/24 0024 14:38
     */
    public static AipOcrExcleRequestResultBean ocrExcle(String filePath,String accessToken) throws Exception {
        String s = ocrExcle(filePath, true, "", accessToken);
        return JSON.parseObject(s, AipOcrExcleRequestResultBean.class);
    }

    /**
     * @Method ocr表格识别
     * @Author MC
    filePath:要识别的图片地址 必传
    isSync:是否同步识别 默认false
    requestType:传递json或者excel或者空 默认使用excel
    传递json时返回的是解析结果的json字符串,传递excel或者空时返回的是识别结果的下载地址
    accessToken:token必传
     * @Return
     * @Date 2019/10/24 0024 13:49
     */
    public static String ocrExcle (String filePath,boolean isSync,String requestType,String accessToken) throws Exception {
        if(StringUtils.isEmpty(accessToken)){
            return null;
        }
        if(checkFileIsImg(filePath)){
            if(!StringUtils.isEmpty(requestType) && !"excel".equals(requestType)&& !"json".equals(requestType)){
                return null;
            }
            if(isSync){//同步
                String params;
                if(StringUtils.isEmpty(requestType) || "excel".equals(requestType)){
                    params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(filePath, "UTF-8")
                            +"&is_sync=true";
                }else{
                    params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(filePath, "UTF-8")
                            +"&is_sync=true&request_type=json";
                }
                return HttpUtil.ocrPost(Constants.OCR_API_URL_GET_EXCL, accessToken, params);
            }else{//异步
                String requestId ="";
                if(StringUtils.isEmpty(requestType) || "excel".equals(requestType)){//异步 excle下载
                    requestId = getOcrExcleRequestId(filePath, accessToken,"");
                }else{
                    String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(filePath, "UTF-8")
                            +"&is_sync=false&request_type=json";
                    requestId = getOcrExcleRequestId(filePath, accessToken,params);
                }
                if(!StringUtils.isEmpty(requestId)){
                    String reslut = "";
                    do{
                        reslut = getExclResultUrl(requestId, accessToken);
                    }while (!StringUtils.isEmpty(reslut));
                    return reslut;
                }
            }
        }
        return null;
    }


    /**
     * @Method 获取OCR表格文字识别的requestId
     * @Author MC
    filePath:要识别的图片路径
    accessToken:token
    注意:默认使用异步获取,以excle识别结果下载excle的方式
     * @Return
     * @Date 2019/10/24 0024 13:32
     */
    public static String getOcrExcleRequestId(String filePath,String accessToken,String param) throws Exception {
        String params;
        if(!StringUtils.isEmpty(param)){
            params = param;
        }else{
            String imgStr = imgFile2Bytes2Encode(filePath);
            params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8");
        }
        String result = HttpUtil.ocrPost(Constants.OCR_API_URL_GET_EXCL, accessToken, params);
        //结果例:{"result":[{"request_id":"17593750_1197891"}],"log_id":1571894645836523}
        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(result);
        List<com.alibaba.fastjson.JSONObject> list = (List<com.alibaba.fastjson.JSONObject>) jsonObject.get("result");
        com.alibaba.fastjson.JSONObject jsonObject1 = list.get(0);
        String request_id = jsonObject1.getString("request_id");
        return request_id;
    }

    /**
     * @Method 获取表格文字识别结果的excle下载地址
     * @Author MC
    request_id:表格文字识别接口返回的ID
    accessToken:token
     * @Return
     * @Date 2019/10/24 0024 13:03
     */
    public static String getExclResultUrl(String request_id,String accessToken) throws Exception {
        String params = URLEncoder.encode("request_id", "UTF-8") + "=" + URLEncoder.encode(request_id, "UTF-8");
        String result = HttpUtil.ocrPost(Constants.OCR_API_URL_GET_EXCL_RESULT, accessToken, params);
        AipOcrExcleRequestResultBean aipOcrExcleRequestResultBean = JSON.parseObject(result, AipOcrExcleRequestResultBean.class);
        String ret_code = aipOcrExcleRequestResultBean.getResult().getRet_code();
        if("3".equals(ret_code)){
            return aipOcrExcleRequestResultBean.getResult().getResult_data();
        }
        return "";
    }

    /**
     * 获取API访问token
     * 该token有一定的有效期,需要自行管理,当失效时需重新获取.
     * @param ak - 百度云官网获取的 API Key
     * @param sk - 百度云官网获取的 Securet Key
     * @return assess_token 示例:
     * "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
     */
    public static AccessTokenResultBean getAuth(String ak, String sk) {
        AccessTokenResultBean bean = (AccessTokenResultBean) CacheUtils.get(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN);
        if(bean != null){
            //检查缓存中的token是否过期
            long expires_in = bean.getMyTimeOut().getTime();
            Date nowDate = new Date();
            long nowDateTime = nowDate.getTime();
            if(expires_in > nowDateTime){
                return bean;
            }else{
                CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN);
            }
        }

        // 获取token地址
        String authHost = Constants.OCR_API_URL_GET_ACCESS_TOKEN + "?";
        String getAccessTokenUrl = authHost
                // 1. grant_type为固定参数
                + "grant_type=client_credentials"
                // 2. 官网获取的 API Key
                + "&client_id=" + ak
                // 3. 官网获取的 Secret Key
                + "&client_secret=" + sk;
        try {
            URL realUrl = new URL(getAccessTokenUrl);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
//            for (String key : map.keySet()) {
//                System.err.println(key + "--->" + map.get(key));
//            }
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            /**
             * 返回结果示例
             */
            System.err.println("result:" + result);
            AccessTokenResultBean accessTokenResultBean = JSON.parseObject(result, AccessTokenResultBean.class);
            if(StringUtils.isEmpty(accessTokenResultBean.getError())){//获取成功 进行缓存
                Integer expires_in = accessTokenResultBean.getExpires_in();
                if(expires_in > 0){
                    int days = expires_in / ( 60 * 60 * 24);
                    Date date=new Date();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE,days);//把日期往后增加一天.整数往后推,负数往前移动
                    date=calendar.getTime();
                    accessTokenResultBean.setMyTimeOut(date);//设置有效期
                }else{
                    CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN);
                    return null;
                }
                CacheUtils.put(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN,accessTokenResultBean);
            }else{
                CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN);
            }
            return accessTokenResultBean;
           /* JSONObject jsonObject = new JSONObject(result);
            String access_token = jsonObject.getString("access_token");
            return access_token;*/
        } catch (Exception e) {
            System.err.printf("获取token失败!");
            CacheUtils.remove(Constants.OCR_CACHE_NAME,Constants.OCR_CACHE_ACCESS_TOKEN);
            e.printStackTrace(System.err);
        }
        return null;
    }

    private static boolean checkFileIsImg(String path){
        if(StringUtils.isEmpty(path)){
            return false;
        }
        File srcFile = new File(path);
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            return false;
        }
        if(path.indexOf(".") == -1){
            return false;
        }
        String suffix= path.substring(path.indexOf("."), path.length());
        if(
                suffix.equalsIgnoreCase("jpg") ||
                        suffix.equalsIgnoreCase("PNG ") ||
                        suffix.equalsIgnoreCase("GIF") ||
                        suffix.equalsIgnoreCase("tif") ||
                        suffix.equalsIgnoreCase("bmp")
        ){
            return false;
        }

        return true;
    }


    /**
     * @Method 图片文件转换为字节在进行base64转码
     * @Author MC

     * @Return
     * @Date 2019/10/24 0024 10:19
     */
    public static String imgFile2Bytes2Encode(String filePath) throws IOException {
        if(!checkFileIsImg(filePath)){
            return null;
        }
        byte[] imgData = readFileByBytes(filePath);
        String imgStr = Base64Util.encode(imgData);
        return imgStr;
    }

    /**
     * @Method 文件转换为字节在进行base64转码
     * @Author MC

     * @Return
     * @Date 2019/10/24 0024 10:19
     */
    public static String file2Bytes2Encode(String filePath) throws IOException {
        if(StringUtils.isEmpty(filePath)){
            return null;
        }
        File srcFile = new File(filePath);
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            return null;
        }
        byte[] imgData = readFileByBytes(filePath);
        String imgStr = Base64Util.encode(imgData);
        return imgStr;
    }

    /**
     * 根据文件路径读取byte[] 数组
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }




}

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不能吃辣的JAVA程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值