简要
使用腾讯云OCR文字识别中的通用文字识别来实现识别简历pdf文件随后获取简历pdf上的文字
起步工作
首先在腾讯云中进行登录以及注册,随后进入到腾讯云OCR的快速入门教学(链接:文字识别 一分钟接入服务端 API_腾讯云),跟随教学开通文字识别服务,进入文字识别控制台开通,获取到免费的额度

随后跟随教学页面,获取到我们主要需要的两个参数,文字识别api的sercrekey以及sercrevalue
代码编写
配置文件编写
获取到腾讯OCRapi的secrekey以及sercrevalue之后,我们首先将获取到的这两个写入到配置文件application.yml中,里面放入自己的api的对应的的两个值
java
TENCENT:
TENCENT_SERCREKEY: "xxxxxxxxxxxxxxxxxxxxxxxx"
TENCENT_SERCREVALUE: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
工具类编写
在开始调用文字识别api的时候,我们需要先根据我们获取的api的key以及value来验证并创建认证对象,创建HTTP配置,创建客户端配置来创建OCR客户端实例(用来调用文字识别api)
java
package cn.enilu.flash.utils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.TextDetection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class OcrClientUtil {
@Value("${TENCENT.TENCENT_SERCREKEY}")
private String secretKey;
@Value("${TENCENT.TENCENT_SERCREVALUE}")
private String secretValue;
/**
* 创建OCR客户端
* @return OCR客户端实例
*/
public OcrClient createOcrClient() {
try {
// 创建认证对象,需要提供你api的key以及value
Credential cred = new Credential(
secretKey,
secretValue
);
// 创建HTTP配置
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
// 创建客户端配置
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 返回OCR客户端实例
return new OcrClient(cred, "", clientProfile);
} catch (Exception e) {
log.error("创建OCR客户端失败", e);
throw new RuntimeException("创建OCR客户端失败", e);
}
}
}
获取到了OCR客户端实例后,就能使用OCR客户端来调用通用文字识别接口,我们需要提供的是pdf文件的链接(注意是链接,不是文件,可以先将文件上传至OSS中获取文件链接)
java
public String userGeneralBasicOCR(String pdfUrl) {
OcrClient client = createOcrClient();
// 实例化一个请求对象,每个接口都会对应一个request对象
GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
if(pdfUrl == null){
throw new RuntimeException("pdfUrl不能为空");
}
req.setImageUrl(pdfUrl);
req.setLanguageType("zh");
req.setIsPdf(true);
GeneralBasicOCRResponse response=new GeneralBasicOCRResponse();
try{
response=client.GeneralBasicOCR(req);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
TextDetection[] textDetections = response.getTextDetections();
if (textDetections == null || textDetections.length == 0) {
throw new RuntimeException("简历信息为空");
}
StringBuilder detectedTextBuilder = new StringBuilder();
for (TextDetection textDetection : textDetections) {
detectedTextBuilder.append(textDetection.getDetectedText());
}
return detectedTextBuilder.toString();
}
代码解析
req.setImageUrl(pdfUrl);---》设置要进行文字识别文件的url
req.setLanguageType("zh");---》设置识别文字的语言类型,zh可以实现中英文的识别,想要更多的语言参数设置为auto,自动识别文字内容
GeneralBasicOCRResponse response=new GeneralBasicOCRResponse();-->获取到文字识别的结果
TextDetection[] textDetections = response.getTextDetections();-->获取到文字识别内容中的文字内容,文字识别的结果不仅仅包含文字,还有文字的坐标准确度等等,可以自行打印response查看有上面信息
测试类实验
java
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan(basePackages = "cn.enilu.flash.api")
public class OcrClientUtilTest {
@Autowired
private OcrClientUtil ocrClientUtil;
@Test
public void tryGetMessage(){
//可以不是jpg,也可以是pdf等等
String generalBasicOCRResponse = ocrClientUtil.userGeneralBasicOCR("xxxx.jpg");
//随后就是直接看答案
System.out.println(generalBasicOCRResponse);
}
}
实验对象

识别结果示例
老鱼简历J 12345678909 [email protected]在职资深
Java开发工程师老鱼简历教育经历清华大学计算机科学与技术2019-09 ~ 2023-07
全日制计算机学院专业技能Java熟练掌握Java 后端开发,包括Java 核心技术、Spring 框架、MyBatis 等数据库
老鱼简历老鱼简熟悉关系型数据库(如MySQL)和非关系型数据库(如MongoDB)
,能够编写高效的SQL查询语句网络编程具备网络编程基础,了解TCP/IP、HTTP协议,能够进行网络通信、接口调试等
工作经历ABC科技有限公司2020-01~ 2021-01软件开发部Java 后端开发工程师北京负责公司内部物流管理系统的开发和维护,参与需求分析、数据库设计、接口开发等工作,主要使用Java、Spring Boot、MySQL 等技术。解决了系统性能瓶颈问题,优化了查询速度,提升了用户体验。
XYZ软件公司2019-01~2020-01云计算部Java 开发工程师上海参与公司自研云平台的开发与维护,负责用户管理、权限控制、日志记录等功能模块的设计和实现。使用Java、Spring Cloud、MySQL 等技术,解决了平台稳定性和可扩展性的问题,提供了良好的用户体验。项目经历订单管理系统2021-03 ~ 2021-05
Java后端开发工程师北京老鱼简迂包主工首竺田亥弦的巨毕工尖句手工首净悠收本询竺陆此的迟斗n守现声田3
注意!
该文字识别只能实现识别一页pdf,如果需要识别多页pdf需要自己设计分页传输并且多次调用接口