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

最近有由于工作需要,开始接触阿里云的云市场的印刷文字识别API-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单参考

最近有由于需要,我开始接触阿里云的云市场的印刷文字识别-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单参考。

一、API介绍

JAVA示例: 

 public static void main(String[] args) { String host = "https://dm-58.data.aliyun.com"; String path = "/rest/160601/ocr/ocr_business_license.json"; String method = "POST"; String appcode = "你自己的AppCode"; Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); //根据API的要求,定义相对应的Content-Type headers.put("Content-Type", "application/json; charset=UTF-8"); Map querys = new HashMap(); String bodys = "{\"image\":\"对图片内容进行Base64编码\"}"; try { /** * 重要提示如下: * HttpUtils请从 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java * 下载 * * 相应的依赖请参照 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml */ HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); System.out.println(response.toString()); //获取response的body //System.out.println(EntityUtils.toString(response.getEntity())); } catch (Exception e) { e.printStackTrace(); } }

返回码:

 { "config_str" : "null\n", #配置字符串信息 "angle" : float, #输入图片的角度(顺时针旋转),[0, 90, 180,270] "reg_num" : string, #注册号,没有识别出来时返回"FailInRecognition" "name" : string, #公司名称,没有识别出来时返回"FailInRecognition" "type" : string, #公司类型,没有识别出来时返回"FailInRecognition" "person" : string, #公司法人,没有识别出来时返回"FailInRecognition" "establish_date": string, #公司注册日期(例:证件上为"2014年04月16日",算法返回"20140416") "valid_period": string, #公司营业期限终止日期(例:证件上为"2014年04月16日至2034年04月15日",算法返回"20340415") #当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231",若证件上没有营业期限,则默认其为"长期",返回"29991231"。 "address" : string, #公司地址,没有识别出来时返回"FailInRecognition" "capital" : string, #注册资本,没有识别出来时返回"FailInRecognition" "business": string, #经营范围,没有识别出来时返回"FailInRecognition" "emblem" : string, #国徽位置[top,left,height,width],没有识别出来时返回"FailInDetection" "title" : string, #标题位置[top,left,height,width],没有识别出来时返回"FailInDetection" "stamp" : string, #印章位置[top,left,height,width],没有识别出来时返回"FailInDetection" "qrcode" : string, #二维码位置[top,left,height,width],没有识别出来时返回"FailInDetection" "success" : bool, #识别成功与否 true/false "request_id": string } 

购买后,在云市场列表里展示,你可要点击进去查看AppCode等其主要信息(接口调用里需要AppCode)

简单的API介绍,但挺有效了的,唯一不好的就是没有错误返回码的描述,需要自己测试。

二、代码开发

闲话少说,上代码:

AliyunImageRecognitionUtil :阿里云图像识别工具类

 package com.casic.cloud.qcy.util; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.web.multipart.commons.CommonsMultipartFile; import com.alibaba.fastjson.JSONObject; import com.casic.cloud.qcy.constant.Constants; import sun.misc.BASE64Encoder; /** * @ClassName: AliyunImageRecognitionUtil * @Description: 阿里云图像识别工具类 * @author: tianpengw * @date 2019年3月21日 上午8:49:08 * */ public class AliyunImageRecognitionUtil { private static String businessLicenceHost = PropertiesUtil.getProperties("businessLicenceHost"); private static String businessLicencePath = PropertiesUtil.getProperties("businessLicencePath"); private static String method = "POST"; private static String appCode = PropertiesUtil.getProperties("appCode"); /** * * @Description: 根据文件全路径识别 * @author: tianpengw * @param imgPath * @return */ public static Map bussinessLicenceRecognition(String imgPath){ Map resultMap = new HashMap(); Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appCode); headers.put("Content-Type", "application/json; charset=UTF-8"); Map querys = new HashMap(); String bodys = "{\"image\":\""+imageToBase64Str(imgPath)+"\"}"; try { /** * 重要提示如下: * HttpUtils请从 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java * 下载 * * 相应的依赖请参照 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml */ HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys); String resStr = EntityUtils.toString(response.getEntity()); System.out.println(resStr); resultMap = JSONObject.parseObject(resStr, Map.class); //获取response的body } catch (Exception e) { e.printStackTrace(); } return resultMap; } /** * * @Description: 根据InputStream识别 * @author: tianpengw * @param imgPath * @return */ public static Map bussinessLicenceRecognition(InputStream inputStream){ Map以上就是JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java