调用腾讯优图OCR手写体文字识别接口
一、百度搜索:腾讯优图·AI开放平台;
二、点击技术体验中心,可以体验一下效果;
三、点击开发者中心,左侧找到“API文档”里面的“通用手写体文字识别”。这里面有个“鉴权签名方法”的链接,点击进去按照步骤一步步来生成鉴权签名。首先登录open.youtu.qq.com,按照要求做就能获取AppID、SecretID和SecretKey;
四、新建一个Youtu.java,然后复制下面代码进去:
package com.youtu;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import com.youtu.sign.*;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class Youtu {
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public final static String API_YOUTU_END_POINT = "https://api.youtu.qq.com/youtu/";
public final static String API_YOUTU_CHARGE_END_POINT = "https://vip-api.youtu.qq.com/youtu/";
// 30 days
private static int EXPIRED_SECONDS = 2592000;
private String m_appid;
private String m_secret_id;
private String m_secret_key;
private String m_end_point;
private String m_user_id;
private boolean m_not_use_https;
/**
* PicCloud 构造方法
*
* @param appid
* 授权appid
* @param secret_id
* 授权secret_id
* @param secret_key
* 授权secret_key
*/
public Youtu(String appid, String secret_id, String secret_key,String end_point,String user_id) {
m_appid = appid;
m_secret_id = secret_id;
m_secret_key = secret_key;
m_end_point=end_point;
m_user_id=user_id;
m_not_use_https=!end_point.startsWith("https");
}
public String StatusText(int status) {
String statusText = "UNKOWN";
switch (status)
{
case 0:
statusText = "CONNECT_FAIL";
break;
case 200:
statusText = "HTTP OK";
break;
case 400:
statusText = "BAD_REQUEST";
break;
case 401:
statusText = "UNAUTHORIZED";
break;
case 403:
statusText = "FORBIDDEN";
break;
case 404:
statusText = "NOTFOUND";
break;
case 411:
statusText = "REQ_NOLENGTH";
break;
case 423:
statusText = "SERVER_NOTFOUND";
break;
case 424:
statusText = "METHOD_NOTFOUND";
break;
case 425:
statusText = "REQUEST_OVERFLOW";
break;
case 500:
statusText = "INTERNAL_SERVER_ERROR";
break;
case 503:
statusText = "SERVICE_UNAVAILABLE";
break;
case 504:
statusText = "GATEWAY_TIME_OUT";
break;
}
return statusText;
}
private void GetBase64FromFile(String filePath, StringBuffer base64)
throws IOException {
File imageFile = new File(filePath);
if (imageFile.exists()) {
InputStream in = new FileInputStream(imageFile);
byte data[] = new byte[(int) imageFile.length()]; // 创建合适文件大小的数组
in.read(data); // 读取文件中的内容到b[]数组
in.close();
base64.append(Base64Util.encode(data));
} else {
throw new FileNotFoundException(filePath + " not exist");
}
}
private JSONObject SendHttpRequest(JSONObject postData, String mothod)
throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
StringBuffer mySign = new StringBuffer("");
YoutuSign.appSign(m_appid, m_secret_id, m_secret_key,
System.currentTimeMillis() / 1000 + EXPIRED_SECONDS,
m_user_id, mySign);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
URL url = new URL(m_end_point + mothod);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// set header
connection.setRequestMethod("POST");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("user-agent", "youtu-java-sdk");
connection.setRequestProperty("Authorization", mySign.toString());
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "text/json");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
postData.put("app_id", m_appid);
out.write(postData.toString().getBytes("utf-8"));
//out.writeBytes(postData.toString());
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer resposeBuffer = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
resposeBuffer.append(lines);
}
// System.out.println(resposeBuffer+"\n");
reader.close();
// 断开连接
connection.disconnect();
JSONObject respose = new JSONObject(resposeBuffer.toString());
return respose;
}
private JSONObject SendHttpsRequest(JSONObject postData,String mothod)
throws NoSuchAlgorithmException, KeyManagementException,
IOException, JSONException {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
new java.security.SecureRandom());
StringBuffer mySign = new StringBuffer("");
YoutuSign.appSign(m_appid, m_secret_id, m_secret_key,
System.currentTimeMillis() / 1000 + EXPIRED_SECONDS,
m_user_id, mySign);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
URL url = new URL(m_end_point + mothod);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(sc.getSocketFactory());
connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
// set header
connection.setRequestMethod("POST");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("user-agent", "youtu-java-sdk");
connection.setRequestProperty("Authorization", mySign.toString());
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "text/json");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
postData.put("app_id", m_appid);
out.write(postData.toString().getBytes("utf-8"));
// 刷新、关闭
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer resposeBuffer = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
resposeBuffer.append(lines);
}
// System.out.println(resposeBuffer+"\n");
reader.close();
// 断开连接
connection.disconnect();
JSONObject respose = new JSONObject(resposeBuffer.toString());
return respose;
}
/**
* 识别手写签名
* @param image_path
* @return
* @throws IOException
* @throws JSONException
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
*/
public JSONObject PlateOcr2(String image_path) throws IOException,
JSONException, KeyManagementException, NoSuchAlgorithmException {
JSONObject data = new JSONObject();
StringBuffer image_data = new StringBuffer("");
GetBase64FromFile(image_path, image_data);
data.put("image", image_data);
JSONObject response = m_not_use_https ? SendHttpRequest(data, "ocrapi/handwritingocr") : SendHttpsRequest(data, "ocrapi/handwritingocr");
return response;
}
public JSONObject PlateOcrUrl2(String image_url) throws IOException,
JSONException, KeyManagementException, NoSuchAlgorithmException {
JSONObject data = new JSONObject();
data.put("url", image_url);
JSONObject response = m_not_use_https ? SendHttpRequest(data, "ocrapi/handwritingocr") : SendHttpsRequest(data, "ocrapi/handwritingocr");
return response;
}
/**
* 身份证识别
*/
public JSONObject IdCardOcr(String image_path,int card_type) throws IOException,
JSONException, KeyManagementException, NoSuchAlgorithmException {
StringBuffer image_data = new StringBuffer("");
JSONObject data = new JSONObject();
GetBase64FromFile(image_path, image_data);
data.put("image", image_data.toString());
data.put("card_type", card_type);
JSONObject respose =m_not_use_https?SendHttpRequest(data, "ocrapi/idcardocr"):SendHttpsRequest(data, "ocrapi/idcardocr");
return respose;
}
public JSONObject IdCardOcrUrl(String url,int card_type) throws IOException,
JSONException, KeyManagementException, NoSuchAlgorithmException {
JSONObject data = new JSONObject();
data.put("url", url);
data.put("card_type", card_type);
JSONObject respose =m_not_use_https?SendHttpRequest(data, "ocrapi/idcardocr"):SendHttpsRequest(data, "ocrapi/idcardocr");
return respose;
}
}
五、直接new 这个实例就可以调用了;
六、要识别的签字图片别忘了改变一下文字和纸张的比例,我试了好久找到一个识别率最高的比例:
七、压缩原有图片并将它附着在新的空白底页的方法:
public static final String overlapImage(String bigPath, String smallPath) {
try {
BufferedImage big = ImageIO.read(new File(bigPath));
BufferedImage small = ImageIO.read(new File(smallPath));
Graphics2D g = big.createGraphics();
int x = (big.getWidth() - small.getWidth()) / 26;
int y = (big.getHeight() - small.getHeight()) / 18;
g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
g.dispose();
ImageIO.write(big, "jpg", new File(smallPath));
return smallPath;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
调用腾讯优图OCR手写体文字识别接口相关推荐
- Android 腾讯优图 OCR 云平台识别身份证、银行卡、行驶证、驾驶证,依赖包小,识别次数免费
cardocr 项目地址:Eric0liang/cardocr 简介:Android 腾讯优图 OCR 云平台识别身份证.银行卡.行驶证.驾驶证,依赖包小,识别次数免费 更多:作者 提 Bug ...
- Python调用腾讯优图OCR通用API实现文字识别
API地址:https://ai.qq.com/doc/ocrgeneralocr.shtml 腾讯优图的API比较复杂的就是生成签名,不过不知道腾讯的服务器出什么问题了,调用的时候一直提示504,演 ...
- 百度大脑和腾讯云的OCR图片文字识别接口
百度大脑 通用文字识别: https://ai.baidu.com/tech/ocr/general 通用物体和场景识别:https://ai.baidu.com/tech/imagerecognit ...
- 腾讯优图实验室AI手语识别研究白皮书
前言 据2017年北京听力协会预估数据,我国听障人群数量约达到7200万.放眼世界,世界卫生组织发布的最新数据显示,全世界有共计约4.66亿人患有残疾性听力损失.尽管听障人群能够凭借手语进行交流,但在 ...
- 阿里云 OCR 图片文字识别接口使用案例(java)
阿里云 OCR 图片文字识别接口使用案例(java) 阿里云官方接口文档 前期需要完成 购买阿里云服务 购买服务 可以购买测试服务.每个阿里云用户可以购买1次免费的500次接口请求进行测试 购买完成之 ...
- Python调用腾讯优图进行人脸检测分析,并可视化
opexcel.py import xlrd import xlwt from xlutils.copy import copy class Operatingexcel():def get_exce ...
- 基于腾讯优图实现二代身份证识别
----------------- 2018.5.29 更新 ----------------- 添加腾讯ocr身份证识别,支持正反面,最新效果图见文章最后.替换新的图片选择框架,兼容7.0以上设备. ...
- 调用腾讯优图开放平台进行人脸识别-Java调用API实现
第一步:鉴权服务技术方案 Java代码实现如下 import java.util.Date; import com.baidu.aip.util.Base64Util; /** * 获取Authori ...
- 有道自然语言翻译和文字识别OCR(图片文字识别)接口调用
官网: ai.youdao.com 文档地址 :ai.youdao.com/docs/doc-oc- 在Python中调用api. #/usr/bin/env python #coding=utf8i ...
最新文章
- 基于Android移动终端的微型餐饮管理系统的设计与实现4——Android基础
- TCP/IP详解--学习笔记(12)-TCP的超时与重传
- c++游戏代码坦克大作战_一红一蓝多种模式的双人小游戏:红蓝大作战
- JAVA中几个常用的方法
- 显示器驱动有什么用_科普一下:电脑显示器用什么接口好,主流接口有哪些?...
- 小米全新5G旗舰手机即将登场 售价必将再创新高
- Spring boot Gradle项目搭建
- vpp flowprobe
- 计算机网络——应用层
- 用python实现文件加密功能
- 域名证书是什么样子的_什么是网站域名证书
- html 自动切换tab栏,html 实现tab切换的示例代码
- Navicat 15.0.27 激活时弹出No All Pattern Found File Already Patched?(已解决)
- SQL实际问题——列的替换和汇率打折问题
- IDEA自动补全tab键向下选择s-tab向上选择
- linux怎么发送邮件到qq邮箱,centos7命令行下用QQ邮箱发送邮件教程
- oracle mysql limit用法_mysql以及oracle数据分页的sql示例(limit和Rownum的用法) | Soo Smart!...
- 每日英语:China's Conflicted Consumers
- QS世界大学 计算机科学与信息系统学科排名!中国高校表现如何?
- 人工智能筑牢建筑工地疫情防控和工程质量安全双防线