아 리 운 OCR - 신분증 / 영업 허가증 식별

51111 단어
구체 적 인 서비스 호출 을 진행 하기 전에 다음 절 차 를 참고 하여 준비 작업 을 완성 하 십시오.
아 리 클 라 우 드 AccessKeyId 와 AccessKeySecret 를 만 듭 니 다.구체 적 으로 는 AccessKey 생 성 을 참조 하 십시오.자바 의존 설치.구체 적 으로 자바 의존 설치 참조.프로젝트 프로젝트 에 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;
    }

좋은 웹페이지 즐겨찾기