阿里云OCR识别

ocr识别证件照;主要是使用阿里云j接口:

1、注册阿里云账户获取appKey \appSer

2、调用接口

2.1:定义常量

复制代码
public class OrcHttpConstant {

    /**
     *   驾驶证识别appCode
     */
    public static final String OCR_APPCODE = "033f4b0f719d4e01b431a18cba7a79e1";

    /**
     * 调用方式
     */
    public static final String MONTH = "POST";

    /**
    *   驾驶证识别url
    */
    public static final String DRIVER_LICENSE_HOST = "https://cardriving.market.alicloudapi.com";

    /**
    *   驾驶证识别path
    */
    public static final String DRIVER_LICENSE_PATH = "/rest/160601/ocr/ocr_driver_license.json";


    /**
     *   身份证识别url
     */
    public static final String ID_CARD_HOST = "https://cardnumber.market.alicloudapi.com";

    /**
     *   身份证识别path
     */
    public static final String ID_CARD_PATH = "/rest/160601/ocr/ocr_idcard.json";


    /**
     *   行驶证识别url
     */
    public static final String VEHICLE_LICENSE_HOST = "https://driving.market.alicloudapi.com";

    /**
     *   行驶证识别path
     */
    public static final String VEHICLE_LICENSE_PATH = "/rest/160601/ocr/ocr_vehicle.json";


    /**
     *   营业执照证识别url
     */
    public static final String BUSINESS_LICENSE_HOST = "https://bizlicense.market.alicloudapi.com";

    /**
     *   营业执照证识别path
     */
    public static final String BUSINESS_LICENSE_PATH = "/rest/160601/ocr/ocr_business_license.json";


    /**
     *   发票识别url
     */
    public static final String INVOICE_LICENSE_HOST = "https://dgfp.market.alicloudapi.com";

    /**
     *   发票识别path
     */
    public static final String INVOICE_LICENSE_PATH = "/ocrservice/invoice";

}

2.2 :OCR识别工具: 主要是按照type调用个子的方法

复制代码
/**
 * 阿里云OCR识别功能
 */
@Component
@Slf4j
public class AliOcrTools {




    /**
     * orc识别接口
     * @param appCode 识别code
     * @param imgFile 识别图片路径
     * @param isFace 正反面
     * @param type 类型
     * @return
     */
    public static JSONObject orcIdentify(String appCode,String imgFile,Boolean isFace,String type) {
        if(StringUtil.isEmpty(appCode)){
            appCode= OrcHttpConstant.OCR_APPCODE;
        }
        String method = OrcHttpConstant.MONTH;
        switch (type) {
            //驾驶证
            case "1":
                return getDriverLicenseInfo(appCode, imgFile, isFace,method);
            //行驶证
            case "2":
                return getVehicleInfo(appCode, imgFile,isFace,method);
            //营业执照
            case "3":
                return getBusinessLicenseInfo(appCode, imgFile,method);
            //身份证
            case "4":
                return getIdcardInfo(appCode, imgFile,method,isFace);
            //增值税发票
            case "5":
                return getCustomizeInfo(appCode,imgFile,method);
            default:
                return null;
        }
    }

    /**
     * 驾驶证识别
     * flag(true:正面;false反面)
     * @param imgUrl
     *
     */
    public static JSONObject getDriverLicenseInfo(String appcode, String imgUrl,Boolean flag,String method) {
        //String host = "https://dm-52.data.aliyun.com";
        String host = OrcHttpConstant.DRIVER_LICENSE_HOST;
        // String path = "/rest/160601/ocr/ocr_driver_license.json";
        String path = OrcHttpConstant.DRIVER_LICENSE_PATH;
        JSONObject configObj = new JSONObject();
        if(flag){
            configObj.put("side", "face");
        }else {
            configObj.put("side", "back");
        }
        String config_str = configObj.toString();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        // 对图像进行base64编码
        String imgBase64 = img_base64(imgUrl);
        //如果文档的输入中含有inputs字段,设置为True, 否则设置为False
        Boolean is_old_format = false;
        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        try {
            requestObj.put("image", imgBase64);
            if(config_str.length() > 0) {
                requestObj.put("configure", config_str);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String bodys = requestObj.toString();
        log.info("body:"+bodys);
        return dealReauest(host,path,method,headers,querys,bodys);
    }


    /**
     * 行驶证识别
     * flag(true:正面;false反面)
     * @param imgUrl(反面)
     */
    public static JSONObject getVehicleInfo(String appcode,String imgUrl,Boolean flag,String method) {
       /* String host = "https://dm-53.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_vehicle.json";*/
        String host = OrcHttpConstant.VEHICLE_LICENSE_HOST;
        String path = OrcHttpConstant.VEHICLE_LICENSE_PATH;
        JSONObject configObj = new JSONObject();
        if(flag){
            configObj.put("side", "face");
        }else {
            configObj.put("side", "back");
        }
        String config_str = configObj.toString();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        // 对图像进行base64编码
        String imgBase64 = getImgBase64(imgUrl);
        //如果文档的输入中含有inputs字段,设置为True, 否则设置为False
        Boolean is_old_format = false;
        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        try {
            if(is_old_format) {
                JSONObject obj = new JSONObject();
                obj.put("image", imgBase64);
                if(config_str.length() > 0) {
                    obj.put("configure", config_str);
                }
                JSONArray inputArray = new JSONArray();
                inputArray.add(obj);
                requestObj.put("inputs", inputArray);
            }else{
                requestObj.put("image", imgBase64);
                if(config_str.length() > 0) {
                    requestObj.put("configure", config_str);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String bodys = requestObj.toString();
        return dealReauest(host,path,method,headers,querys,bodys);
    }


    /**
     *  营业执照识别
     *  @param imgUrl
     *  @return
     */
    public static JSONObject getBusinessLicenseInfo(String appcode,String imgUrl,String method) {
       /* String host = "https://dm-58.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_business_license.json";*/
        String host = OrcHttpConstant.BUSINESS_LICENSE_HOST;
        String path = OrcHttpConstant.BUSINESS_LICENSE_PATH;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String imgBase64 = getImgBase64(imgUrl);
        if(imgBase64 == null) {
            return null;
        }
        //拼接请求体
        String bodys =  "{\"image\":\""+imgBase64+"\"}";
        return dealReauest(host,path,method,headers,querys,bodys);
    }


    /**
     * 身份证正反面识别
     * @param appcode 识别码,
     * @param imgUrl 需要识别的文件路径
     * @param method 调用方式
     * @param flag (true:正面;false反面)
     * @return
     */
    public static JSONObject getIdcardInfo(String appcode,String imgUrl,String method,Boolean flag) {
      /*  String host = "http://dm-51.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_idcard.json";*/
        String host = OrcHttpConstant.ID_CARD_HOST;
        String path = OrcHttpConstant.ID_CARD_PATH;
        JSONObject configObj = new JSONObject();
        if(flag){
            //脸部面
            configObj.put("side", "face");
        }else {
            //国徽
            configObj.put("side", "back");
        }
        String config_str = configObj.toString();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        // 对图像进行base64编码
        String imgBase64 = img_base64(imgUrl);
        //如果文档的输入中含有inputs字段,设置为True, 否则设置为False
        Boolean is_old_format = false;
        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        try {
            if(is_old_format) {
                JSONObject obj = new JSONObject();
                obj.put("image", imgBase64);
                if(config_str.length() > 0) {
                    obj.put("configure", config_str);
                }
                JSONArray inputArray = new JSONArray();
                inputArray.add(obj);
                requestObj.put("inputs", inputArray);
            }else{
                requestObj.put("image", imgBase64);
                if(config_str.length() > 0) {
                    requestObj.put("configure", config_str);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String bodys = requestObj.toString();
        return dealReauest(host,path,method,headers,querys,bodys);
    }


    /**
     *
     *
     * @param: [host, path, method, appCode, imgFile]
     * @param: [调用url, 调用路径, 调用方式, 识别码, 需要识别的文件路径]
     * @return: dadao.base.String
     * @author: Mr luo
     * @date: 2023/7/13 15:01
     */
    /**
     * 发票识别
     * @param appCode 识别码,
     * @param imgFile 需要识别的文件路径
     * @param method 调用方式
     * @return
     */
    private static JSONObject getCustomizeInfo(String appCode,String imgFile,String method) {
        String host = OrcHttpConstant.INVOICE_LICENSE_HOST;
        String path = OrcHttpConstant.INVOICE_LICENSE_PATH;
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appCode);
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        String imgBase64 = img_base64(imgFile);
        if (imgBase64 == null) {
            return null;
        }
        String bodys = "{\"img\":\"" + imgBase64 + "\",\"url\":\"\"}";
        return dealReauest(host,path,method,headers,querys,bodys);
    }

    /**
     * 根据图片路径,获取图片的Base64编码(按照网络图片)
     * @param
     * @return
     */
    public static String getImgBase64(String imgUrl)  {
        URL url = null;
        try {
            log.info("imgUrl路径:"+ imgUrl);
            url = new URL(imgUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        //打开链接
        HttpURLConnection conn = null;
        InputStream inStream = null;
        String base64 = "";
        try {
            conn = (HttpURLConnection) url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //超时响应时间为5秒
            conn.setConnectTimeout(5 * 1000);
            //通过输入流获取图片数据
            inStream = conn.getInputStream();
            //得到图片的二进制数据,以二进制封装得到数据,具有通用性
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            //创建一个Buffer字符串
            byte[] buffer = new byte[1024];
            //每次读取的字符串长度,如果为-1,代表全部读取完毕
            int len = 0;
            //使用一个输入流从buffer里把数据读取出来
            while ((len = inStream.read(buffer)) != -1) {
                //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                outStream.write(buffer, 0, len);
            }
            byte[] data = outStream.toByteArray();
            //对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            base64 = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭输入流
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return base64;
        }
    }

    public static String img_base64(String path) {
        /**
         *  对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
         */
        String imgBase64="";
        if (path.startsWith("http")){
            log.info("imgUrl网络地址路径:"+ path);
            imgBase64= getImgBase64(path);
        }else {
            try {
                File file = new File(path);
                byte[] content = new byte[(int) file.length()];
                FileInputStream finputstream = new FileInputStream(file);
                finputstream.read(content);
                finputstream.close();
                imgBase64 = new String(Base64.encodeBase64(content));
            } catch (IOException e) {
                e.printStackTrace();
                return imgBase64;
            }
        }

        return imgBase64;
    }

    /**
     * 识别方法 向阿里云发送请求
     * @param host 调用url
     * @param path 调用路径
     * @param method 调用方式
     * @param headers 请求头
     * @param querys 查询条件
     * @param bodys 参数体
     * @return
     */
    private static JSONObject dealReauest( String host,String path,String method, Map<String, String> headers, Map<String, String> querys,String bodys){
        JSONObject jsonObject = new JSONObject();
        try {
            HttpResponse response = AliHttpUtils.doPost(host, path, method, headers, querys, bodys);
            log.info("返回结果response:"+response.toString());
            int stat = response.getStatusLine().getStatusCode();
            if(stat != 200){
                return null;
            }
            String res = EntityUtils.toString(response.getEntity());
            log.info("识别结果:"+res);
            jsonObject = JSON.parseObject(res);
            jsonObject.put("success",1);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            return jsonObject;
        }
    }
相关推荐
~烈14 小时前
Umi-OCR批量图片扫描PDF文字识别实操教程
ocr·paddleocr·ocr图片识别
明哥聊AI1 天前
开源发布|ZhDocParser:不是 OCR,而是面向 RAG 与 Agent 的中文复杂文档结构化解析工具
开源·ocr
AI人工智能+2 天前
护照OCR识别技术,依托深度神经网络模型,实现了从像素到语义的端到端智能解析
深度学习·计算机视觉·自然语言处理·ocr·护照ocr识别
石榴树下的七彩鱼4 天前
出租车票/行程单OCR识别API实战:差旅报销自动化,毫秒提取行程信息(附Python/Java代码)
python·ocr·财务自动化·api接入·石榴智能·出租车票ocr·行程单ocr
医疗信息化王工5 天前
从零到一:基于 GGUF 格式部署 Unlimited-OCR 搭建企业级证件识别服务
图像处理·c#·ocr
Lyn_Li5 天前
扫描 PDF 歪了怎么办?用 6 种检测方法做本地批量扶正(附开源工具)
python·pdf·ocr·tesseract·开源工具·文档处理·本地处理·扫描件纠偏
余俊晖6 天前
多模态文档解析开源新进展:Unlimited OCR技术方案
人工智能·ocr·多模态
黑黑的独立开发笔记6 天前
「 简记往来」用户反馈驱动的产品迭代:简记往来的6次关键更新与一次“不做”的决策
ocr·用户运营·迭代加深·用户体验·产品决策·简记往来
weixin_408099677 天前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)
图像处理·python·ocr·文字识别·api调用·批量识别·石榴智能
stereohomology7 天前
测试unlimited ocr
ocr·colab