如何通过Tesseract开源OCR引擎创建Android OCR应用

2025-03-20 01:21:13
推荐回答(2个)
回答1:

Tesseract是遵守 Apache License 2.0协议的开源OCR引擎。

如何在Android平台编译Tesseract,以及如何快速创建一个简单的OCR应用。

Tesseract Android Tools

要编译Android平台的Tesseract,需要使用Google提供的tesseract-android-tools。

代码获取方式:   


打开README,在命令行工具中执行下面的步骤:

  1. cd 

  2. curl -O 

  3. curl -O 

  4. tar -zxvf tesseract-ocr-3.02.02.tar.gz

  5. tar -zxvf leptonica-1.69.tar.gz

  6. rm -f tesseract-ocr-3.02.02.tar.gz

  7. rm -f leptonica-1.69.tar.gz

  8. mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src

  9. mv leptonica-1.69 jni/com_googlecode_leptonica_android/src

  10. ndk-build -j8

  11. android update project --target 1 --path .

  12. ant debug (release)

注意:如果在使用NDK r9,编译的时候会出现错误:

format not a string literal and no format arguments [-Werror=format-security]

解决的方法就是在Application.mk中加入一行:

APP_CFLAGS += -Wno-error=format-security

编译之后会生成class.jar和一些*.so。

Android OCR Application

创建一个Android应用,把生成的jar和so导入进来。

创建TessOCR:

  1. public class TessOCR {

  2.     private TessBaseAPI mTess;

  3.   

  4.     public TessOCR() {

  5.         // TODO Auto-generated constructor stub

  6.         mTess = new TessBaseAPI();

  7.         String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";

  8.         String language = "eng";

  9.         File dir = new File(datapath + "tessdata/");

  10.         if (!dir.exists()) 

  11.             dir.mkdirs();

  12.         mTess.init(datapath, language);

  13.     }

  14.   

  15.     public String getOCRResult(Bitmap bitmap) {

  16.   

  17.         mTess.setImage(bitmap);

  18.         String result = mTess.getUTF8Text();

  19.   

  20.         return result;

  21.     }

  22.   

  23.     public void onDestroy() {

  24.         if (mTess != null)

  25.             mTess.end();

  26.     }

  27.   

  28. }

构造函数中需要在存储卡上创建一个目录tessdata,如果不创建程序运行就会出错。因为源码中会检测这个目录,不存在就抛出异常:

  1. public boolean init(String datapath, String language) {

  2.         if (datapath == null) {

  3.             throw new IllegalArgumentException("Data path must not be null!");

  4.         }

  5.         if (!datapath.endsWith(File.separator)) {

  6.             datapath += File.separator;

  7.         }

  8.   

  9.         File tessdata = new File(datapath + "tessdata");

  10.         if (!tessdata.exists() || !tessdata.isDirectory()) {

  11.             throw new IllegalArgumentException("Data path must contain subfolder tessdata!");

  12.         }

  13.   

  14.         return nativeInit(datapath, language);

  15.     }

就这么简单。现在通过三种方式获取图片做OCR:

在图库中选取一张图,选择发送或者分享,选择OCR应用

在AndroidManifest.xml中加入IntentFilter,让OCR应用出现在图库的分享列表中: 

  1.                 

  2.   

  3.                 

  4.                 

  5.                 

获得URI之后,对URI解码,获取bitmap:  

  1. if (Intent.ACTION_SEND.equals(intent.getAction())) {

  2.     Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

  3.     uriOCR(uri);

  4. }

  5. private void uriOCR(Uri uri) {

  6.         if (uri != null) {

  7.             InputStream is = null;

  8.             try {

  9.                 is = getContentResolver().openInputStream(uri);

  10.                 Bitmap bitmap = BitmapFactory.decodeStream(is);

  11.                 mImage.setImageBitmap(bitmap);

  12.                 doOCR(bitmap);

  13.             } catch (FileNotFoundException e) {

  14.                 // TODO Auto-generated catch block

  15.                 e.printStackTrace();

  16.             } finally {

  17.                 if (is != null) {

  18.                     try {

  19.                         is.close();

  20.                     } catch (IOException e) {

  21.                         // TODO Auto-generated catch block

  22.                         e.printStackTrace();

  23.                     }

  24.                 }

  25.             }

  26.         }

  27. }

启动OCR应用,从图库中选择一张图做OCR

发送Intent调用图库,在onActivityResult中获取返回的URI做OCR:

  1. Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

  2. startActivityForResult(intent, REQUEST_PICK_PHOTO);

启动OCR应用,拍照之后做OCR

为了获取高质量的图片,在Intent中加入图片路径。返回之后就可以直接使用这个图片路径解码:

  1. private void dispatchTakePictureIntent() {

  2.         Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

  3.         // Ensure that there's a camera activity to handle the intent

  4.         if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

  5.             // Create the File where the photo should go

  6.             File photoFile = null;

  7.             try {

  8.                 photoFile = createImageFile();

  9.             } catch (IOException ex) {

  10.                 // Error occurred while creating the File

  11.   

  12.             }

  13.             // Continue only if the File was successfully created

  14.             if (photoFile != null) {

  15.                 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,

  16.                         Uri.fromFile(photoFile));

  17.                 startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

  18.             }

  19.         }

  20. }

不要忘记下载语言包,并push到存储卡的tessdata目录下。

回答2:

要编译Android平台的Tesseract,需要使用Google提供的tesseract-android-tools。

代码获取方式:

git clone https //code google com/p/tesseract-android-tools/

打开README,在命令行工具中执行下面的步骤:

cd
curl -O https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
curl -O http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz
tar -zxvf tesseract-ocr-3.02.02.tar.gz
tar -zxvf leptonica-1.69.tar.gz
rm -f tesseract-ocr-3.02.02.tar.gz
rm -f leptonica-1.69.tar.gz
mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
mv leptonica-1.69 jni/com_googlecode_leptonica_android/src
ndk-build -j8
android update project --target 1 --path .
ant debug (release)

注意:如果在使用NDK r9,编译的时候会出现错误:

format not a string literal and no format arguments [-Werror=format-security]

解决的方法就是在Application.mk中加入一行:

APP_CFLAGS += -Wno-error=format-security

编译之后会生成class.jar和一些*.so。