真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)服務(wù)項目包括紅河網(wǎng)站建設(shè)、紅河網(wǎng)站制作、紅河網(wǎng)頁制作以及紅河網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,紅河網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到紅河省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

最近在開發(fā)的時候需要識別圖片中的一些文字,網(wǎng)上找了相關(guān)資料之后,發(fā)現(xiàn)google有一個離線的工具,以下為java使用的demo

在此之前,使用這個工具需要在本地安裝OCR工具:

如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能

下面一個是一定要安裝的離線包,建議默認安裝

上面一個是中文的語言包,如果網(wǎng)絡(luò)可以FQ的童鞋可以在安裝的時候就選擇語言包在線安裝,有多種語言可供選擇,默認只有英文的

exe安裝好之后,把上面一個文件拷到安裝目錄下tessdata文件夾下

如C:\Program Files (x86)\Tesseract-OCR\tessdata下

然后下面兩個是可選包,如果圖片不做臨時文件處理的話,可以不需要帶的

如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能

首先是一個臨時文件生成用的類以防源文件損壞,參考某位博友的例子@Gunner

package org.ink.image.textrz;

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 {
  private Locale locale=Locale.CHINESE;
  /**
   * user set locale Construct
   * @param locale
   */
  public ImageIOHelper(Locale locale){
    this.locale=locale;
  }
  
  /**
   * default construct using default locale Locale.CHINESE
   */
  public ImageIOHelper(){
    
  }
  /**
   * create tempFile of Image in order to prevent damaging original file
   * @param imageFile
   * @param imageFormat like png,jps .etc
   * @return TempFile of Image
   * @throws IOException
   */
  public File createImage(File imageFile, String imageFormat) throws IOException {  
    Iterator readers = ImageIO.getImageReadersByFormatName(imageFormat);  
    ImageReader reader = readers.next();  
    ImageInputStream iis = ImageIO.createImageInputStream(imageFile);  
    reader.setInput(iis);  
    IIOMetadata streamMetadata = reader.getStreamMetadata();  
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.CHINESE);  
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);  
    Iterator writers = ImageIO.getImageWritersByFormatName("tiff");  
    ImageWriter writer = writers.next();  
    BufferedImage bi = reader.read(0);  
    IIOImage image = new IIOImage(bi,null,reader.getImageMetadata(0));  
    File tempFile = tempImageFile(imageFile);  
    ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);  
    writer.setOutput(ios);  
    writer.write(streamMetadata, image, tiffWriteParam);  
    ios.close();
    iis.close();
    writer.dispose();  
    reader.dispose();  
    return tempFile;  
  }  
  /**
   * add suffix to tempfile
   * @param imageFile
   * @return
   * @throws IOException 
   */
  private File tempImageFile(File imageFile) throws IOException {  
    String path = imageFile.getPath();  
    StringBuffer strB = new StringBuffer(path);  
    strB.insert(path.lastIndexOf('.'),"_text_recognize_temp");
    String s=strB.toString().replaceFirst("(?<=//.)(//w+)$", "tif");
    Runtime.getRuntime().exec("attrib "+"\""+s+"\""+" +H"); //設(shè)置文件隱藏
    return new File(strB.toString()); 
  }  
  
} 

下面是真正識別的內(nèi)容:

package org.ink.image.textrz;

import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;  
import java.util.ArrayList;  
import java.util.List;
import java.util.Locale;

import org.jdesktop.swingx.util.OS;  

/**
 * TEXT Recognize Utils
 * @author ink.Flower
 *
 */
public class OCRUtil { 
  private final String LANG_OPTION = "-l"; //英文字母小寫l,并非數(shù)字1  
  private final String EOL = System.getProperty("line.separator");  
  private String tessPath = "C://Program Files (x86)//Tesseract-OCR";//ocr默認安裝路徑
  private String transname="chi_sim";//默認中文語言包,識別中文
  
  /**
   * Construct method of OCR ,set Tesseract-OCR install path
   * @param tessPath Tesseract-OCR install path
   * @param transFileName traningFile name like eng.traineddata
   */
  public OCRUtil(String tessPath,String transFileName){
    this.tessPath=tessPath;
    this.transname=transFileName;
  }
  /**
   * Construct method of OCR,default path is "C://Program Files (x86)//Tesseract-OCR"
   */
  public OCRUtil(){   }
  
  public String getTessPath() {
    return tessPath;
  }
  public void setTessPath(String tessPath) {
    this.tessPath = tessPath;
  }
  public String getTransname() {
    return transname;
  }
  public void setTransname(String transname) {
    this.transname = transname;
  }
  public String getLANG_OPTION() {
    return LANG_OPTION;
  }
  public String getEOL() {
    return EOL;
  }
  
  /**
   * recognize text in image
   * @param imageFile
   * @param imageFormat
   * @return text recognized in image
   * @throws Exception
   */
  public String recognizeText(File imageFile,String imageFormat)throws Exception{  
    File tempImage = new ImageIOHelper().createImage(imageFile,imageFormat);  
    return ocrImages(tempImage, imageFile);  
  }  
  
  /**
   * recognize text in image
   * @param imageFile
   * @param imageFormat
   * @param locale
   * @return text recognized in image
   * @throws Exception
   */
  public String recognizeText(File imageFile,String imageFormat,Locale locale)throws Exception{  
    File tempImage = new ImageIOHelper(locale).createImage(imageFile,imageFormat);
    return ocrImages(tempImage, imageFile);
      
  }
  /**
   * 
   * @param tempImage
   * @param imageFile
   * @return
   * @throws IOException
   * @throws InterruptedException
   */
  private String ocrImages(File tempImage,File imageFile) throws IOException, InterruptedException{
    File outputFile = new File(imageFile.getParentFile(),"output");
    Runtime.getRuntime().exec("attrib "+"\""+outputFile.getAbsolutePath()+"\""+" +H"); //設(shè)置文件隱藏
    StringBuffer strB = new StringBuffer();  
    List cmd = new ArrayList();  
    if(OS.isWindowsXP()){  
      cmd.add(tessPath+"http://tesseract");  
    }else if(OS.isLinux()){  
      cmd.add("tesseract");  
    }else{  
      cmd.add(tessPath+"http://tesseract");  
    }  
    cmd.add("");  
    cmd.add(outputFile.getName());  
    cmd.add(LANG_OPTION);  
    cmd.add(transname);  
    ProcessBuilder pb = new ProcessBuilder();  
    pb.directory(imageFile.getParentFile());  
    cmd.set(1, tempImage.getName());  
    pb.command(cmd);  
    pb.redirectErrorStream(true);  
    Process process = pb.start();  
    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(); 
  }
}  

上述就是小編為大家分享的如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)站標題:如何在java項目中利用ocr實現(xiàn)一個圖片文字識別功能
文章網(wǎng)址:http://weahome.cn/article/jjodee.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部