背景:
日常個人學習或者公司業務中,需要將書籍或者紙質文件轉換成電子文稿,最近剛剛做完這一功能,在這里分享給大家
由于本人主要是做后臺數據開發,所以主要講解后臺代碼,如有不足,還請多噴
首先,在文字識別這一塊,百度是做的比較好的,我們就以他為例,每天可以免費使用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 = "/ocr", method = RequestMethod.POST)
public JSONObject test(@RequestPart(value = "file", required = false) MultipartFile file) {
ModelAndView mav = new ModelAndView("index");
//public static JSONObject test(){ // 請求url String 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_license String 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;
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/** * http 工具類 */public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/x-www-form-urlencoded";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(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 = new DataOutputStream(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 = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
}
import java.io.*;
/** * 文件讀取工具類 */public class FileUtil {
/** * 讀取文件內容,作為字符串返回 */ public static String readFileAsString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
if (file.length() > 1024 * 1024 * 1024) {
throw new IOException("File is too large");
}
StringBuilder sb = new StringBuilder((int) (file.length()));
// 創建字節輸入流 FileInputStream fis = new FileInputStream(filePath);
// 創建一個長度為10240的Buffer byte[] bbuf = new byte[10240];
// 用于保存實際讀取的字節數 int hasRead = 0;
while ( (hasRead = fis.read(bbuf)) > 0 ) {
sb.append(new String(bbuf, 0, hasRead));
}
fis.close();
return sb.toString();
}
/** * 根據文件路徑讀取byte[] 數組 */ public static byte[] readFileByBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
short bufSize = 1024;
byte[] buffer = new byte[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();
}
}
}
}
/** * Base64 工具類 */public class Base64Util {
private static final char last2byte = (char) Integer.parseInt("00000011", 2);
private static final char last4byte = (char) Integer.parseInt("00001111", 2);
private static final char last6byte = (char) Integer.parseInt("00111111", 2);
private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
private static final char[] encodeTable = new char[]{'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', '+', '/'};
public Base64Util() {
}
public static String encode(byte[] from) {
StringBuilder to = new StringBuilder((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) {
case 0:
currentByte = (char) (from[i] & lead6byte);
currentByte = (char) (currentByte >>> 2);
case 1:
case 3:
case 5:
default:
break;
case 2:
currentByte = (char) (from[i] & last6byte);
break;
case 4:
currentByte = (char) (from[i] & last4byte);
currentByte = (char) (currentByte << 2);
if (i + 1 < from.length) {
currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
}
break;
case 6:
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("=");
}
}
return to.toString();
}
}
下面給出語音播報內容:pom中添加依賴,如果不成功,請下載后,用maven自行導入
命令如下: mvn install:install-file -Dfile=cloud.jar(本地包名) -DgroupId=com.hope.cloud -DartifactId=cloud -Dversion=1.0 -Dpackaging=jar
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.10</version>
</dependency>
另外需要下載jacob-1.17-M2-x86.dll,并添加到JDK的bin目錄
下面上代碼:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import com.jacob.com.Dispatch;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;
/*將jacob-1.17-M2-x86.dll添加到JDK的bin目錄和windows的system32目錄(64位系統添加jacob-1.17-M2-x64.dll) * * * * */public class VoiceAnnouncements {
public static void main(String[] args) {
try {
StringBuilder stringbuilder = new StringBuilder();
//創建Scanner對象,接受從控制臺輸入 Scanner scanner = new Scanner(System.in);
String m = "-1";
//如果輸入的是“t”,則結束服務 while (!"t".equalsIgnoreCase(stringbuilder.toString())) {
System.out.println("請輸入需要語音播報的內容:");
read("請輸入需要語音播報的內容");
while (true) {
String text = scanner.nextLine().trim();
if ("".equals(text)) {
break;
}
stringbuilder.append(text);
}
if (stringbuilder.toString().endsWith("t")) {
System.out.println("感謝使用");
read("感謝使用");
break;
} else {
System.err.println("需要播報的輸入內容為:"+stringbuilder.toString());
//播報 read(stringbuilder.toString());
/** * 清空字符串 */ stringbuilder.delete(0, stringbuilder.length());
read("播報結束");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
/** * 根據字符串進行語音播報 * 問題:為什么方法為static * @param str */ public static void read(String str) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// 音量 0-100 sap.setProperty("Volume", new Variant(100));
// 語音朗讀速度 -10 到 +10 sap.setProperty("Rate", new Variant(0));
// 獲取執行對象 Dispatch sapo = sap.getObject();
// 執行朗讀 Dispatch.call(sapo, "Speak", new Variant(str));
/*// 關閉執行對象 sapo.safeRelease(); // 關閉應用程序連接 sap.safeRelease();*/ }
}通過以上步驟,應該可以正確運行文字識別,并語音播報了,似不似很方便。
通過以上步驟,應該可以正確運行文字識別,并語音播報了,似不似很方便。