在做一个项目时候需要录入一些信息到数据库,所以使用了百度的文字识别接口,进行文字识别然后分析相关信息录入到数据库。
首先,想要使用需要先去网站申请注册

然后创建需要的应用

然后管理应用就能看到
其中AppID,APIkey,SecretKey 都是需要用到的。
相关文档
我用的是springboot 所以使用pom文件引入相关jar包,
1.引入jar包
<!--百度识别-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.12.0</version>
</dependency>
2.在配置文件中配置好参数

3.封装成工具类
@Component
public class PictureRecognition2Words {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Value("${AppID}")
private String APP_ID;
@Value("${ApiKey}")
private String API_KEY;
@Value("${SecretKey}")
private String SECRET_KEY;
private AipOcr client=null;
// 初始化用户对象
public AipOcr init() {
// 初始化一个AipOcr
if(client==null) {
client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
}else {
return this.client;
}
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
public String getPictureWords(String url) {
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("paragraph", "false");
options.put("probability", "false");
// 参数为本地图片路径
JSONObject res;
if(url.indexOf("http")==0){
res=client.basicGeneralUrl(url,options);
}else {
res = client.basicAccurateGeneral(url, options);
}
return res.toString();
}
}
通过发送请求方式调用

识别图片为Dnf装备图片,

最后识别结果:

如何在idea发送请求?
这样就将相关的文字提取出来,然后进行相关的字段分析提取出来关键字 然后将需要的数据录入进数据库就完成了我的相关需求。
欢迎大家指出问题,共同进步。
982





