2015-10-16 165 views
0

可以閱讀驗證碼?我如何閱讀?如何使用tesseract,寬度和背景變量讀取驗證碼

captcha

---編輯 代碼:

public static String imgToTxt(String imgFileName) { 
 
\t \t try { 
 
\t \t \t Process pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log")) 
 
\t \t \t \t \t .redirectError(new File("/tmp/tesseract-error.log")) 
 
\t \t \t \t \t .command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start(); 
 
\t \t \t pr.waitFor(); 
 
\t \t \t Reader input = new FileReader(new File(imgFileName + ".txt")); 
 
\t \t \t StringWriter output = new StringWriter(); 
 
\t \t \t IOUtils.copy(input, output); 
 
\t \t \t return output.toString(); 
 
\t \t } catch (Exception e) { 
 
\t \t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t return null; 
 
\t }

+0

提供您的代碼,你如何試圖才達到它 –

+0

請提供代碼,#2是不是編碼機。你需要做代碼,我們在這裏幫助消除錯誤並理解新的代碼。 –

+0

對,我編輯了我的帖子,謝謝。 –

回答

1

對於今後的援助。 我不得不使用imagemagick工具來處理圖像。刪除背景並改進字體文本。

public static String imgToTxt(String imgFileName) { 
 
\t \t try { 
 
\t \t \t ProcessBuilder pb = new ProcessBuilder("convert", imgFileName + ".png", "-fuzz",  "15%", "-fill", "black", 
 
\t \t \t \t \t "-opaque", "rgb(16,128,176)", "-fuzz", "40%", "-fill", "white", "+opaque", "black", 
 
\t \t \t \t \t imgFileName + ".png"); 
 
\t \t \t pb.redirectErrorStream(true); 
 
\t \t \t Process pr = pb.start(); 
 
\t \t \t BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())); 
 
\t \t \t String line = null; 
 
\t \t \t while ((line = br.readLine()) != null) { 
 
\t \t \t \t System.out.println(line); 
 
\t \t \t } 
 
\t \t \t pr.waitFor(); 
 

 
\t \t \t pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log")) 
 
\t \t \t \t \t .redirectError(new File("/tmp/tesseract-error.log")) 
 
\t \t \t \t \t .command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start(); 
 
\t \t \t pr.waitFor(); 
 
\t \t \t Reader input = new FileReader(new File(imgFileName + ".txt")); 
 
\t \t \t StringWriter output = new StringWriter(); 
 
\t \t \t IOUtils.copy(input, output); 
 
\t \t \t return output.toString(); 
 
\t \t } catch (Exception e) { 
 
\t \t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t return null; 
 
\t }