基於Tesseract-OCR實現的JAVA WEB版OCR(圖片轉文字)
接下來就是新建一個JAVA EE專案,把Tesseract-OCR放在專案WebRoot下。
下面是主要程式碼:
接受客戶端上傳過來的圖片,使用Tesseract-OCR識別後返回至前臺。
[java] view plain copy print?- package servlet;
- import java.io.IOException;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import
- import javax.servlet.http.HttpServletResponse;
- import util.FileUtil;
- import util.OCRUtil;
- import com.jspsmart.upload.File;
- import com.jspsmart.upload.SmartUpload;
- import com.jspsmart.upload.SmartUploadException;
- publicclass OCRServlet extends HttpServlet {
- publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setCharacterEncoding("gbk");
- SmartUpload upload = new SmartUpload();
- ServletConfig sc = this.getServletConfig();
- upload.initialize(sc, request, response);
- File file = null;
- long size = 5*1024*1024;
- upload.setAllowedFilesList("gif,jpg,bmp,png");
- upload.setMaxFileSize(size);
- upload.setCharset("GBK");
- try {
- upload.upload();
- file = upload.getFiles().getFile(0);
- String userPath = "upload\\"+request.getRemoteAddr().replaceAll("\\.", "")+"\\";
- String svpath = userPath+file.getFileName();
- if(!file.isMissing()){
- String realPath = request.getRealPath("/");
- FileUtil.creatPath(realPath+userPath);
- file.saveAs(svpath,SmartUpload.SAVE_VIRTUAL);
- try {
- OCRUtil.runOCR(realPath, realPath+svpath, realPath+userPath+"ocr",true);
- request.setAttribute("txt", FileUtil.read(realPath+userPath+"ocr.txt").trim());
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- } catch (Exception e) {
- e.printStackTrace();
- }
- FileUtil.delete(realPath+userPath);
- }
- } catch (SmartUploadException e) {
- e.printStackTrace();
- }
- }
- }
package servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.FileUtil;
import util.OCRUtil;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class OCRServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gbk");
SmartUpload upload = new SmartUpload();
ServletConfig sc = this.getServletConfig();
upload.initialize(sc, request, response);
File file = null;
long size = 5*1024*1024;
upload.setAllowedFilesList("gif,jpg,bmp,png");
upload.setMaxFileSize(size);
upload.setCharset("GBK");
try {
upload.upload();
file = upload.getFiles().getFile(0);
String userPath = "upload\\"+request.getRemoteAddr().replaceAll("\\.", "")+"\\";
String svpath = userPath+file.getFileName();
if(!file.isMissing()){
String realPath = request.getRealPath("/");
FileUtil.creatPath(realPath+userPath);
file.saveAs(svpath,SmartUpload.SAVE_VIRTUAL);
try {
OCRUtil.runOCR(realPath, realPath+svpath, realPath+userPath+"ocr",true);
request.setAttribute("txt", FileUtil.read(realPath+userPath+"ocr.txt").trim());
request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
FileUtil.delete(realPath+userPath);
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}
[java]
view plain
copy
print?
- package util;
- publicclass OCRUtil {
- publicstatic String chiSIM = "chi_sim";
- publicstaticvoid runOCR(String realPath,String imagePath,String outPath,boolean isChi) throws Exception{
- Runtime r = Runtime.getRuntime();
- String cmd = "\""+realPath+"Tesseract-OCR\\tesseract.exe\" \""+imagePath+"\" \""+outPath+"\" -l "+(isChi?chiSIM:"");
- r.exec(cmd);
- }
- }
package util;
public class OCRUtil {
public static String chiSIM = "chi_sim";
public static void runOCR(String realPath,String imagePath,String outPath,boolean isChi) throws Exception{
Runtime r = Runtime.getRuntime();
String cmd = "\""+realPath+"Tesseract-OCR\\tesseract.exe\" \""+imagePath+"\" \""+outPath+"\" -l "+(isChi?chiSIM:"");
r.exec(cmd);
}
}
[java]
view plain
copy
print?
- package util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- publicclass FileUtil {
- publicstatic String read(String path) throws IOException{
- String txt = "";
- File file = new File(path);
- long timeout = 30*60;
- while(!(file.isFile() && file.exists())){
- file = new File(path);
- try {
- Thread.sleep(100);
- timeout -= 100;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (file.isFile() && file.exists()) {
- InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
- BufferedReader bReader = new BufferedReader(read);
- String temptxt = "";
- txt = "";
- while((temptxt=bReader.readLine())!=null){
- txt += temptxt;
- }
- bReader.close();
- read.close();
- }
- return txt;
- }
- publicstaticvoid creatPath(String path) throws IOException{
- File file = new File(path);
- file.mkdir();
- }
- publicstaticvoid delete(String path) throws IOException{
- File file = new File(path);
- String[] list = file.list();
- File tempFile = null;
- for(String temp : list){
- tempFile = new File(path+temp);
- tempFile.delete();
- }
- file.delete();
- }
- }
package util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileUtil {
public static String read(String path) throws IOException{
String txt = "";
File file = new File(path);
long timeout = 30*60;
while(!(file.isFile() && file.exists())){
file = new File(path);
try {
Thread.sleep(100);
timeout -= 100;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader bReader = new BufferedReader(read);
String temptxt = "";
txt = "";
while((temptxt=bReader.readLine())!=null){
txt += temptxt;
}
bReader.close();
read.close();
}
return txt;
}
public static void creatPath(String path) throws IOException{
File file = new File(path);
file.mkdir();
}
public static void delete(String path) throws IOException{
File file = new File(path);
String[] list = file.list();
File tempFile = null;
for(String temp : list){
tempFile = new File(path+temp);
tempFile.delete();
}
file.delete();
}
}
下面是JSP程式碼: [java] view plain copy print?
- <%@ page language="java"import="java.util.*" pageEncoding="GBK"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>線上OCR--By Lee</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--a
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <script type="text/javascript">
- function check(){
- var path = document.getElementById("image").value;
- if(path.length==0){
- alert("請選擇要匯入的圖片!");
- returnfalse;
- }
- if(!(path.match(/.jpg$/i)||path.match(/.bmp$/i)||path.match(/.gif$/i)||path.match(/.png$/i))){
- alert("只支援JPG,BMP,GIF,PNG格式!");
- returnfalse;
- }
- returntrue;
- }
- </script>
- <body>
- <form enctype="multipart/form-data" method="post" action="OCRServlet" onsubmit="return check();">
- 選擇檔案:<input type="file" id="image" name="image"><br/>
- 上傳檔案:<input type="submit" value="提交上傳">
- </form>
- <textarea rows="20" cols="60"><%Object txt = request.getAttribute("txt");
- if(txt!=null&&txt.toString().length()==0){
- out.print("未識別出任何文字!");
- }elseif(txt!=null){
- out.print(txt.toString());
- }
- %></textarea>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>線上OCR--By Lee</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--a
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
function check(){
var path = document.getElementById("image").value;
if(path.length==0){
alert("請選擇要匯入的圖片!");
return false;
}
if(!(path.match(/.jpg$/i)||path.match(/.bmp$/i)||path.match(/.gif$/i)||path.match(/.png$/i))){
alert("只支援JPG,BMP,GIF,PNG格式!");
return false;
}
return true;
}
</script>
<body>
<form enctype="multipart/form-data" method="post" action="OCRServlet" onsubmit="return check();">
選擇檔案:<input type="file" id="image" name="image"><br/>
上傳檔案:<input type="submit" value="提交上傳">
</form>
<textarea rows="20" cols="60"><%Object txt = request.getAttribute("txt");
if(txt!=null&&txt.toString().length()==0){
out.print("未識別出任何文字!");
}else if(txt!=null){
out.print(txt.toString());
}
%></textarea>
</body>
</html>
效果圖:
在圖片沒做任何處理的情況下,識別率還是挺低的。。
相關推薦
基於Tesseract-OCR實現的JAVA WEB版OCR(圖片轉文字)
接下來就是新建一個JAVA EE專案,把Tesseract-OCR放在專案WebRoot下。 下面是主要程式碼: 接受客戶端上傳過來的圖片,使用Tesseract-OCR識別後返回至前臺。 [java] view plain copy print? package servlet; impo
基於GoEasy實現Java web實時資料推送
以前都是使用ajax定時傳送請求到後臺,這種方式非常消耗系統資源。在大併發情況時如果不對執行緒進行控制的話,還會重複取資料,造成資料錯誤。 鑑於這種情況,使用websocket通訊就是一個非常好的選擇。websocket能避免浪費系統資源,但是它有一個缺點就是不相容
Centos7 安裝skywalking+elasticsearch,實現java web程式鏈路追蹤
skywalking+elasticsearch實現java web程式鏈路追蹤 原始碼:聯絡作者1782800572 Skywalking暫不支援elasticsearch版本6.0.0以上 安裝elastic search5.6.13: tar -xzvf el
資料結構實現 10.2:對映_基於AVL樹實現(C++版)
資料結構實現 10.2:對映_基於AVL樹實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 修改操作 2.4 查詢操作 2.5 其他操作 3.
資料結構實現 6.4:優先佇列_基於連結串列實現(C++版)
資料結構實現 6.4:優先佇列_基於連結串列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 入隊操作 2.2 出隊操作 2.3 查詢操作 2.4 其他操作 3. 演算法複雜度分析
資料結構實現 6.3:優先佇列_基於動態陣列實現(C++版)
資料結構實現 6.3:優先佇列_基於動態陣列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 入隊操作 2.2 出隊操作 2.3 查詢操作 2.4 其他操作 3. 演算法複雜度分析
資料結構實現 6.1:二叉堆_基於動態陣列實現(C++版)
資料結構實現 6.1:二叉堆_基於動態陣列實現(C++版) 1. 概念及基本框架 1.1 滿二叉樹 1.2 完全二叉樹 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 查詢操作
資料結構實現 5.2:對映_基於連結串列實現(C++版)
資料結構實現 5.2:對映_基於連結串列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 修改操作 2.4 查詢操作 2.5 其他操作 3. 演
資料結構實現 4.2:集合_基於連結串列實現(C++版)
資料結構實現 4.2:集合_基於連結串列實現(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 查詢操作 2.4 其他操作 3. 演算法複雜度分析
實現Java Web專案分頁功能
接觸Java技術以來,專案中實現分頁功能一直以來是一大糾結點。近日終歸算是下定決心研究一下其中的原理了嘍~~~ 本次主要研究分頁功能實現原理,所以沒有使用任何框架技術,通過簡單的Servlet、jsp連線MySQL資料庫來實現。 一、準備工作: 1. 在MySQL資料庫中建立資料表,並新增
練習搭建spring+springmvc+mybatis實現java web登陸
關於spring、springmvc、mybatis的相關資料需要提前瞭解一下。我也只是初學者,就不介紹了。 我使用的是idea 如果有用eclipse也是類似 第一步我是用的maven來引進外部包,它很好用省得你到處去下載包。 新建一個maven專案 修改p
Servlet監聽器與Timer定時器配合實現JAVA WEB應用簡單自動作業
在web應用中,有時候客戶需要一些定時程式,不需要客戶自己去操作,而是由應用程式自行觸發執行某些操作。這個時候監聽與定時器的配合使用就基本可以實現這個需求了。 1.建立一個監聽的SERVELET,這個類繼承javax.servlet.http.HttpSe
java-web系列(一)---搭建一個基於SSM框架的java-web專案
前言 extensible專案當前功能模組如下: 如對該專案有疑問,可在我的部落格/github下面留言,也可以以郵件的方式告知。 我的聯絡方式:[email protected] extensible 這是一個基礎的java web專案。後期我會
選購和配置阿里雲伺服器(Java web版)
1 購買伺服器 1.1 進入阿里雲服務官網,購買雲伺服器 ECS。 1.2 選擇下圖所示的預裝環境,配置和地域根據自己喜好選擇就行了,然後進行下一步購買就可以了。 經過上面操作,一個阿里雲伺服器的購買就完成了,下面我們來講下怎麼配置。 2 Win
vue-codemirror + Java Compiler實現Java Web IDE
# 背景 >最近同事告訴我一個很有趣的需求:讓使用者(應用場景中,一般為其他開發者)自己填入**Java程式碼片段**,程式碼片段的內容為已經規定好的模板類的**繼承類**,實現模板類定義的方法。我們的專案要實現動態編譯程式碼片段,儲存程式碼片段和使用者操作記錄的對映關係,並能夠在業務中載入程式碼片段執
迅捷OCR文字識別軟體教你如何將圖片轉文字
大家平時接收到的資料或者下載的檔案,很多都是圖片格式的,有時候需要將裡面的文字提取出來應用在別的地方,這時候就需要將圖片轉文字了,具體如何去操作呢?準備工作:圖片轉文字需要使用到工具,我們可以開啟電腦搜尋下載一個迅捷OCR文字識別軟體到自己的電腦中去,接下來會使用到。 操作步驟: 1:將安裝好的軟
迅捷OCR文字識別軟件教你如何將圖片轉文字
需要 .com 轉換 oss 如何 步驟 term 保存 ces 大家平時接收到的資料或者下載的文件,很多都是圖片格式的,有時候需要將裏面的文字提取出來應用在別的地方,這時候就需要將圖片轉文字了,具體如何去操作呢?準備工作:圖片轉文字需要使用到工具,我們可以打開電腦搜索下載
圖片轉文字很簡單,這三款OCR文字識別軟體必裝
比如要把書上的文字複製下來, 需要照著書一個一個字打,但是如果通過ocr識別軟體, 我們就可以直接通過拍照的形式把這些文字擷取下來。舉個例子:OCR識別之後 10每個人都聽命於三個“長官”控制模式必須因時制宜,一藥不能治百疾現在讓我們來看看人的行為如何被控制或影響。如果你的車子正需要換輪胎,你可能會先
如何利用OCR文字識別軟體將圖片轉文字
想要將圖片轉文字,可實現的方法有很多,接下來小編將分享一種在文字識別軟體中進行轉換的方法,一起來學習下。 輔助工具:電腦 迅捷OCR文字識別軟體 實用係數:☆☆☆☆☆ 推薦理由:該軟體是一款智慧化的OCR圖片文字識別軟體,支援PDF識別、掃
java web service 寫入圖片到web/img/
獲取本類service路徑,然後字串擷取和拼接 String classpath= this.getClass().getResource("/").getPath(); String path = classpath.substring(0,classpath.length()