0


SpringBoot项目(百度AI整合)——如何在Springboot中使用文字识别OCR入门

在这里插入图片描述

前言

前言:本系列博客尝试结合官网案例,阐述百度 AI 开放平台里的组件使用方式,核心是如何在spring项目中快速上手应用。

本文介绍如何在Springboot中使用百度AI的文字识别OCR

在这里插入图片描述

其他相关的使用百度AI的文章列表如下:

如何在Springboot中使用语音文件识别 & ffmpeg的安装和使用

在这里插入图片描述

文章目录

引出


1.从官网demo到idea中使用;
2.如何阅读官网的说明文档,小经验分享;

在这里插入图片描述

小经验:如何使用官方文档

https://ai.baidu.com/ai-doc/index/OCR

https://ai.baidu.com/ai-doc/OCR/Ek3h7xypm

在这里插入图片描述

1.API文档的使用

万里长征第一步,Ctrl c + v,复制粘贴

在这里插入图片描述

2.HTTP-SDK文档的使用

网络请求SDK案例

在这里插入图片描述

基于官网案例demo的实现

从官网的案例到spring项目整合

在这里插入图片描述

1.使用AipOcr客户端

BaiduOcrPro实体类

packagecom.tianju.config.baidu;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;/**
 * OCR相关的配置
 */@Component@ConfigurationProperties(prefix ="baidu.ocr")@PropertySource("classpath:config/baiduAip.properties")@Data@NoArgsConstructor@AllArgsConstructorpublicclassBaiduOcrPro{privateString appId;privateString apiKey;privateString secretKey;}

初始化AipOcr,放到spring容器中

packagecom.tianju.config.baidu;importcom.baidu.aip.ocr.AipOcr;importcom.baidu.aip.speech.AipSpeech;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/**
 * 百度相关的配置文件
 */@ConfigurationpublicclassBaiduConfig{@AutowiredprivateBaiduOcrPro baiduOcrPro;/**
     * 图像相关的 AipOcr
     * @return AipOcr 放容器中
     */@BeanpublicAipOcraipOcr(){AipOcr aipOcr =newAipOcr(baiduOcrPro.getAppId(),
                baiduOcrPro.getApiKey(),
                baiduOcrPro.getSecretKey());// 可选:设置网络连接参数
        aipOcr.setConnectionTimeoutInMillis(2000);
        aipOcr.setSocketTimeoutInMillis(60000);return aipOcr;}}

controller层进行调用

packagecom.tianju.config.controller;importcom.baidu.aip.ocr.AipOcr;importcom.tianju.config.resp.HttpResp;importcom.tianju.config.util.baidu.Base64Util;importcom.tianju.config.util.baidu.FileUtil;importcom.tianju.config.util.baidu.HttpUtil;importorg.json.JSONArray;importorg.json.JSONObject;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.net.URLEncoder;importjava.util.HashMap;@RestController@RequestMapping("/api/baidu/ocr")publicclassBaiduOCRController{@AutowiredprivateAipOcr aipOcr;// http://124.70.138.34:9000/hello/1.jpg@GetMapping("/imgUrl")publicHttpRespocrFromImgUrl(String imgUrl){// 传入可选参数调用接口HashMap<String,String> options =newHashMap<String,String>();
        options.put("language_type","CHN_ENG");
        options.put("detect_direction","true");
        options.put("detect_language","true");
        options.put("probability","true");/**
         * 网络图像
         */JSONObject res = aipOcr.basicGeneralUrl(
                imgUrl,
                options
        );/**
         * {"words_result":
         * [{"probability":{"average":0.9994496107,"min":0.9990026355,"variance":1.469044975E-7},
         *  "words":"爱我中华"}],
         * "log_id":1705920508293856573,"words_result_num":1,"language":3,"direction":0}
         */JSONArray wordsResult =(org.json.JSONArray)res.get("words_result");JSONObject o =(JSONObject) wordsResult.get(0);Object words = o.get("words");System.out.println(words);System.out.println("######################");System.out.println(res.toString(2));returnHttpResp.success(words);}}

在这里插入图片描述

2.使用官网的HttpUtil工具类

packagecom.tianju.config.controller;importcom.baidu.aip.ocr.AipOcr;importcom.tianju.config.resp.HttpResp;importcom.tianju.config.util.baidu.Base64Util;importcom.tianju.config.util.baidu.FileUtil;importcom.tianju.config.util.baidu.HttpUtil;importorg.json.JSONArray;importorg.json.JSONObject;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.net.URLEncoder;importjava.util.HashMap;@RestController@RequestMapping("/api/baidu/ocr")publicclassBaiduOCRController{/**
     * 以下为官网的案例,token的方式
     * https://ai.baidu.com/ai-doc/OCR/zk3h7xz52
     */publicstaticStringgeneralBasic(){// 请求urlString url ="https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";try{// 本地文件路径String filePath ="D:\\Myprogram\\springboot-workspace\\spring-project\\baidu-api\\src\\main\\resources\\static\\ocr_test.jpg";byte[] imgData =FileUtil.readFileByBytes(filePath);String imgStr =Base64Util.encode(imgData);String imgParam =URLEncoder.encode(imgStr,"UTF-8");String param ="image="+ imgParam;System.out.println(param);// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。String accessToken ="24.2f4d3e23a805ba89627472c38addcdcd.2592000.1698147302.282335-38781099";String result =HttpUtil.post(url, accessToken, param);System.out.println(result);return result;}catch(Exception e){
            e.printStackTrace();}returnnull;}publicstaticvoidmain(String[] args){generalBasic();}}

在这里插入图片描述

附录:官网的工具类

1.Base64Util图片编码工具

packagecom.tianju.config.util.baidu;/**
 * Base64 工具类
 */publicclassBase64Util{privatestaticfinalchar last2byte =(char)Integer.parseInt("00000011",2);privatestaticfinalchar last4byte =(char)Integer.parseInt("00001111",2);privatestaticfinalchar last6byte =(char)Integer.parseInt("00111111",2);privatestaticfinalchar lead6byte =(char)Integer.parseInt("11111100",2);privatestaticfinalchar lead4byte =(char)Integer.parseInt("11110000",2);privatestaticfinalchar lead2byte =(char)Integer.parseInt("11000000",2);privatestaticfinalchar[] encodeTable =newchar[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};publicBase64Util(){}publicstaticStringencode(byte[] from){StringBuilderto=newStringBuilder((int)((double) from.length *1.34D)+3);int num =0;char currentByte =0;int i;for(i =0; i < from.length;++i){for(num %=8; num <8; num +=6){switch(num){case0:
                        currentByte =(char)(from[i]& lead6byte);
                        currentByte =(char)(currentByte >>>2);case1:case3:case5:default:break;case2:
                        currentByte =(char)(from[i]& last6byte);break;case4:
                        currentByte =(char)(from[i]& last4byte);
                        currentByte =(char)(currentByte <<2);if(i +1< from.length){
                            currentByte =(char)(currentByte |(from[i +1]& lead2byte)>>>6);}break;case6:
                        currentByte =(char)(from[i]& last2byte);
                        currentByte =(char)(currentByte <<4);if(i +1< from.length){
                            currentByte =(char)(currentByte |(from[i +1]& lead4byte)>>>4);}}to.append(encodeTable[currentByte]);}}if(to.length()%4!=0){for(i =4-to.length()%4; i >0;--i){to.append("=");}}returnto.toString();}}

2.FileUtil读取文件工具类

packagecom.tianju.config.util.baidu;importjava.io.*;/**
 * 文件读取工具类
 */publicclassFileUtil{/**
     * 读取文件内容,作为字符串返回
     */publicstaticStringreadFileAsString(String filePath)throwsIOException{File file =newFile(filePath);if(!file.exists()){thrownewFileNotFoundException(filePath);}if(file.length()>1024*1024*1024){thrownewIOException("File is too large");}StringBuilder sb =newStringBuilder((int)(file.length()));// 创建字节输入流  FileInputStream fis =newFileInputStream(filePath);// 创建一个长度为10240的Bufferbyte[] bbuf =newbyte[10240];// 用于保存实际读取的字节数  int hasRead =0;while((hasRead = fis.read(bbuf))>0){  
            sb.append(newString(bbuf,0, hasRead));}  
        fis.close();return sb.toString();}/**
     * 根据文件路径读取byte[] 数组
     */publicstaticbyte[]readFileByBytes(String filePath)throwsIOException{File file =newFile(filePath);if(!file.exists()){thrownewFileNotFoundException(filePath);}else{ByteArrayOutputStream bos =newByteArrayOutputStream((int) file.length());BufferedInputStream in =null;try{
                in =newBufferedInputStream(newFileInputStream(file));short bufSize =1024;byte[] buffer =newbyte[bufSize];int len1;while(-1!=(len1 = in.read(buffer,0, bufSize))){
                    bos.write(buffer,0, len1);}byte[] var7 = bos.toByteArray();return var7;}finally{try{if(in !=null){
                        in.close();}}catch(IOException var14){
                    var14.printStackTrace();}

                bos.close();}}}}

3.基于Google的gson的Json工具类

/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */packagecom.tianju.config.util.baidu;importcom.google.gson.Gson;importcom.google.gson.GsonBuilder;importcom.google.gson.JsonParseException;importjava.lang.reflect.Type;/**
 * Json工具类.
 */publicclassGsonUtils{privatestaticGson gson =newGsonBuilder().create();publicstaticStringtoJson(Object value){return gson.toJson(value);}publicstatic<T>TfromJson(String json,Class<T> classOfT)throwsJsonParseException{return gson.fromJson(json, classOfT);}publicstatic<T>TfromJson(String json,Type typeOfT)throwsJsonParseException{return(T) gson.fromJson(json, typeOfT);}}

4.Http请求发起和获得响应工具类

packagecom.tianju.config.util.baidu;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.List;importjava.util.Map;/**
 * http 工具类
 */publicclassHttpUtil{publicstaticStringpost(String requestUrl,String accessToken,String params)throwsException{String contentType ="application/x-www-form-urlencoded";returnHttpUtil.post(requestUrl, accessToken, contentType, params);}publicstaticStringpost(String requestUrl,String accessToken,String contentType,String params)throwsException{String encoding ="UTF-8";if(requestUrl.contains("nlp")){
            encoding ="GBK";}returnHttpUtil.post(requestUrl, accessToken, contentType, params, encoding);}publicstaticStringpost(String requestUrl,String accessToken,String contentType,String params,String encoding)throwsException{String url = requestUrl +"?access_token="+ accessToken;returnHttpUtil.postGeneralUrl(url, contentType, params, encoding);}publicstaticStringpostGeneralUrl(String generalUrl,String contentType,String params,String encoding)throwsException{URL url =newURL(generalUrl);// 打开和URL之间的连接HttpURLConnection connection =(HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");// 设置通用的请求属性
        connection.setRequestProperty("Content-Type", contentType);
        connection.setRequestProperty("Connection","Keep-Alive");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);// 得到请求的输出流对象DataOutputStream out =newDataOutputStream(connection.getOutputStream());
        out.write(params.getBytes(encoding));
        out.flush();
        out.close();// 建立实际的连接
        connection.connect();// 获取所有响应头字段Map<String,List<String>> headers = connection.getHeaderFields();// 遍历所有的响应头字段for(String key : headers.keySet()){System.err.println(key +"--->"+ headers.get(key));}// 定义 BufferedReader输入流来读取URL的响应BufferedReader in =null;
        in =newBufferedReader(newInputStreamReader(connection.getInputStream(), encoding));String result ="";String getLine;while((getLine = in.readLine())!=null){
            result += getLine;}
        in.close();System.err.println("result:"+ result);return result;}}

总结

1.从官网demo到idea中使用;
2.如何阅读官网的说明文档,小经验分享;


本文转载自: https://blog.csdn.net/Pireley/article/details/133251387
版权归原作者 Perley620 所有, 如有侵权,请联系我们删除。

“SpringBoot项目(百度AI整合)——如何在Springboot中使用文字识别OCR入门”的评论:

还没有评论