java文字手写识别_【手写文字识别】-JavaAPI示例代码

手写文字识别-JavaAPI示例代码

不知不觉手写文字识别百度已经开始邀测了。需要的小伙伴去申请了哦。申请方式加入文字识别群找PM。或者工单提交申请。都要说明自己的APPID哦。

接口地址:https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting

-----------------------------------------------------下面开始代码-----------------------------------------------------

手写文字识别-示例代码

import java.net.URLEncoder;

import com.baidu.aip.util.Base64Util;

import com.xiaoshuai.baidu.util.FileUtil;

import com.xiaoshuai.baidu.util.HttpUtil;

/**

* @author 小帅丶

* @类名称 HandWriteTest

* @remark 手写文字接口示例代码 只返回中英文及部分符合正则的内容

* @date 2018-1-26

*/

public class HandWriteTest {

public static void main(String[] args) throws Exception {

String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting";

String accessToken = "自己的accestoken";

byte[] imageData = FileUtil.readFileByBytes("C:/Users/Administrator/Desktop/xs2.jpg");

String img64 = Base64Util.encode(imageData);

String param ="image="+URLEncoder.encode(img64,"UTF-8");

String object = HttpUtil.post(url, accessToken, param);

HandWriteBean bean = JSONObject.parseObject(object, HandWriteBean.class);

String regex = "^[..,,。!a-zA-Z0-9_\u4e00-\u9fa5]*$";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = null;

String text ="";

List list = bean.getWords_result();

for (int i = 0; i < list.size(); i++) {

matcher = pattern.matcher(list.get(i).getWords());

while (matcher.find()) {

text +=matcher.group();

}

}

System.out.println("识别的内容是:"+text);

}

}

手写文字识别-所需要的Java对象

package com.xiaoshuai.baidu.ocr;

import java.util.List;

/**

* @author 小帅丶

* @类名称 HandWriteBean

* @remark

* @date 2018-1-27

*/

public class HandWriteBean {

private long log_id;

private int words_result_num;

private List words_result;

/**

* @return the log_id

*/

public long getLog_id() {

return log_id;

}

/**

* @param log_id

* log_id

*/

public void setLog_id(long log_id) {

this.log_id = log_id;

}

/**

* @return the words_result_num

*/

public int getWords_result_num() {

return words_result_num;

}

/**

* @param words_result_num

* words_result_num

*/

public void setWords_result_num(int words_result_num) {

this.words_result_num = words_result_num;

}

/**

* @return the words_result

*/

public List getWords_result() {

return words_result;

}

/**

* @param words_result

* words_result

*/

public void setWords_result(List words_result) {

this.words_result = words_result;

}

public static class Words_result{

private Location location;

private String words;

/**

* @return the location

*/

public Location getLocation() {

return location;

}

/**

* @param location

* location

*/

public void setLocation(Location location) {

this.location = location;

}

/**

* @return the words

*/

public String getWords() {

return words;

}

/**

* @param words

* words

*/

public void setWords(String words) {

this.words = words;

}

}

public static class Location{

private int width;

private int top;

private int left;

private int height;

/**

* @return the width

*/

public int getWidth() {

return width;

}

/**

* @param width

* width

*/

public void setWidth(int width) {

this.width = width;

}

/**

* @return the top

*/

public int getTop() {

return top;

}

/**

* @param top

* top

*/

public void setTop(int top) {

this.top = top;

}

/**

* @return the left

*/

public int getLeft() {

return left;

}

/**

* @param left

* left

*/

public void setLeft(int left) {

this.left = left;

}

/**

* @return the height

*/

public int getHeight() {

return height;

}

/**

* @param height

* height

*/

public void setHeight(int height) {

this.height = height;

}

}

}

手写文字识别-返回的JSON字符串(全部内容)

因为文档还没有。不太确定参数是什么。默认只传递到了image参数。会对图片上的横线也做了识别。

{"log_id": 8502255431261249697, "words_result_num": 11, "words_result": [{"location": {"width": 323, "top": 20, "left": 5, "height": 18}, "words": "………………….…………………………………………"}, {"location": {"width": 1041, "top": 25, "left": 347, "height": 41}, "words": "……………………………………………………………………………………………………………………………………………………………………………………………………"}, {"location": {"width": 944, "top": 159, "left": 2, "height": 39}, "words": "………………………"}, {"location": {"width": 438, "top": 176, "left": 981, "height": 25}, "words": "…………………………………:*"}, {"location": {"width": 243, "top": 298, "left": 2, "height": 23}, "words": "…………………………………………………………."}, {"location": {"width": 436, "top": 309, "left": 266, "height": 20}, "words": "……………………………………"}, {"location": {"width": 729, "top": 314, "left": 698, "height": 23}, "words": "………………………………………………"}, {"location": {"width": 233, "top": 588, "left": 5, "height": 22}, "words": "…………"}, {"location": {"width": 692, "top": 454, "left": 366, "height": 198}, "words": "开发者小帅"}, {"location": {"width": 398, "top": 732, "left": 423, "height": 15}, "words": "………………,………………………………………………………………………………"}, {"location": {"width": 596, "top": 862, "left": 840, "height": 19}, "words": "……………………………………"}]}

手写文字识别-返回的JSON字符串(图片文字内容)

{

"log_id": 8502255431261250000,

"words_result_num": 11,

"words_result": [

{

"location": {

"width": 692,

"top": 454,

"left": 366,

"height": 198

},

"words": "开发者小帅"

}

]

}

手写文字识别-测试图片

391d71d039c4c08260200b75026aba33.png

是不是发现识别还是蛮不错的。相当准确。当然前提是作者写的字不错了。哈哈。

过于潦草的也进行了测试。准确率在75%左右

这周我的小程序也争取更新加入手写文字识别功能,敬请期待。

fe759709a9707d45849890c9b475898a.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值