程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 辨認率很高的java文字辨認技巧

辨認率很高的java文字辨認技巧

編輯:關於JAVA

辨認率很高的java文字辨認技巧。本站提示廣大學習愛好者:(辨認率很高的java文字辨認技巧)文章只能為提供參考,不一定能成為您想要的結果。以下是辨認率很高的java文字辨認技巧正文


java文字辨認法式的症結是尋覓一個可以挪用的OCR引擎。tesseract-ocr就是一個如許的OCR引擎,在1985年到1995年由HP試驗室開辟,如今在Google。tesseract-ocr 3.0宣布,支撐中文。不外tesseract-ocr 3.0不是圖形化界面的客戶端,他人寫的FreeOCR圖形化客戶端還不支撐導入新的 3.0 traineddata。但這標記著,如今有自在的中文OCR軟件了。

java中應用tesseract-ocr3.01的步調以下: 

1.下載裝置tesseract-ocr-setup-3.01-1.exe(3.0以上版本才增長了中文辨認) 

2.在裝置領導中可以選擇須要下載的說話包。 

3.到網上搜刮下載java圖形處置所需的2個包:jai_imageio-1.1-alpha.jar,swingx-1.6.1.jar 

4.java法式清單: 

ImageIOHelper 類:

 import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;

public class ImageIOHelper { 
  
 public static File createImage(File imageFile, String imageFormat) { 
  File tempFile = null; 
  try { 
   Iterator readers = ImageIO.getImageReadersByFormatName(imageFormat); 
   ImageReader reader = readers.next(); 
   
   ImageInputStream iis = ImageIO.createImageInputStream(imageFile); 
   reader.setInput(iis); 
   //Read the stream metadata 
   IIOMetadata streamMetadata = reader.getStreamMetadata(); 
    
   //Set up the writeParam 
   TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.CHINESE); 
   tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); 
    
   //Get tif writer and set output to file 
   Iterator writers = ImageIO.getImageWritersByFormatName("tiff"); 
   ImageWriter writer = writers.next(); 
    
   BufferedImage bi = reader.read(0); 
   IIOImage image = new IIOImage(bi,null,reader.getImageMetadata(0)); 
   tempFile = tempImageFile(imageFile); 
   ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile); 
   writer.setOutput(ios); 
   writer.write(streamMetadata, image, tiffWriteParam); 
   ios.close(); 
    
   writer.dispose(); 
   reader.dispose(); 
    
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
  return tempFile; 
 } 
 
 private static File tempImageFile(File imageFile) { 
  String path = imageFile.getPath(); 
  StringBuffer strB = new StringBuffer(path); 
  strB.insert(path.lastIndexOf('.'),0); 
  return new File(strB.toString().replaceFirst("(?<=//.)(//w+)$", "tif")); 
 } 
 
} 

OCR 類:

 package com.hhp.util;

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
import org.jdesktop.swingx.util.OS; 
 
public class OCR { 
 private final String LANG_OPTION = "-l"; //英文字母小寫l,並不是數字1 
 private final String EOL = System.getProperty("line.separator"); 
 private String tessPath = "C://Program Files (x86)//Tesseract-OCR"; 
 //private String tessPath = new File("tesseract").getAbsolutePath(); 
  
 public String recognizeText(File imageFile,String imageFormat)throws Exception{ 
  File tempImage = ImageIOHelper.createImage(imageFile,imageFormat); 
  File outputFile = new File(imageFile.getParentFile(),"output"); 
  StringBuffer strB = new StringBuffer(); 
  List cmd = new ArrayList(); 
  if(OS.isWindowsXP()){ 
   cmd.add(tessPath+"//tesseract"); 
  }else if(OS.isLinux()){ 
   cmd.add("tesseract"); 
  }else{ 
   cmd.add(tessPath+"//tesseract"); 
  } 
  cmd.add(""); 
  cmd.add(outputFile.getName()); 
  cmd.add(LANG_OPTION); 
  cmd.add("chi_sim"); 
  //cmd.add("eng"); 
   
  ProcessBuilder pb = new ProcessBuilder(); 
  pb.directory(imageFile.getParentFile()); 
   
  cmd.set(1, tempImage.getName()); 
  pb.command(cmd); 
  pb.redirectErrorStream(true); 
   
  Process process = pb.start(); 
  //tesseract.exe 1.jpg 1 -l chi_sim 
  int w = process.waitFor(); 
   
  //刪除暫時正在任務文件 
  tempImage.delete(); 
   
  if(w==0){ 
   BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()+".txt"),"UTF-8")); 
    
   String str; 
   while((str = in.readLine())!=null){ 
    strB.append(str).append(EOL); 
   } 
   in.close(); 
  }else{ 
   String msg; 
   switch(w){ 
    case 1: 
     msg = "Errors accessing files.There may be spaces in your image's filename."; 
     break; 
    case 29: 
     msg = "Cannot recongnize the image or its selected region."; 
     break; 
    case 31: 
     msg = "Unsupported image format."; 
     break; 
    default: 
     msg = "Errors occurred."; 
   } 
   tempImage.delete(); 
   throw new RuntimeException(msg); 
  } 
  new File(outputFile.getAbsolutePath()+".txt").delete(); 
  return strB.toString(); 
 } 
}
  

測試類TestOCR :

import java.io.File;
import java.io.IOException;

import com.hhp.util.OCR;

public class OcrTest {

 public static void main(String[] args) {
  String path = "C://temp//OCRcode//4.png";  
  System.out.println("ORC Test Begin......");
  try {  
   String valCode = new OCR().recognizeText(new File(path), "png");  
   System.out.println(valCode);  
  } catch (IOException e) {  
   e.printStackTrace();  
  } catch (Exception e) { 
   e.printStackTrace(); 
  }  
  System.out.println("ORC Test End......");
 } 

}

經由測試,tesseract-ocr 3.01的文字辨認率很高,關於網站中罕見的驗證碼辨認率也很高。

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved