package com.ruoyi.web.controller.Idcard; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.utils.Base64Util; import com.ruoyi.common.utils.FileUtil; import com.ruoyi.common.utils.HttpUtil; import java.net.URLEncoder; /** * 营业执照识别 * @Author: tjf * @Date: 2024/4/17 9:57 * @Describe: */ public class BusinessLicense { /** * 重要提示代码中所需工具类 * FileUtil,Base64Util,HttpUtil,GsonUtils请从 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3 * 下载 */ public static JSONObject businessLicense(String image) { // 请求url String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license"; try { // 本地文件路径 byte[] imgData = FileUtil.readFileByBytes(image); String imgStr = Base64Util.encode(imgData); String imgParam = URLEncoder.encode(imgStr, "UTF-8"); String param = "image=" + imgParam; // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。 String accessToken = Idcard.getAccessToken(); String result = HttpUtil.post(url, accessToken, param); /** * { "words_result": { "经营范围": { "location": { "top": 589, "left": 381, "width": 90, "height": 19 }, "words": "商务服务业" }, "组成形式": { "location": { "top": -1, "left": -1, "width": 0, "height": 0 }, "words": "无" }, "法人": { "location": { "top": 537, "left": 381, "width": 36, "height": 19 }, "words": "方平" }, "证件编号": { "location": { "top": 218, "left": 302, "width": 140, "height": 15 }, "words": "921MA190538210301" }, "注册资本": { "location": { "top": 431, "left": 1044, "width": 152, "height": 21 }, "words": "200万元" }, "单位名称": { "location": { "top": 431, "left": 384, "width": 71, "height": 20 }, "words": "有限公司" }, "有效期": { "location": { "top": 536, "left": 1044, "width": 198, "height": 20 }, "words": "长期" }, "社会信用代码": { "location": { "top": 300, "left": 241, "width": 156, "height": 16 }, "words": "10440119MA06M85" }, "实收资本": { "location": { "top": -1, "left": -1, "width": 0, "height": 0 }, "words": "无" }, "有效期起始日期": { "location": { "top": 536, "left": 1044, "width": 198, "height": 20 }, "words": "2019年01月01日" }, "核准日期": { "location": { "top": 884, "left": 1188, "width": 199, "height": 22 }, "words": "2019年01月01日" }, "成立日期": { "location": { "top": 484, "left": 1043, "width": 126, "height": 19 }, "words": "2019年01月01日" }, "税务登记号": { "location": { "top": -1, "left": -1, "width": 0, "height": 0 }, "words": "无" }, "地址": { "location": { "top": 588, "left": 1043, "width": 55, "height": 22 }, "words": "广州市" }, "登记机关": { "location": { "top": 0, "left": 0, "width": 0, "height": 0 }, "words": "无" }, "类型": { "location": { "top": 484, "left": 382, "width": 258, "height": 20 }, "words": "有限责任公司(自然人投资或控股)" } }, "direction": 0, "words_result_num": 16, "log_id": "3166723741167575145" } */ JSONObject jsonObject = JSONObject.parseObject(result); return jsonObject; } catch (Exception e) { e.printStackTrace(); } return null; } }