阿里云OCR-身份证/营业执照识别

在进行具体的服务调用之前,请参见以下步骤,完成准备工作:

创建阿里云AccessKeyId和AccessKeySecret。具体请参见创建AccessKey。
安装Java依赖。具体请参见安装Java依赖。
下载并在项目工程中引入Extension.Uploader工具类。

官方文档:

身份证信息

@RequestMapping(value = "/idCardInfo")
    @ResponseBody
    public Object idCardInfo(MultipartFile idcardFront) throws ClientException {
     
        IClientProfile profile = DefaultProfile
                .getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
        DefaultProfile
                .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
        // 指定api返回格式
        imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
        // 指定请求方法
        imageSyncScanRequest.setMethod(MethodType.POST);
        imageSyncScanRequest.setEncoding("utf-8");
        //支持http和https
        imageSyncScanRequest.setProtocol(ProtocolType.HTTP);

        JSONObject httpBody = new JSONObject();
        /**
         * 设置要检测的场景
         * ocr: ocr或者ocr卡证识别
         */
        httpBody.put("scenes", Arrays.asList("ocr"));
        /**
         * 设置待检测图片, 一张图片一个task,最多支持100张图片同时检测,即需要构建100个task
         * 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。因此通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高
         * 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task
         * 图片二进制数据检测相对于互联网图片链接来说,多了一个上传步骤,上传后取返回的链接进行检测
         */
        ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
        byte[] imageBytes = null;
        String url = null;
        try{
     
            //这里读取本地文件作为二进制数据,当做输入做为示例, 实际使用中请直接替换成您的图片二进制数据
            //imageBytes = FileUtils.readFileToByteArray();
            //上传到服务端
            url = clientUploader.uploadBytes(idcardFront.getBytes());
        }catch (Exception e){
     
            System.out.println("upload file to server fail.");
        }

        JSONObject task = new JSONObject();
        task.put("dataId", UUID.randomUUID().toString());
        task.put("url", url);
        task.put("time", new Date());
        httpBody.put("tasks", Arrays.asList(task));

        //ocr卡证识别,设置识别卡证类型
        JSONObject cardExtras = new JSONObject();
        //身份证正面识别
        cardExtras.put("card", "id-card-front");
        //身份证反面
        //cardExtras.put("card", "id-card-back");
        httpBody.put("extras", cardExtras);
        imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
        /**
         * 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置
         * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout 异常
         */
        imageSyncScanRequest.setConnectTimeout(3000);
        imageSyncScanRequest.setReadTimeout(10000);
        HttpResponse httpResponse = null;
        try {
     
            httpResponse = client.doAction(imageSyncScanRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        } catch (Exception e){
     
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        //服务端接收到请求,并完成处理返回的结果
        if(httpResponse != null && httpResponse.isSuccess()){
     
            JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
            //System.out.println(JSON.toJSONString(scrResponse));
            int requestCode = scrResponse.getIntValue("code");
            //每一张图片的检测结果
            JSONArray taskResults = scrResponse.getJSONArray("data");
            if (200 == requestCode) {
     
                for (Object taskResult : taskResults) {
     
                    //单张图片的处理结果
                    int taskCode = ((JSONObject)taskResult).getIntValue("code");
                    //图片要检测的场景的处理结果, 如果是多个场景,则会有每个场景的结果
                    JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
                    if(200 == taskCode){
     
                        for (Object sceneResult : sceneResults) {
     
                            String scene = ((JSONObject)sceneResult).getString("scene");
                            String suggestion = ((JSONObject)sceneResult).getString("suggestion");
                            //do something
                            //有识别出卡证信息
                            if("review" .equals(suggestion) && "ocr".equals(scene)){
     
                                JSONObject idCardInfo =  ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                                // System.out.println(idCardInfo.toJSONString());
                                map.put("name", idCardInfo.get("name"));
                                map.put("number", idCardInfo.get("number"));
                            }
                        }
                    }else{
     
                        //单张图片处理失败, 原因视具体的情况详细分析
                        //System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        map.put("error","task process fail. task response:" + JSON.toJSONString(taskResult));
                    }
                }
            } else {
     
                /**
                 * 表明请求整体处理失败,原因视具体的情况详细分析
                 */
                //System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                map.put("error", "the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
            }
        }
        return map;
    }

营业执照

@RequestMapping(value = "/businessLicenseInfo")
    @ResponseBody
    public Object businessLicenseInfo(MultipartFile file) throws ClientException {
     
        IClientProfile profile = DefaultProfile
                .getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");
        DefaultProfile
                .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
        // 指定api返回格式
        imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
        // 指定请求方法
        imageSyncScanRequest.setMethod(MethodType.POST);
        imageSyncScanRequest.setEncoding("utf-8");
        //支持http和https
        imageSyncScanRequest.setProtocol(ProtocolType.HTTP);

        JSONObject httpBody = new JSONObject();
        /**
         * 设置要检测的场景
         * ocr: ocr或者ocr卡证识别
         */
        httpBody.put("scenes", Arrays.asList("ocr"));
        /**
         * 设置待检测图片, 一张图片一个task,最多支持100张图片同时检测,即需要构建100个task
         * 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。因此通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高
         * 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task
         * 图片二进制数据检测相对于互联网图片链接来说,多了一个上传步骤,上传后取返回的链接进行检测
         */
        ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
        byte[] imageBytes = null;
        String url = null;
        try{
     
            //这里读取本地文件作为二进制数据,当做输入做为示例, 实际使用中请直接替换成您的图片二进制数据
            //imageBytes = FileUtils.readFileToByteArray((File) file);
            //上传到服务端
            url = clientUploader.uploadBytes(file.getBytes());
        }catch (Exception e){
     
            System.out.println("upload file to server fail.");
        }

        JSONObject task = new JSONObject();
        task.put("dataId", UUID.randomUUID().toString());
        task.put("url", url);
        task.put("time", new Date());
        httpBody.put("tasks", Arrays.asList(task));

        //ocr卡证识别,设置识别卡证类型
        JSONObject cardExtras = new JSONObject();
        // 图片类型:营业执照
        cardExtras.put("card", "business-license");
        httpBody.put("extras", cardExtras);
        imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
        /**
         * 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置
         * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout 异常
         */
        imageSyncScanRequest.setConnectTimeout(3000);
        imageSyncScanRequest.setReadTimeout(10000);
        HttpResponse httpResponse = null;
        try {
     
            httpResponse = client.doAction(imageSyncScanRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        } catch (Exception e){
     
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        //服务端接收到请求,并完成处理返回的结果
        if(httpResponse != null && httpResponse.isSuccess()){
     
            JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
            //System.out.println(JSON.toJSONString(scrResponse));
            int requestCode = scrResponse.getIntValue("code");
            //每一张图片的检测结果
            JSONArray taskResults = scrResponse.getJSONArray("data");
            if (200 == requestCode) {
     
                for (Object taskResult : taskResults) {
     
                    //单张图片的处理结果
                    int taskCode = ((JSONObject)taskResult).getIntValue("code");
                    //图片要检测的场景的处理结果, 如果是多个场景,则会有每个场景的结果
                    JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
                    if(200 == taskCode){
     
                        for (Object sceneResult : sceneResults) {
     
                            String scene = ((JSONObject)sceneResult).getString("scene");
                            String suggestion = ((JSONObject)sceneResult).getString("suggestion");
                            //do something
                            //有识别出卡证信息
                            if("review" .equals(suggestion) && "ocr".equals(scene)){
     
                                JSONObject businessLicenseInfo =  ((JSONObject) sceneResult).getJSONObject("businessLicenseInfo");
                                System.out.println(businessLicenseInfo.toJSONString());
                                map.put("regNum", businessLicenseInfo.get("regNum"));
                            }
                        }
                    }else{
     
                        //单张图片处理失败, 原因视具体的情况详细分析
                        //System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        map.put("error","task process fail. task response:" + JSON.toJSONString(taskResult));
                    }
                }
            } else {
     
                /**
                 * 表明请求整体处理失败,原因视具体的情况详细分析
                 */
                //System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                map.put("error", "the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
            }
        }
        return map;
    }

你可能感兴趣的:(阿里云OCR-身份证/营业执照识别)