背景:日常个人学习或者公司业务中,需要将书籍或者纸质文件转换成电子文稿,最近刚刚做完这一功能,在这里分享给大家
由于本人主要是做后台数据开发,所以主要讲解后台代码,如有不足,还请多喷
首先,在文字识别这一块,百度是做的比较好的,我们就以他为例,每天可以免费使用500次,作为普通人的日常使用,足矣 。如果你是企业使用,那就乖乖给钱吧步骤一:到百度云申请AppID 以及secret id,如有不会,请自行摆渡哈
步骤二:再根据获得的appID 以及secret id,获得token,代码如下:
import org.json.JSONObject;import JAVA.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.Map;/** * 获取token类 */public class AuthService {public static void main(String[] args) {getAuth();}/*** 获取权限token* @return 返回示例:* {* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",* "expires_in": 2592000* }*/public static String getAuth() {// 官网获取的 API Key 更新为你注册的String clientId = "KxiVCwBc7T9UU9C9p8qSNqlb";// 官网获取的 Secret Key 更新为你注册的String clientSecret = "WcT9Zx5G5XFZ5GyRvuLGZ0nmEfRf5pzp";return getAuth(clientId, clientSecret);}/*** 获取API访问token* 该token有一定的有效期,需要自行管理,当失效时需重新获取.* @param ak - 百度云官网获取的 API Key* @param sk - 百度云官网获取的 Secret Key* @return assess_token 示例:* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"*/public static String getAuth(String ak, String sk) {// 获取token地址String authHost = "https://aip.baidubce.com/oauth/2.0/token?";String getAccessTokenUrl = authHost// 1. grant_type为固定参数+ "grant_type=client_credentials"// 2. 官网获取的 API Key+ "&client_id=" + ak// 3. 官网获取的 Secret Key+ "&client_secret=" + sk;try {URL realUrl = new URL(getAccessTokenUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();connection.setRequestMethod("GET");connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 遍历所有的响应头字段for (String key : map.keySet()) {System.err.println(key + "--->" + map.get(key));}// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String result = "";String line;while ((line = in.readLine()) != null) {result += line;}/*** 返回结果示例*/System.err.println("result:" + result);JSONObject jsonObject = new JSONObject(result);String access_token = jsonObject.getString("access_token");return access_token;} catch (Exception e) {System.err.printf("获取token失败!");e.printStackTrace(System.err);}return null;}}
步骤三:有了以上两个步骤,就可以写代码进行识别啦,话不多说,直接上代码
import java.net.URLEncoder;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestPart;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import org.springframework.web.servlet.ModelAndView;import static com.lin.practice.voice_announcements.VoiceAnnouncements.read;@Controllerpublic class ImgController {//public static void main(String[] args) {//test();// }@ResponseBody@RequestMapping(value = https://www.isolves.com/it/ai/2020-08-12/"/ocr", method = RequestMethod.POST)public JSONObject test(@RequestPart(value = "file", required = false) MultipartFile file) {ModelAndView mav = new ModelAndView("index");//public static JSONObject test(){// 请求urlString url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";//https://aip.baidubce.com/rest/2.0/ocr/v1/idcard,https://aip.baidubce.com/rest/2.0/ocr/v1/driving_licenseString result = "";JSONObject parseobject = null;try {// 本地文件路径//String filePath = "C:/Users/mayn/Desktop/1596726642(1).png";byte[] imgData = file.getBytes();//byte[] imgData = FileUtil.readFileByBytes(filePath);String imgStr = Base64Util.encode(imgData);String imgParam = URLEncoder.encode(imgStr, "UTF-8");String param = "image=" + imgParam;//String param = "image=" + imgParam+"&id_card_side=front";// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取 。String accessToken = "24.25af24d873386e07025e113c44750dc1.2592000.1599316725.282335-21835341";result = HttpUtil.post(url, accessToken, param);parseObject = JSONArray.parseObject(result);System.out.println(result);read(result);} catch (Exception e) {e.printStackTrace();}return parseObject;}}
推荐阅读
- JAVA程序员常用的几个工具类
- JAVA并发-AtomicInteger
- Java正则表达式详解
- 美团对 Java 新一代垃圾回收器 ZGC 的探索与实践
- java中的装箱和拆箱
- 2019年的6个JavaScript用户认证库
- 又一个小而美的Java Web框架:Solon
- 万字详文:Java内存泄漏、性能优化、宕机死锁的N种姿势
- javascript 知识普及之 FormData
- 超过35%的德国中小企业已使用人工智能技术