文章目錄
-
- OCR效果
- 配置騰訊雲OCR準備工作
-
- 新增賬號
- 建立秘鑰
- 使用萬象優圖建立Bucket
- 操作指引
- 實作代碼
-
- 生成簽名
- 配置網絡請求,調用OCR識别接口
- 寫在後面
使用騰訊智能文字識别 OCR 對圖檔進行文字識别
前段時間有個項目需要一個圖檔識别轉換成文字的功能,後來考慮了一下選擇了騰訊雲的文字識别OCR。當時對接的過程中覺得有一些地方确實有些坑,是以再記錄一下,
避免以後忘記。也希望能給需要的朋友提供一些幫助。
OCR效果
可以參考一下騰訊雲官網的連結:文字識别OCR
配置騰訊雲OCR準備工作
新增賬號
我是直接通過QQ賬号進行注冊登入,大家也可以檢視騰訊雲官方教程進行注冊,注冊騰訊雲
建立秘鑰
建立新秘鑰,可能會彈出視窗提示你不安全,建立子使用者之類,這個看你個人需要,想要建立子使用者就可以建立,不想建立的話直接點解繼續使用即可。最後在左側菜單欄選擇雲API秘鑰->API秘鑰管理,點選 建立秘鑰 即可,記錄下對應的APPID、SecretId、SecretKey,在項目中需要的地方替換掉。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICNzMjNx0zcmcmbw1jZmAjMy0DamIDO20zd-MmMzI2Y0EDZ4QzMiVDO2EzLcdTMvwVMvwVOxAjMvw1bp5Sd0lGeu4GZj1CZs92ZtIXZzV3Lc9CX6MHc0RHaiojIsJye.jpg)
使用萬象優圖建立Bucket
- 在騰訊雲菜單中選擇萬象優圖(連結),點選 Bucket管理,之後點選頁面上的 綁定Bucket
- 會提示 該服務需要建立角色
- 點選 授權
使用騰訊OCR進行文字識别 - 之後繼續選擇 同意授權
- 之後會提示進行身份驗證,使用微信掃描即可,也可以選擇使用備選驗證方式等
- 再次點選頁面上的 綁定Bucket
- 新增方式選擇 建立
- 所屬項目不用改,直接用 預設項目
- 名稱自己命名即可,隻要符合規則,其餘沒什麼限制,記住這個名稱,之後在項目中會需要用到
- 其餘選項可以不需要改動
使用騰訊OCR進行文字識别
- 記住建立之後的bucket名稱,之後在項目中需要的地方替換掉
操作指引
如果上面的說明有比較模糊的地方,也可以參考騰訊雲官網的操作指引。
實作代碼
生成簽名
具體說明可以參考騰訊雲官網的說明:鑒權簽名,我這裡使用的java語言,是以直接使用的java簽名示例。
将官網給出的代碼拷貝到java檔案中即可,之後需要使用簽名的時候直接調用檔案中的appSign方法
配置網絡請求,調用OCR識别接口
這一步是當時我覺得比較麻煩的,因為這個接口拼起來有點費勁。并且目前效果是識别本地檔案
官方給出的文檔在這兒:OCR-通用印刷體識别,如果出現了一些錯誤也可以在這裡找對應的狀态碼檢視原因。
- 配置網絡連接配接的方法
/**
* 配置Connection對象
* @throws Exception
*/
private static HttpURLConnection handlerConnection(String path, String imageName) throws Exception {
URL url = new URL(URL);
// 擷取HttpURLConnection對象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); // 設定 Post 請求方式
connection.setDoOutput(true); // 允許輸出流
connection.setDoInput(true); // 允許輸入流
connection.setUseCaches(false); // 禁用緩存
// 設定請求頭
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
connection.setRequestProperty("authorization", sign());
connection.setRequestProperty("host", HOST);
System.out.println( "請求頭設定完成");
// 擷取HttpURLConnection的輸出流
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
StringBuffer strBufparam = new StringBuffer();
strBufparam.append(LINE_END);
// 封裝鍵值對資料參數
String inputPartHeader1 = "--" + BOUNDARY + LINE_END + "Content-Disposition:form-data;name=\""+ "appid" +"\";" + LINE_END + LINE_END + APPID + LINE_END;
String inputPartHeader2 = "--" + BOUNDARY + LINE_END + "Content-Disposition:form-data;name=\""+ "bucket" +"\";" + LINE_END + LINE_END + BUCKET + LINE_END;
strBufparam.append(inputPartHeader1);
strBufparam.append(inputPartHeader2);
// 拼接完成後,一起寫入
outputStream.write(strBufparam.toString().getBytes());
// 寫入圖檔檔案
String imagePartHeader = "--" + BOUNDARY + LINE_END +
"Content-Disposition: form-data; name=\"" + "image" + "\"; filename=\"" + imageName + "\"" + LINE_END +
"Content-Type: image/jpeg" + LINE_END + LINE_END;
byte[] bytes = imagePartHeader.getBytes();
outputStream.write(bytes);
// 擷取圖檔的檔案流
String imagePath = path + File.separator + imageName;
InputStream fileInputStream = getImgIns(imagePath);
byte[] buffer = new byte[1024*2];
int length = -1;
while ((length = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,length);
}
outputStream.flush();
fileInputStream.close();
// 寫入标記結束位
byte[] endData = ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + LINE_END + BOUNDARY + "--" + LINE_END).getBytes();//寫結束标記位
outputStream.write(endData);
outputStream.flush();
return connection;
}
- 部分用到的工具方法
/**
* 根據檔案名擷取檔案輸入流
* @throws FileNotFoundException
*/
private static InputStream getImgIns(String imagePath) throws FileNotFoundException {
File file = new File(imagePath);
FileInputStream is = new FileInputStream(file);
return is;
}
/**
* 把輸入流的内容轉化成字元串
* @param is
* @return
* @throws IOException
*/
public static String readInputStream(InputStream is) throws IOException{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int length=0;
byte[] buffer=new byte[1024];
while((length=is.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
is.close();
baos.close();
return baos.toString();
}
/**
* 簽名方法,調用Sign檔案中的appSign方法生成簽名
* @return 生成後的簽名
*/
public static String sign(){
long expired = 10000;
try {
return Sign.appSign(APPID, SECRET_ID, SECRET_KEY, BUCKET, expired);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
- 進行圖檔識别
/***
* 上傳圖檔進行識别
* @param urlStr 請求位址
* @param path 圖檔所在檔案夾的路徑
* @param imageName 圖檔名稱
*/
public void uploadImage(String path, String imageName) {
new Thread(){
@Override
public void run() {
try {
// 配置HttpURLConnection對象
HttpURLConnection connection = handlerConnection(path, imageName);
// 連接配接HttpURLConnection
connection.connect();
// 得到響應
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
String result = readInputStream(connection.getInputStream());//将流轉換為字元串。
System.out.println("請求成功:" + result);
} else {
String errorMsg = readInputStream(connection.getErrorStream());//将流轉換為字元串。
System.out.println("請求失敗:" + errorMsg);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println( "網絡請求出現異常: " + e.getMessage());
}
}
}.start();
}
寫在後面
- 源碼位址:TencentOCRDemo(歡迎Star,謝謝!)
- 使用Git下載下傳:
git clone https://github.com/beichensky/TencentOCRDemo.git
關于使用 Java 語言抵用騰訊雲文字OCR識别的方式,大概就在這裡了,如果有說的不好或者不清楚地地方,歡迎大家指正。