android tesseract-ocr執行個體教程(包含中文識別)(附源碼)

來源:互聯網
上載者:User

標籤:

(轉載請註明出處:http://blog.csdn.net/buptgshengod

1.介紹    

    快過年了,博主的新應用-螢幕取詞之了老花鏡的編碼工作也在緊鑼密鼓的進行中。下面分享一下這個應用中的核心功能ocr,也就是圖片識詞功能。先來看下我的實現效果。是在網上隨便截下來的一個帶有英文的頁面,是我的應用程式的實現效果。


 

2.實現    (1)首先要下載我的源碼和語言套件,部落格下方會給出地址。(源碼設為10分,是想讓大家珍惜別人的勞動成果)     (2)把代碼中的lib中的兩個檔案夾和jar檔案匯入。     (3)需要注意的有兩點請認真看下面貼出的代碼的注釋 [java] view plaincopy 
  1. package com.example.tess;  
  2.   
  3.   
  4. import java.io.File;  
  5.   
  6. import com.googlecode.tesseract.android.TessBaseAPI;  
  7.   
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.content.Intent;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.BitmapFactory;  
  13. import android.view.Menu;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.TextView;  
  18.   
  19. public class MainActivity extends Activity {  
  20.   
  21.     private TextView text;  
  22.     TessBaseAPI baseApi;  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         Button bt=new Button(getBaseContext());  
  29.         bt=(Button)findViewById(R.id.button1);  
  30.           
  31.           text=new TextView(getBaseContext());  
  32.           text=(TextView)findViewById(R.id.textView1);  
  33.           
  34.           baseApi=new TessBaseAPI();  
  35.           //(注意)前面的地址是語言套件的父級。eng表示解析的是英文  
  36.           baseApi.init("/mnt/sdcard/tesseract/", "eng");  
  37.             
  38.         bt.setOnClickListener(new OnClickListener() {  
  39.             @Override  
  40.              public void onClick(View sourse) {  
  41.            // text.setText("sb");  
  42.                 //設定要ocr的圖片bitmap,要解析的圖片地址(注意)  
  43.                 baseApi.setImage(getDiskBitmap("/mnt/sdcard/mypic.bmp"));  
  44.                 //根據Init的語言,獲得ocr後的字串  
  45.                 String text1= baseApi.getUTF8Text();  
  46.                 text.setText(text1);  
  47.                 //釋放bitmap  
  48.                 baseApi.clear();  
  49.             }  
  50.         }  
  51.          );  
  52.     }  
  53.     /* 
  54.      * 將本地圖片轉換為bitmap 
  55.      */  
  56.   
  57.     private Bitmap getDiskBitmap(String pathString)  
  58.     {  
  59.         Bitmap bitmap = null;  
  60.         try  
  61.         {  
  62.             File file = new File(pathString);  
  63.             if(file.exists())  
  64.             {  
  65.                 bitmap = BitmapFactory.decodeFile(pathString);  
  66.                   
  67.             }  
  68.         } catch (Exception e)  
  69.         {  
  70.             // TODO: handle exception  
  71.         }  
  72.           
  73.           
  74.         return bitmap;  
  75.     }  
  76. }  

(4)圖片越大耗時越長,本例耗時差不多半分鐘 3.源碼及相關檔案  好吧,好多人說代碼下載不了或者說10分太貴了,這裡提供一下免費。其中tess檔案夾是android程式tessdata是語言套件   4.中文識別
 
 可到以下地址下載,將其解壓放到/tesseract/tessdata下面,然後將eng改為chi_sim
http://code.google.com/p/tesseract-ocr/downloads/detail?name=chi_sim.traineddata.gz&can=2&q=

android tesseract-ocr執行個體教程(包含中文識別)(附源碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.