100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 阿里云OCR识别

阿里云OCR识别

时间:2023-02-22 14:41:46

相关推荐

阿里云OCR识别

1.首先购买阿里云ocr识别接口.

2.控制台中点击 [api网关]

3.点击应用名称进入

4.以身份证ocr为例子在购买页面查看代码示例链接如下:

阿里云身份证ocr代码示例

即可看见对应语言的代码示例

5.本人代码示例

public Result<?> requestAlibaba(String host, String path, AliYunOcrCommonDto entityDTO, Object entityObj) {try {// 请根据线上文档修改configure字段JSONObject configObj = new JSONObject();configObj.put(OCR_SIDE, entityDTO.getFaceOrBack());String configStr = configObj.toString();Map<String, String> headers = requestHeaderMap();Map<String, String> query = new HashMap<String, String>(1);// 对图像进行base64编码String imgBase64 = imgBase64(entityDTO.getImgFile());// 拼装请求body的json字符串JSONObject requestObj = new JSONObject();try {requestObj.put(OCR_IMAGE, imgBase64);if (configStr.length() > 0) {requestObj.put(OCR_CONFIGURE, configStr);}} catch (JSONException e) {log.error(OCR_SYSTEM_ERROR_TITLE, e);}String requestBody = requestObj.toString();HttpResponse response = HttpUtils.doPost(host, path, OCR_POST, headers, query, requestBody);// 判断返回数据是否为空if (ObjectUtils.isEmpty(response)) {// 调用处已经对返回值为null做了处理return Result.error("查询结果为空!");}int stat = response.getStatusLine().getStatusCode();// 如果访问失败if (stat != OCR_HTTP_CODE_200) {// 后端日志错误打印requestPrintLog(stat, response);// 响应体中的错误信息转换为小写 -> responseHttpMsgString responseHttpMsg = EntityUtils.toString(response.getEntity()).toLowerCase();// 获取http枚举类中所有数据HttpCodeMes[] httpCodeMes = HttpCodeMes.values();// 迭代 并对比for (HttpCodeMes httpCM : httpCodeMes) {// 把response响应体中的信息转换为小写String httpCMLow = httpCM.getHttpMsg().toLowerCase();// 响应体信息 与 枚举类信息 小写contains (contains类似SQL语句的like)if (httpCM.getHttpCode() == stat && httpCMLow.contains(responseHttpMsg)) {// 如果 对比成功那么返回当前http状态码所代表的含义给前端 通过统一返还类型return Result.error(httpCMLow + httpCM.getHttpMsgCn(), stat);}}return Result.error("OCR接口未知http状态码!");}// 200 成功时// 证件正面if (OCR_FACE.equals(entityDTO.getFaceOrBack())) {if (entityObj instanceof DrivingLicenseEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);DrivingLicenseEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), DrivingLicenseEntityVO.class);return Result.OK(entityVO);} else if (entityObj instanceof IdentityCardEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);IdentityCardEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), IdentityCardEntityVO.class);return Result.OK(entityVO);} else if (entityObj instanceof VehicleLicenseEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);VehicleLicenseEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), VehicleLicenseEntityVO.class);return Result.OK(entityVO);}// 证件反面} else {if (entityObj instanceof DrivingLicenseEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);DrivingLicenseBackEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), DrivingLicenseBackEntityVO.class);return Result.OK(entityVO);} else if (entityObj instanceof IdentityCardEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);IdentityCardBackEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), IdentityCardBackEntityVO.class);return Result.OK(entityVO);} else if (entityObj instanceof VehicleLicenseEntity) {String res = EntityUtils.toString(response.getEntity());JSONObject jsonObject = JSON.parseObject(res);VehicleLicenseBackEntityVO entityVO = JSONObject.parseObject(jsonObject.toJSONString(), VehicleLicenseBackEntityVO.class);return Result.OK(entityVO);}}} catch (IOException e) {log.error(OCR_SYSTEM_ERROR_TITLE, e);return Result.error("系统异常!", HttpStatus.SC_INTERNAL_SERVER_ERROR);}return Result.OK("查询结果为空!");}

这是封装好的代码,仅供参考之用,如果需要,请自行修改.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。