伙伴云客服论坛»论坛 S区 S软件开发 查看内容

0 评论

0 收藏

分享

JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别

最近有由于需要,我开端接触阿里云的云市场的印刷文字识别-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单参考。
一、API介绍
JAVA示例:
  1. public static void main(String[] args) {
  2.    String host = "https://dm-58.data.aliyun.com";
  3.    String path = "/rest/160601/ocr/ocr_business_license.json";
  4.    String method = "POST";
  5.    String appcode = "你自己的AppCode";
  6.    Map<String, String> headers = new HashMap<String, String>();
  7.    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
  8.    headers.put("Authorization", "APPCODE " + appcode);
  9.    //根据API的要求,定义相对应的Content-Type
  10.    headers.put("Content-Type", "application/json; charset=UTF-8");
  11.    Map<String, String> querys = new HashMap<String, String>();
  12.    String bodys = "{"image":"对图片内容停止Base64编码"}";
  13.    try {
  14.    /**
  15.    * 重要提示如下:
  16.    * HttpUtils请从
  17.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
  18.    * 下载
  19.    *
  20.    * 相应的依赖请参照
  21.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
  22.    */
  23.    HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
  24.    System.out.println(response.toString());
  25.    //获取response的body
  26.    //System.out.println(EntityUtils.toString(response.getEntity()));
  27.    } catch (Exception e) {
  28.    e.printStackTrace();
  29.    }
  30. }
复制代码
返回码:
  1. {
  2.   "config_str" : "null\n", #配置字符串信息
  3.   "angle" : float, #输入图片的角度(顺时针旋转),[0, 90, 180,270]
  4.   "reg_num" : string, #注册号,没有识别出来时返回"FailInRecognition"
  5.   "name" : string, #公司名称,没有识别出来时返回"FailInRecognition"
  6.   "type" : string, #公司类型,没有识别出来时返回"FailInRecognition"
  7.   "person" : string, #公司法人,没有识别出来时返回"FailInRecognition"
  8.   "establish_date": string, #公司注册日期(例:证件上为"2014年04月16日",算法返回"20140416")
  9.   "valid_period": string, #公司营业期限终止日期(例:证件上为"2014年04月16日至2034年04月15日",算法返回"20340415")
  10.   #当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231",若证件上没有营业期限,则默认其为"长期",返回"29991231"。
  11.   "address" : string, #公司地址,没有识别出来时返回"FailInRecognition"
  12.   "capital" : string, #注册资本,没有识别出来时返回"FailInRecognition"
  13.   "business": string, #经营范围,没有识别出来时返回"FailInRecognition"
  14.   "emblem" : string, #国徽位置[top,left,height,width],没有识别出来时返回"FailInDetection"
  15.   "title" : string, #标题位置[top,left,height,width],没有识别出来时返回"FailInDetection"
  16.   "stamp" : string, #印章位置[top,left,height,width],没有识别出来时返回"FailInDetection"
  17.   "qrcode" : string, #二维码位置[top,left,height,width],没有识别出来时返回"FailInDetection"
  18.   "success" : bool, #识别胜利与否 true/false
  19.   "request_id": string
  20. }
复制代码
购置后,在云市场列表里展示,你可要点击进去查看AppCode等其主要信息(接口调用里需要AppCode)
简单的API介绍,但挺有效了的,唯一不好的就是没有错误返回码的描绘,需要自己测试。
二、代码开发

闲话少说,上代码:
AliyunImageRecognitionUtil :阿里云图像识别工具类
  1. package com.casic.cloud.qcy.util;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.util.EntityUtils;
  9. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.casic.cloud.qcy.constant.Constants;
  12. import sun.misc.BASE64Encoder;
  13. /**
  14. * @ClassName: AliyunImageRecognitionUtil
  15. * @Description: 阿里云图像识别工具类
  16. * @author: tianpengw
  17. * @date 2019年3月21日 上午8:49:08
  18. *
  19. */
  20. public class AliyunImageRecognitionUtil {
  21. private static String businessLicenceHost = PropertiesUtil.getProperties("businessLicenceHost");
  22. private static String businessLicencePath = PropertiesUtil.getProperties("businessLicencePath");
  23. private static String method = "POST";
  24. private static String appCode = PropertiesUtil.getProperties("appCode");
  25. /**
  26. *
  27. * @Description: 根据文件全途径识别
  28. * @author: tianpengw
  29. * @param imgPath
  30. * @return
  31. */
  32. public static Map<String,Object> bussinessLicenceRecognition(String imgPath){
  33. Map<String,Object> resultMap = new HashMap<String,Object>();
  34. Map<String, String> headers = new HashMap<String, String>();
  35. //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
  36.    headers.put("Authorization", "APPCODE " + appCode);
  37.    headers.put("Content-Type", "application/json; charset=UTF-8");
  38.    Map<String, String> querys = new HashMap<String, String>();
  39.    String bodys = "{"image":""+imageToBase64Str(imgPath)+""}";
  40.    try {
  41.    /**
  42.    * 重要提示如下:
  43.    * HttpUtils请从
  44.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
  45.    * 下载
  46.    *
  47.    * 相应的依赖请参照
  48.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
  49.    */
  50.    HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
  51.    String resStr = EntityUtils.toString(response.getEntity());
  52.    System.out.println(resStr);
  53.    resultMap = JSONObject.parseObject(resStr, Map.class);
  54.    //获取response的body
  55.    } catch (Exception e) {
  56.    e.printStackTrace();
  57.    }
  58.    return resultMap;
  59. }
  60. /**
  61. *
  62. * @Description: 根据InputStream识别
  63. * @author: tianpengw
  64. * @param imgPath
  65. * @return
  66. */
  67. public static Map<String,Object> bussinessLicenceRecognition(InputStream inputStream){
  68. Map<String,Object> resultMap = new HashMap<String,Object>();
  69. Map<String, String> headers = new HashMap<String, String>();
  70. //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
  71.    headers.put("Authorization", "APPCODE " + appCode);
  72.    headers.put("Content-Type", "application/json; charset=UTF-8");
  73.    Map<String, String> querys = new HashMap<String, String>();
  74.    // 加密
  75.    BASE64Encoder encoder = new BASE64Encoder();
  76.    byte[] data = null;
  77.    try {
  78.     data = new byte[inputStream.available()];
  79.     inputStream.read(data);
  80.     inputStream.close();
  81.    } catch (IOException e) {
  82.     e.printStackTrace();
  83.    }
  84.    String bodys = "{"image":""+encoder.encode(data)+""}";
  85.    try {
  86.    /**
  87.    * 重要提示如下:
  88.    * HttpUtils请从
  89.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
  90.    * 下载
  91.    *
  92.    * 相应的依赖请参照
  93.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
  94.    */
  95.    HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
  96.    String resStr = EntityUtils.toString(response.getEntity());
  97.    System.out.println(resStr);
  98.    resultMap = JSONObject.parseObject(resStr, Map.class);
  99.    resultMap.put("errCode", Constants.RESULT_SUCCESS);
  100.    //获取response的body
  101.    } catch (Exception e) {
  102.    e.printStackTrace();
  103.    resultMap.put("errCode", Constants.RESULT_FAIL);
  104.    }
  105.    return resultMap;
  106. }
  107. /**
  108. *
  109. * @Description: 根据byte[]识别
  110. * @author: tianpengw
  111. * @param imgPath
  112. * @return
  113. */
  114. public static Map<String,Object> bussinessLicenceRecognition( byte[] data){
  115. Map<String,Object> resultMap = new HashMap<String,Object>();
  116. Map<String, String> headers = new HashMap<String, String>();
  117. //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
  118.    headers.put("Authorization", "APPCODE " + appCode);
  119.    headers.put("Content-Type", "application/json; charset=UTF-8");
  120.    Map<String, String> querys = new HashMap<String, String>();
  121.    // 加密
  122.    BASE64Encoder encoder = new BASE64Encoder();
  123.    String bodys = "{"image":""+encoder.encode(data)+""}";
  124.    try {
  125.    /**
  126.    * 重要提示如下:
  127.    * HttpUtils请从
  128.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
  129.    * 下载
  130.    *
  131.    * 相应的依赖请参照
  132.    * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
  133.    */
  134.    HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
  135.    String resStr = EntityUtils.toString(response.getEntity());
  136.    System.out.println(resStr);
  137.    resultMap = JSONObject.parseObject(resStr, Map.class);
  138.    resultMap.put("errCode", Constants.RESULT_SUCCESS);
  139.    //获取response的body
  140.    } catch (Exception e) {
  141.    e.printStackTrace();
  142.    resultMap.put("errCode", Constants.RESULT_FAIL);
  143.    }
  144.    return resultMap;
  145. }
  146. /**
  147.   * 图片转base64字符串
  148.   * @param imgFile 图片途径
  149.   * @return
  150.   */
  151.   public static String imageToBase64Str(String imgFile) {
  152.    InputStream inputStream = null;
  153.    byte[] data = null;
  154.    try {
  155.     inputStream = new FileInputStream(imgFile);
  156.     data = new byte[inputStream.available()];
  157.     inputStream.read(data);
  158.     inputStream.close();
  159.    } catch (IOException e) {
  160.     e.printStackTrace();
  161.    }
  162.    // 加密
  163.    BASE64Encoder encoder = new BASE64Encoder();
  164.    return encoder.encode(data);
  165.   }
  166.   public static void main(String[] args) {
  167. String imgPath = "d:/yyzznew1.jpg";
  168. Map<String,Object> map = AliyunImageRecognitionUtil.bussinessLicenceRecognition(imgPath);
  169. System.out.println(map.get("person"));
  170. System.out.println(map.get("reg_num"));
  171. }
  172. }
复制代码
AliyunHttpUtils :阿里云httpUtils
  1. package com.casic.cloud.qcy.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.security.KeyManagementException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.cert.X509Certificate;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.net.ssl.SSLContext;
  11. import javax.net.ssl.TrustManager;
  12. import javax.net.ssl.X509TrustManager;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.http.HttpResponse;
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. import org.apache.http.client.methods.HttpDelete;
  19. import org.apache.http.client.methods.HttpGet;
  20. import org.apache.http.client.methods.HttpPost;
  21. import org.apache.http.client.methods.HttpPut;
  22. import org.apache.http.conn.ClientConnectionManager;
  23. import org.apache.http.conn.scheme.Scheme;
  24. import org.apache.http.conn.scheme.SchemeRegistry;
  25. import org.apache.http.conn.ssl.SSLSocketFactory;
  26. import org.apache.http.entity.ByteArrayEntity;
  27. import org.apache.http.entity.StringEntity;
  28. import org.apache.http.impl.client.DefaultHttpClient;
  29. import org.apache.http.message.BasicNameValuePair;
  30. /**
  31. * @ClassName: AliyunHttpUtils
  32. * @Description:
  33. * @author: tianpengw
  34. * @date 2019年3月21日 上午8:44:51
  35. *
  36. */
  37. public class AliyunHttpUtils {
  38. /**
  39. * get
  40. *
  41. * @param host
  42. * @param path
  43. * @param method
  44. * @param headers
  45. * @param querys
  46. * @return
  47. * @throws Exception
  48. */
  49. public static HttpResponse doGet(String host, String path, String method,
  50.   Map<String, String> headers,
  51.   Map<String, String> querys)
  52.       throws Exception {   
  53.    HttpClient httpClient = wrapClient(host);
  54.    HttpGet request = new HttpGet(buildUrl(host, path, querys));
  55.     for (Map.Entry<String, String> e : headers.entrySet()) {
  56.      request.addHeader(e.getKey(), e.getValue());
  57.     }
  58.     return httpClient.execute(request);
  59.   }
  60. /**
  61. * post form
  62. *
  63. * @param host
  64. * @param path
  65. * @param method
  66. * @param headers
  67. * @param querys
  68. * @param bodys
  69. * @return
  70. * @throws Exception
  71. */
  72. public static HttpResponse doPost(String host, String path, String method,
  73.   Map<String, String> headers,
  74.   Map<String, String> querys,
  75.   Map<String, String> bodys)
  76.       throws Exception {   
  77.    HttpClient httpClient = wrapClient(host);
  78.    HttpPost request = new HttpPost(buildUrl(host, path, querys));
  79.     for (Map.Entry<String, String> e : headers.entrySet()) {
  80.      request.addHeader(e.getKey(), e.getValue());
  81.     }
  82.     if (bodys != null) {
  83.       List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  84.       for (String key : bodys.keySet()) {
  85.         nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
  86.       }
  87.       UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
  88.       formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
  89.       request.setEntity(formEntity);
  90.     }
  91.     return httpClient.execute(request);
  92.   }
  93. /**
  94. * Post String
  95. *
  96. * @param host
  97. * @param path
  98. * @param method
  99. * @param headers
  100. * @param querys
  101. * @param body
  102. * @return
  103. * @throws Exception
  104. */
  105. public static HttpResponse doPost(String host, String path, String method,
  106.   Map<String, String> headers,
  107.   Map<String, String> querys,
  108.   String body)
  109.       throws Exception {   
  110.    HttpClient httpClient = wrapClient(host);
  111.    HttpPost request = new HttpPost(buildUrl(host, path, querys));
  112.     for (Map.Entry<String, String> e : headers.entrySet()) {
  113.      request.addHeader(e.getKey(), e.getValue());
  114.     }
  115.     if (StringUtils.isNotBlank(body)) {
  116.      request.setEntity(new StringEntity(body, "utf-8"));
  117.     }
  118.     return httpClient.execute(request);
  119.   }
  120. /**
  121. * Post stream
  122. *
  123. * @param host
  124. * @param path
  125. * @param method
  126. * @param headers
  127. * @param querys
  128. * @param body
  129. * @return
  130. * @throws Exception
  131. */
  132. public static HttpResponse doPost(String host, String path, String method,
  133.   Map<String, String> headers,
  134.   Map<String, String> querys,
  135.   byte[] body)
  136.       throws Exception {   
  137.    HttpClient httpClient = wrapClient(host);
  138.    HttpPost request = new HttpPost(buildUrl(host, path, querys));
  139.     for (Map.Entry<String, String> e : headers.entrySet()) {
  140.      request.addHeader(e.getKey(), e.getValue());
  141.     }
  142.     if (body != null) {
  143.      request.setEntity(new ByteArrayEntity(body));
  144.     }
  145.     return httpClient.execute(request);
  146.   }
  147. /**
  148. * Put String
  149. * @param host
  150. * @param path
  151. * @param method
  152. * @param headers
  153. * @param querys
  154. * @param body
  155. * @return
  156. * @throws Exception
  157. */
  158. public static HttpResponse doPut(String host, String path, String method,
  159.   Map<String, String> headers,
  160.   Map<String, String> querys,
  161.   String body)
  162.       throws Exception {   
  163.    HttpClient httpClient = wrapClient(host);
  164.    HttpPut request = new HttpPut(buildUrl(host, path, querys));
  165.     for (Map.Entry<String, String> e : headers.entrySet()) {
  166.      request.addHeader(e.getKey(), e.getValue());
  167.     }
  168.     if (StringUtils.isNotBlank(body)) {
  169.      request.setEntity(new StringEntity(body, "utf-8"));
  170.     }
  171.     return httpClient.execute(request);
  172.   }
  173. /**
  174. * Put stream
  175. * @param host
  176. * @param path
  177. * @param method
  178. * @param headers
  179. * @param querys
  180. * @param body
  181. * @return
  182. * @throws Exception
  183. */
  184. public static HttpResponse doPut(String host, String path, String method,
  185.   Map<String, String> headers,
  186.   Map<String, String> querys,
  187.   byte[] body)
  188.       throws Exception {   
  189.    HttpClient httpClient = wrapClient(host);
  190.    HttpPut request = new HttpPut(buildUrl(host, path, querys));
  191.     for (Map.Entry<String, String> e : headers.entrySet()) {
  192.      request.addHeader(e.getKey(), e.getValue());
  193.     }
  194.     if (body != null) {
  195.      request.setEntity(new ByteArrayEntity(body));
  196.     }
  197.     return httpClient.execute(request);
  198.   }
  199. /**
  200. * Delete
  201. *
  202. * @param host
  203. * @param path
  204. * @param method
  205. * @param headers
  206. * @param querys
  207. * @return
  208. * @throws Exception
  209. */
  210. public static HttpResponse doDelete(String host, String path, String method,
  211.   Map<String, String> headers,
  212.   Map<String, String> querys)
  213.       throws Exception {   
  214.    HttpClient httpClient = wrapClient(host);
  215.    HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
  216.     for (Map.Entry<String, String> e : headers.entrySet()) {
  217.      request.addHeader(e.getKey(), e.getValue());
  218.     }
  219.     return httpClient.execute(request);
  220.   }
  221. private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
  222.    StringBuilder sbUrl = new StringBuilder();
  223.    sbUrl.append(host);
  224.    if (!StringUtils.isBlank(path)) {
  225.    sbUrl.append(path);
  226.     }
  227.    if (null != querys) {
  228.    StringBuilder sbQuery = new StringBuilder();
  229.      for (Map.Entry<String, String> query : querys.entrySet()) {
  230.      if (0 < sbQuery.length()) {
  231.       sbQuery.append("&");
  232.      }
  233.      if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
  234.       sbQuery.append(query.getValue());
  235.         }
  236.      if (!StringUtils.isBlank(query.getKey())) {
  237.       sbQuery.append(query.getKey());
  238.       if (!StringUtils.isBlank(query.getValue())) {
  239.       sbQuery.append("=");
  240.       sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
  241.       }      
  242.         }
  243.      }
  244.      if (0 < sbQuery.length()) {
  245.      sbUrl.append("?").append(sbQuery);
  246.      }
  247.     }
  248.    return sbUrl.toString();
  249.   }
  250. private static HttpClient wrapClient(String host) {
  251. HttpClient httpClient = new DefaultHttpClient();
  252. if (host.startsWith("https://")) {
  253.   sslClient(httpClient);
  254. }
  255. return httpClient;
  256. }
  257. private static void sslClient(HttpClient httpClient) {
  258.     try {
  259.       SSLContext ctx = SSLContext.getInstance("TLS");
  260.       X509TrustManager tm = new X509TrustManager() {
  261.         public X509Certificate[] getAcceptedIssuers() {
  262.           return null;
  263.         }
  264.         public void checkClientTrusted(X509Certificate[] xcs, String str) {
  265.         }
  266.         public void checkServerTrusted(X509Certificate[] xcs, String str) {
  267.         }
  268.       };
  269.       ctx.init(null, new TrustManager[] { tm }, null);
  270.       SSLSocketFactory ssf = new SSLSocketFactory(ctx);
  271.       ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  272.       ClientConnectionManager ccm = httpClient.getConnectionManager();
  273.       SchemeRegistry registry = ccm.getSchemeRegistry();
  274.       registry.register(new Scheme("https", 443, ssf));
  275.     } catch (KeyManagementException ex) {
  276.       throw new RuntimeException(ex);
  277.     } catch (NoSuchAlgorithmException ex) {
  278.      throw new RuntimeException(ex);
  279.     }
  280.   }
  281. }
复制代码
PropertiesUtil :读取配置文件工具
  1. package com.casic.cloud.qcy.util;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. import org.apache.logging.log4j.LogManager;
  6. import org.apache.logging.log4j.Logger;
  7. /**
  8. * @ClassName: PropertiesUtil
  9. * @Description:
  10. * @author tianpengw
  11. * @date 2018年6月27日 下午3:09:08
  12. *
  13. */
  14. public class PropertiesUtil {
  15. private static Logger log = LogManager.getLogger(PropertiesUtil.class);
  16. private static Properties prop;
  17. static{
  18. try{
  19.   if(null == prop){
  20.   prop = new Properties();
  21.   }
  22.   InputStream fis = null;
  23.   fis = ClassLoaderUtils.getResourceAsStream("common.properties", PropertiesUtil.class);
  24.   if(fis!=null){
  25.   prop.load(fis);// 将属性文件流装载到Properties对象中  
  26.   fis.close();// 关闭流  
  27.   }
  28. }catch (Exception e) {
  29.   log.error("读取配置文件出错:" + e );
  30. }
  31. }
  32. /**
  33. *
  34. * @Description: 根据key获取配置的值
  35. * @author tianpengw
  36. * @param key
  37. * @return
  38. */
  39. public static String getProperties(String key){
  40. if(key==null) return null;
  41. return prop.getProperty(key);
  42. }
  43. /**
  44. *
  45. * @Description:
  46. * @author tianpengw
  47. * @param proper 读取配置的文件名称
  48. * @param key
  49. * @return
  50. */
  51. public static String getPropertie(String resourceName,String key){
  52. InputStream fis = ClassLoaderUtils.getResourceAsStream(resourceName, PropertiesUtil.class);
  53. if(fis!=null){
  54.   try {
  55.   prop.load(fis);
  56.   fis.close();// 关闭流  
  57.   } catch (IOException e) {
  58.   e.printStackTrace();
  59.   }  
  60. }
  61. if(key==null) return null;
  62. return prop.getProperty(key);
  63. }
  64. /**
  65. *
  66. * @Description: 根据key获取配置的值,若没有,则取传过来的默认的值
  67. * @author tianpengw
  68. * @param key
  69. * @param defaultValue 默认值
  70. * @return
  71. */
  72. public static String getProperties(String key,String defaultValue){
  73. if(key==null) return null;
  74. return prop.getProperty(key, defaultValue);
  75. }
  76. }
复制代码
配置文件common.properties(放在 src/main/resources下)
  1. #aliyun business licence recognition
  2. businessLicenceHost = https://dm-58.data.aliyun.com
  3. businessLicencePath = /rest/160601/ocr/ocr_business_license.json
  4. appCode = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
复制代码
三、测试结果

胜利的结果:
失败结果,当处置非营业执照的图片时将会返回“invalid business licence”结果,我这么没有停止判断处置直接依照正确的json解析,所以报错,此处你们可以依照自己需求选择处置逻辑。从这返回结果也看的出该接口做的不合理,返回内容不统一,导致接口使用者,必需知晓各种情况才干做有效的处置,假设此处能返回json串,给错误码那对于调用者来说就方便多了。
四、后记

此API适宜一般的营业执照和新的三证合一的营业执照,但是字段获取的含义不同,此处需要注意;
由于API并未列出错误码,在未知的错误情况还包含识别次数不够的时候是报错还是有错误提示(由于条件限制,此情况无法验证);

工具是死的,人是活的,开发需要考虑尽可能多的情况,来保证代码的完善。
至此,工具介绍完毕,欢送交流。

回复

举报 使用道具

全部回复
暂无回帖,快来参与回复吧
本版积分规则 高级模式
B Color Image Link Quote Code Smilies

微黄
注册会员
主题 13
回复 20
粉丝 0
|网站地图
快速回复 返回顶部 返回列表