真厲害!Java代碼實(shí)現(xiàn)圖片提取文字功能
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
摘要
一、tesseract-ocr介紹
二、安裝tesseract
三、使用命令行
四、程序?qū)崿F(xiàn)(Python)
#?coding=utf-8
from?flask?import?Flask,?request
import?os
import?datetime
import?time
app?=?Flask(__name__)
def?get_time_stamp():
????times?=?datetime.datetime.now().strftime('%Y-%m-%d?%H:%M:%S')
????array?=?time.strptime(times,?"%Y-%m-%d?%H:%M:%S")
????time_stamp?=?int(time.mktime(array))
????return?time_stamp
@app.route('/image/extract',?methods=['POST'])
def?pure_rec():
????file?=?request.files.get('file')
????ts?=?str(get_time_stamp())
????up_path?=?os.path.join(ts?+?file.filename)
????file.save(up_path)
????cmd?=?"tesseract?"+up_path+"?"?+?ts?+?"?-l?chi_sim"
????print(cmd)
????os.system(cmd)
????with?open(ts+".txt",?'r+',?encoding="utf-8")?as?f:
????????result?=?f.read()
????????return?result
if?__name__?==?'__main__':
????app.run(debug=True)
五、程序?qū)崿F(xiàn)(Java)
package?com.lbh.web.controller;
/*
?*?Copyright@[email protected]
?*?Author:liubinhao
?*?Date:2020/11/23
?*?++++?______?@author???????liubinhao???______?????????????______
?*?+++/?????/|?????????????????????????/?????/|???????????/?????/|
?*?+/_____/??|???????????????????????/_____/??|?????????/_____/??|
?*?|?????|???|??????????????????????|?????|???|????????|?????|???|
?*?|?????|???|??????????????????????|?????|???|________|?????|???|
?*?|?????|???|??????????????????????|?????|??/?????????|?????|???|
?*?|?????|???|??????????????????????|?????|/___________|?????|???|
?*?|?????|???|___________________???|?????|____________|?????|???|
?*?|?????|??/??????????????????/?|??|?????|???|????????|?????|???|
?*?|?????|/?_________________/??/???|?????|??/?????????|?????|??/
?*?|_________________________|/b????|_____|/???????????|_____|/
?*/
import?org.springframework.web.bind.annotation.PostMapping;
import?org.springframework.web.bind.annotation.RequestParam;
import?org.springframework.web.bind.annotation.RestController;
import?org.springframework.web.multipart.MultipartFile;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.IOException;
import?java.io.InputStreamReader;
@RestController
public?class?LiteralExtractController?{
????@PostMapping("/image/extract")
????public?String?reg(@RequestParam("file")MultipartFile?file)?throws?IOException?{
????????String?result?=?"";
????????String?filename?=?file.getOriginalFilename();
????????File?save?=?new?File(System.getProperty("user.dir")+"\\"+filename);
????????if?(!save.exists()){
????????????save.createNewFile();
????????}
????????file.transferTo(save);
????????String?cmd?=?String.format("tesseract?%s?stdout?-l?%s",System.getProperty("user.dir")+"\\"+filename,"chi_sim");
????????result?=?cmd(cmd);
????????return?result;
????}
????public?static?String?cmd(String?cmd)?{
????????BufferedReader?br?=?null;
????????try?{
????????????Process?p?=?Runtime.getRuntime().exec(cmd);
????????????br?=?new?BufferedReader(new?InputStreamReader(p.getInputStream()));
????????????String?line?=?null;
????????????StringBuilder?sb?=?new?StringBuilder();
????????????while?((line?=?br.readLine())?!=?null)?{
????????????????sb.append(line?+?"\n");
????????????}
????????????return?sb.toString();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????finally
????????{
????????????if?(br?!=?null)
????????????{
????????????????try?{
????????????????????br.close();
????????????????}?catch?(Exception?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
????????return?null;
????}
}
六、實(shí)驗(yàn)測(cè)試
七、總結(jié)
? 作者?|??興趣使然的程序猿
來源 |??csdn.net/weixin_44671737/article/details/110000864

加鋒哥微信:?java3459?? 圍觀鋒哥朋友圈,每天推送Java干貨!
評(píng)論
圖片
表情









