package test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
/**手写文字识别WebAPI接口调用示例接口文档(必看):https://doc.xfyun.cn/rest_api/%E6%89%8B%E5%86%99%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB.html
*图片属性:jpg/png/bmp,最短边至少15px,最长边最大4096px,编码后大小不超过4M,识别文字语种:中英文
*webapi OCR服务参考帖子(必看):http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=39111&highlight=OCR
*(Very Important)创建完webapi应用添加服务之后一定要设置ip白名单,找到控制台--我的应用--设置ip白名单,如何设置参考:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=41891
*错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)
*@author iflytek
*/
public class TestWebOcr {
// 手写文字识别webapi接口地址
private static final String WEBOCR_URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting";
// 应用APPID(必须为webapi类型应用,并开通手写文字识别服务,参考帖子如何创建一个webapi应用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)
private static final String TEST_APPID = "*****";
// 接口密钥(webapi类型应用开通手写文字识别后,控制台--我的应用---手写文字识别---相应服务的apikey)
private static final String TEST_API_KEY = "*****";
// 测试图片文件存放位置
private static final String IMAGE_FILE_PATH = "E://1.jpg";
/**
* 组装http请求头
*
* @param aue
* @param engineType
* @return
* @throws UnsupportedEncodingException
* @throws ParseException
*/
private static Map<String, String> constructHeader(String language, String location) throws UnsupportedEncodingException, ParseException {
// 系统当前时间戳
String X_CurTime = System.currentTimeMillis() / 1000L + "";
// 业务参数
String param = "{\"language\":\""+language+"\""+",\"location\":\"" + location + "\"}";
String X_Param = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
// 接口密钥
String apiKey = TEST_API_KEY;
// 讯飞开放平台应用ID
String X_Appid = TEST_APPID;
// 生成令牌
String X_CheckSum = DigestUtils.md5Hex(apiKey + X_CurTime + X_Param);
// 组装请求头
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
header.put("X-Param", X_Param);
header.put("X-CurTime", X_CurTime);
header.put("X-CheckSum", X_CheckSum);
header.put("X-Appid", X_Appid);
return header;
}
public static void main(String[] args) throws IOException,ParseException{
Map<String, String> header = constructHeader("en", "false");
// 读取图像文件,转二进制数组,然后Base64编码
byte[] imageByteArray = FileUtil.read2ByteArray(IMAGE_FILE_PATH);
String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
String bodyParam = "image=" + imageBase64;
String result = HttpUtil.doPost(WEBOCR_URL, header, bodyParam);
System.out.println(result);
}
}
信息处理技术应用《手写文字识别》软件代码
需积分: 5 198 浏览量
2023-12-19
11:41:08
上传
评论
收藏 7.2MB ZIP 举报
STM32单片机定制
- 粉丝: 1w+
- 资源: 433
最新资源
- 基于Javascript的影视动画设计源码 - cad
- 基于Java和深度学习的瓦斯浓度预测系统后端设计源码 - 瓦斯浓度预测后端
- Screenshot_20240528_103010.jpg
- 基于Python的新能源承载力计算及界面设计源码 - HAINING-DG
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈