java实现文字识别营业执照识别(百度、讯飞)
工作中遇到一个需求是识别营业执照,看了阿里云的,腾讯云的,讯飞的,百度的。然后发现阿里云和腾讯云目前都是公测或者内测阶段,所以就去试了百度的,但是百度的只是普通的文字识别,就是识别文字中的图片,而讯飞的就比较专业了,单纯的识别营业执照。
先看一下使用百度的文字识别的步骤
百度AI开放平台的地址:营业执照识别,准确识别各样式关键字段,免费试用-百度AI开放平台
登录之后在右上角控制台创建一个应用,
创建完应用后点击管理应用
这里用AppId和两个key,后面代码里用到
需要引入的包
<dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>4.15.1</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.13</version></dependency>
代码实现:
把刚才的AppID和两个key放在对应的地方就行
package com.vhukze.utils;import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;/*** 百度文字识别*/
public class BaiDuDiscern {//设置APPID/AK/SKpublic static final String APP_ID = "";public static final String API_KEY = "";public static final String SECRET_KEY = "";private static AipOcr client = null;public static void main(String[] args) {disPro();}public static void init(){// 初始化一个AipOcrif(client == null){client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);}// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
// client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理// 可选:设置log4j日志输出格式,若不设置,则使用默认配置// 也可以直接通过jvm启动参数设置此环境变量
// System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");// 调用接口
// String path = "test.jpg";
// JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
// System.out.println(res.toString(2));// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);}//普通文字识别public static void dis(){init();// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();options.put("language_type", "CHN_ENG");options.put("detect_direction", "true");options.put("detect_language", "true");options.put("probability", "true");// 参数为本地图片路径
// String image = "C:\\Users\\zsz\\Desktop\\yyzz1.jfif";
// JSONObject res = client.basicGeneral(image, options);
// System.out.println(res.toString(2));// 参数为本地图片二进制数组byte[] file = getImageBinary("C:\\Users\\zsz\\Desktop\\yyzz1.jfif");JSONObject res = client.basicGeneral(file, options);System.out.println(res.toString(2));// 通用文字识别, 图片参数为远程url图片
// JSONObject res = client.basicGeneralUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604469311212&di=c0d7b582c196c8dba2061d920ed3bb28&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171119%2F97ecd36d67b24e34b850c61d744148e3.jpeg", options);
// System.out.println(res.toString(2));}//高精度版文字识别public static void disPro(){init();// 传入可选参数调用接口HashMap<String, String> options = new HashMap<String, String>();options.put("detect_direction", "true");options.put("probability", "true");// 参数为本地图片路径String image = "C:\\Users\\zsz\\Desktop\\yyzz1.jfif";JSONObject res = client.basicAccurateGeneral(image, options);System.out.println(res.toString(2));// 参数为本地图片二进制数组
// byte[] file = getImageBinary(image);
// res = client.basicAccurateGeneral(file, options);
// System.out.println(res.toString(2));}private static byte[] getImageBinary(String path){File f = new File(path);BufferedImage bi;try {bi = ImageIO.read(f);ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(bi, "jpg", baos);byte[] bytes = baos.toByteArray();return bytes;} catch (IOException e) {e.printStackTrace();}return null;}
}
但是百度的这个文字识别识别的不太好,有的文字错了 有的文字缺了
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面看讯飞的营业执照识别
链接:营业执照识别 intsig - 文字识别 - 讯飞开放平台
点击免费试用,需要实名认证就认证一下
创建一个应用
这里面只有一个AppID和一个APIKey,这两个代码里面用到
直接上代码
package com.vhukze.utils;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;/*** 讯飞营业执照识别*/
public class XfDiscern {// OCR webapi 接口地址private static final String WEBOCR_URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/business_license";// 应用APPID(必须为webapi类型应用,并开通营业执照识别服务,参考帖子如何创建一个webapi应用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)private static final String APPID = "";// 接口密钥(webapi类型应用开通营业执照识别服务后,控制台--我的应用---营业执照识别---相应服务的apikey)private static final String API_KEY = "";// 引擎类型private static final String ENGINE_TYPE = "business_license";// 图片地址private static final String AUDIO_PATH = "C:\\Users\\zsz\\Desktop\\yyzz1.jfif";/*** OCR WebAPI 调用示例程序** @param args* @throws IOException*/public static void main(String[] args) throws IOException {dis();}public static void dis() throws IOException{Map<String, String> header = buildHttpHeader();byte[] imageByteArray = FileUtil.read(AUDIO_PATH);String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");String result = HttpUtil.doPost1(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));System.out.println("WEB card 接口调用结果:" + result);//错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)400开头错误码请在接口文档底部查看}/*** 组装http请求头*/private static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {String curTime = System.currentTimeMillis() / 1000L + "";String param = "{\"engine_type\":\"" + ENGINE_TYPE + "\"}";String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);Map<String, String> header = new HashMap<String, String>();header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");header.put("X-Param", paramBase64);header.put("X-CurTime", curTime);header.put("X-CheckSum", checkSum);header.put("X-Appid", APPID);return header;}
}
里面还用到了两个工具类
FileUtil
package com.vhukze.utils;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** 文件操作工具类*/
public class FileUtil {/*** 读取文件内容为二进制数组* * @param filePath* @return* @throws IOException*/public static byte[] read(String filePath) throws IOException {InputStream in = new FileInputStream(filePath);byte[] data = inputStream2ByteArray(in);in.close();return data;}/*** 流转二进制数组* * @param in* @return* @throws IOException*/private static byte[] inputStream2ByteArray(InputStream in) throws IOException {ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024 * 4];int n = 0;while ((n = in.read(buffer)) != -1) {out.write(buffer, 0, n);}return out.toByteArray();}/*** 保存文件* * @param filePath* @param fileName* @param content*/public static void save(String filePath, String fileName, byte[] content) {try {File filedir = new File(filePath);if (!filedir.exists()) {filedir.mkdirs();}File file = new File(filedir, fileName);OutputStream os = new FileOutputStream(file);os.write(content, 0, content.length);os.flush();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}
HttpUtil
package com.vhukze.utils;import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;/*** Http Client 工具类*/
public class HttpUtil {/*** 发送post请求,根据 Content-Type 返回不同的返回值* * @param url* @param header* @param body* @return*/public static Map<String, Object> doPost2(String url, Map<String, String> header, String body) {Map<String, Object> resultMap = new HashMap<String, Object>();PrintWriter out = null;try {// 设置 urlURL realUrl = new URL(url);URLConnection connection = realUrl.openConnection();HttpURLConnection httpURLConnection = (HttpURLConnection) connection;// 设置 headerfor (String key : header.keySet()) {httpURLConnection.setRequestProperty(key, header.get(key));}// 设置请求 bodyhttpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);out = new PrintWriter(httpURLConnection.getOutputStream());// 保存bodyout.print(body);// 发送bodyout.flush();if (HttpURLConnection.HTTP_OK != httpURLConnection.getResponseCode()) {System.out.println("Http 请求失败,状态码:" + httpURLConnection.getResponseCode());return null;}// 获取响应headerString responseContentType = httpURLConnection.getHeaderField("Content-Type");if ("audio/mpeg".equals(responseContentType)) {// 获取响应bodybyte[] bytes = toByteArray(httpURLConnection.getInputStream());resultMap.put("Content-Type", "audio/mpeg");resultMap.put("sid", httpURLConnection.getHeaderField("sid"));resultMap.put("body", bytes);return resultMap;} else {// 设置请求 bodyBufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));String line;String result = "";while ((line = in.readLine()) != null) {result += line;}resultMap.put("Content-Type", "text/plain");resultMap.put("body", result);return resultMap;}} catch (Exception e) {return null;}}/*** 发送post请求* * @param url* @param header* @param body* @return*/public static String doPost1(String url, Map<String, String> header, String body) {String result = "";BufferedReader in = null;PrintWriter out = null;try {// 设置 urlURL realUrl = new URL(url);URLConnection connection = realUrl.openConnection();HttpURLConnection httpURLConnection = (HttpURLConnection) connection;// 设置 headerfor (String key : header.keySet()) {httpURLConnection.setRequestProperty(key, header.get(key));}// 设置请求 bodyhttpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);out = new PrintWriter(httpURLConnection.getOutputStream());// 保存bodyout.print(body);// 发送bodyout.flush();if (HttpURLConnection.HTTP_OK != httpURLConnection.getResponseCode()) {System.out.println("Http 请求失败,状态码:" + httpURLConnection.getResponseCode());return null;}// 获取响应bodyin = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {return null;}return result;}/*** 流转二进制数组* * @param in* @return* @throws IOException*/private static byte[] toByteArray(InputStream in) throws IOException {ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024 * 4];int n = 0;while ((n = in.read(buffer)) != -1) {out.write(buffer, 0, n);}return out.toByteArray();}
}
java实现文字识别营业执照识别(百度、讯飞)相关推荐
- 阿里云-印刷文字识别-营业执照识别
阿里云营业执照识别API 最近有由于需要,我开始接触阿里云的云市场的印刷文字识别-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单 ...
- asr语音转写_python 腾讯/百度/讯飞 ASR 语音转文字
因为项目中有需要把微信里的语音转成文本处理, 本次只说语音转文本. 需要注意的是平台对语音的格式有要求, 所以我们需要对语音进行转换格式. 语音转换 使用的工具是ffmpeg, ffmpeg的安装和配 ...
- 营业执照OCR识别/营业执照识别优势
众所周知,在很多业务场合,都会涉及到营业执照信息的录入,营业执照作为企业经营的必备证件,可应用于企业信息登记环节,如支付业务申请.自媒体平台企业账号申请.办理银行业务等. 由于营业执照上的信息比较繁杂 ...
- 接入百度智能云的营业执照识别接口、工商信息查询(基础版)接口到实际业务,参考代码
我的代码放在公共模块如下: 1.根据百度云官方文档-SDK中心,下载sdk对应jar包,我下载下来的版本是bce-java-sdk.0.10.254 2.使用mvn命令手动引入jar mvn inst ...
- java版阿里云,百度ai,讯飞语音识别效果简单对比及demo
因为公司的业务的需要,对三家的语音识别(简短语句识别java版)进行了调用和对比,把自己的测试成果贴出来供需要的人参考使用.并贴出主要代码块 阿里云的一句话识别: package com.alibab ...
- 营业执照识别/营业执照OCR识别API
关键词:营业执照识别 营业执照云识别 营业执照ocr识别 营业执照ocr识别API 安卓营业执照识别 ios营业执照识别 营业执照识别/营业执照OCR识别API是一款基于服务器平台开发的营业执照OCR ...
- 服务器端部署营业执照识别
关键词:营业执照识别 营业执照云识别 营业执照ocr识别 营业执照ocr识别API 安卓营业执照识别 ios营业执照识别 营业执照识别/营业执照OCR识别API是一款基于服务器平台开发的营业执照OCR ...
- 营业执照识别api接口调用OCR识别
营业执照识别 营业执照云识别 营业执照识别api产品描述 营业执照识别api开发的一款基于服务器平台的营业执照OCR识别服务程序,支持主流Windows.Linux 服务器平台.上传营业执照图像在服务 ...
- OCR识别+人脸识别
阿里云和百度云识别,京东智能识别. 图片识别需求 1,拿到一个文件,2,变成InputStream 3,base64编码将流解析下载, 4 调用方法识别 1.身份证 2,行驶证 3,驾驶证 Fi ...
- 计算机声音怎么转换,怎么把视频里的声音转换成文字?讯飞听见帮你搞定
之前刚做自媒体视频博主时,做视频最痛苦的并不是拍和剪,而是把视频里的声音转成文字,如果是十几二十秒的视频还好,几分钟就能打好文字,可是碰到要做vlog或者其他一些比较长的视频可就愁了,不仅要重新回顾一 ...
最新文章
- 【数据结构】顺序表的应用(2)(C语言)
- 网页中的load函数
- empty vocabulary; perhaps the documents only contain stop words
- poj3264 - Balanced Lineup(RMQ_ST)
- 日常工作问题解决:配置NTP服务器以及一些常见错误解决
- 5分钟搞定Loki告警多渠道接入
- matlab thetal,基於matlab的車道和車道線檢測樣例
- asp.net 客户端回调功能的实现机制探讨(响应部分及可能的优化)
- WLAN通信基础——WLAN物理层通信技术
- gamma分布_深度学习需要掌握的 13 个概率分布(含代码)
- 使用Proteus 8.9仿真STM32F103流水灯实验
- 什么是炎症(inflammation)?抗生素?//2021-2-12
- 对单位cps和单位kc的理解
- ie11 html5播放器卡,GitHub - yangyin/html5-player: 基于react的h5播放器
- 未来的外科手术可能由气泡代劳
- faster-rcnn参数介绍
- 1.1你是谁?你从哪来?你到哪去? -交给学习来解答
- oracle取去年的最后一天,oracle本月、上月、去年同月第一天最后一天
- 从零开始学微信小程序开发:1
- 怎么开通代付通道接口?