调用OCR识别身份证信息

2019-06-21  本文已影响0人  一口奥利奥

OCR身份证服务接口:

//上传身份信息及判断接口返回信息:

try {

            OCRResponse ocrResponse = new OCRClient().recognition(file, cbName, cbIdno);

            if(!ocrResponse.isSuccess()){

                errors.reject(null, ocrResponse.getDesc());

                return error();

            }

            String side = ocrResponse.getResult().getSide();

            logger.info(">>>>> adDocCd: "+adDocCd+",side: "+side);

            //身份证正面照校验

            if("01-1".equals(adDocCd)){

                if(!"front".equals(side)){

                    errors.reject(null, "上传的照片为非身份证正面照片,请核实后上传");

                    return error();

                }

                if(!cbIdno.equals(ocrResponse.getResult().getIdCardNumber())){

                    errors.reject(null, "您上传的身份证信息有误,请核实后再上传");

                    return error();

                }

                if(!cbName.equals(ocrResponse.getResult().getName())){

                    TblCsmCstbsc tblCsmCstbsc=new TblCsmCstbsc();

                    tblCsmCstbsc.setCbCustId(cbCustId);

                    tblCsmCstbsc.setCbName(ocrResponse.getResult().getName());

                    tblCsmCstbscDao.updateByPrimaryKeySelective(tblCsmCstbsc);

                }

            }

            //身份证背面照校验

            if("01-2".equals(adDocCd)){

                if(!"back".equals(side)){

                    errors.reject(null, "上传的照片为非身份证背面照片,请核实后上传");

                    return error();

                }

                String currentDateStr = DateUtil.getCurrentDateStr();

                String validDataBegin = ocrResponse.getResult().getValidDataBegin();

                String validDataEnd = ocrResponse.getResult().getValidDataEnd();

                if(StringUtils.isBlank(validDataBegin)||StringUtils.isBlank(validDataEnd)){

                    errors.reject(null, "无法解析身份证有效期");

                    return error();

                }

                if(!"长期".equals(validDataEnd)){

                    validDataBegin=validDataBegin.replaceAll("\\.","");

                    validDataEnd=validDataEnd.replaceAll("\\.","");

                    int dayCount = DateUtil.dayCount(currentDateStr, validDataEnd);

                    if(dayCount >0 && (dayCount+1) <= 30){

                        errors.reject(null, "当前身份证即将过期,请提供最新身份证进行更新");

                        return error();

                    }

                    if(dayCount <= 0){

                        errors.reject(null, "当前身份证已过期,请提供最新身份证进行更新");

                        return error();

                    }

                }

                TblCsmCstbsc tblCsmCstbsc=new TblCsmCstbsc();

                tblCsmCstbsc.setCbCustId(cbCustId);

                tblCsmCstbsc.setCbIdEffectDate(validDataBegin);

                tblCsmCstbsc.setCbIdExpiryDate(validDataEnd);

                tblCsmCstbsc.setCbIsuseOcr("1");

                tblCsmCstbsc.setCbUseocrDate(currentDateStr);

                tblCsmCstbscDao.updateByPrimaryKeySelective(tblCsmCstbsc);

            }

        }catch (Exception e){

            logger.error(">>>>> OCR recognition Exception:",e);

            errors.reject(null, "文件上传出现错误,请联系管理员");

            return error();

        }

//识别身份信息:

public OCRResponse recognition(MultipartFile file,String name,String idNo){

OCRResponse ocrResponse=new OCRResponse();

String imgBase64;

try {

byte[] btImg = readStream(file.getInputStream());//读取管道中的流数据

BASE64Encoder be =new BASE64Encoder();

imgBase64 = be.encode(btImg).replaceAll("[\r\n]","");

}catch (Exception e) {

logger.error(">>>>> InputStream toBase64 Exception: ",e);

ocrResponse.setDesc("解析文件流出错");

return ocrResponse;

}

String ocrUrl = SysUtil.getStringConfigProperty("ocr.userGroup.url");

logger.info(">>>>> ocrUrl:"+ocrUrl);

JSONObject jsonObj =new JSONObject();

jsonObj.put("channelNo","A10A00JCCF");

jsonObj.put("name", name);

jsonObj.put("idno", idNo);

jsonObj.put("faceImage",imgBase64);

String request = jsonObj.toJSONString();

logger.info(">>>>> Request:"+request);

String response=null;

try{

response = HttpUtil.doPost(ocrUrl, request);

}catch (Exception e){

logger.error(">>>>> Call OCR Exception: ",e);

ocrResponse.setDesc("调用OCR服务出现异常");

}

logger.info("<<<<< Response:"+response);

if(response !=null){

try{

response=response.replaceAll("\\\\","");

response=response.replaceAll("\"\\{","\\{");

response=response.replaceAll("\\}\"","}");

ocrResponse = JSON.parseObject(response, OCRResponse.class);

}catch (Exception e){

logger.error(">>>>> JSON.parseObject Exception: ",e);

ocrResponse.setDesc("解析返回报文出错");

}

}

return ocrResponse;

}

/**

* 读取管道中的流数据

*/

private byte[] readStream(InputStream inStream) {

ByteArrayOutputStream bops =new ByteArrayOutputStream();

int data = -1;

try {

while((data = inStream.read()) != -1){

bops.write(data);

}

return bops.toByteArray();

}catch(Exception e){

logger.error(">>>>> InputStream toByteArray Exception: ",e);

throw new RuntimeException("解析文件流出错");

}

}

上一篇 下一篇

猜你喜欢

热点阅读