zoukankan      html  css  js  c++  java
  • android tesseract-ocr实例教程(包含中文识别)(附源码)

    (转载请注明出处:http://blog.csdn.net/buptgshengod

    1.介绍    

        快过年了,博主的新应用-屏幕取词之了老花镜的编码工作也在紧锣密鼓的进行中。下面分享一下这个应用中的核心功能ocr,也就是图片识词功能。先来看下我的实现效果。上图是在网上随便截下来的一个带有英文的页面,下图是我的应用的实现效果。


     

    2.实现

       (1)首先要下载我的源码和语言包,博客下方会给出地址。(源码设为10分,是想让大家珍惜别人的劳动成果)
        (2)把代码中的lib中的两个文件夹和jar文件导入。
        (3)需要注意的有两点请认真看下面贴出的代码的注释
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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=
  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/gisblogs/p/4210482.html
Copyright © 2011-2022 走看看