用错误的tesseract Android中 [英] Errors with Tesseract in Android

查看:105
本文介绍了用错误的tesseract Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个教程,包括在我的Andr​​oid应用程序的tesseract。下面是我的活动code:

 包com.MyApp;进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口com.google code.tesseract.android.TessBaseAPI;
进口android.annotation.Sup pressLint;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.content.res.AssetManager;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;
进口android.media.ExifInterface;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Environment;
进口android.provider.MediaStore;
进口android.provider.MediaStore.Images.Media;
进口android.util.Log;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.TextView;
公共类OCRExample延伸活动{
    保护按钮_button;
    保护ImageView的_image;
    保护的TextView _field;
    保护字符串_path;
    保护布尔_taken;    受保护的静态最后弦乐PHOTO_TAKEN =photo_taken;
    私有静态最终诠释TAKE_PHOTO_ code = 1;
    公共静态最后弦乐PACKAGE_NAME =com.datumdroid.android.ocr.simple;
    公共静态最后弦乐DATA_PATH =环境
            。.getExternalStorageDirectory()的toString()+/ SimpleAndroidOCR /;
    公共静态最后弦乐LANG =工程;    私有静态最后弦乐TAG =SimpleAndroidOCR.java;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        的String [] =路径,新的String [] {DATA_PATH,DATA_PATH +tessdata /};
        对于(字符串路径:路径){
            文件DIR =新的文件(路径);
            如果(!dir.exists()){
                如果(!dir.mkdirs()){
                    Log.v(TAG,错误:目录的创建+路径+关于SD卡失败);
                    返回;
                }其他{
                    Log.v(TAG,创建目录+SD卡上的路径+);
                }
            }        }        如果(!(新文件(DATA_PATH +tessdata /+郎+.traineddata))。存在()){
            尝试{
                AssetManager assetManager = getAssets();
                InputStream的时间= assetManager.open(tessdata / eng.traineddata);
                // GZIPInputStream杜松子酒=新GZIPInputStream(中);
                出的OutputStream =新的FileOutputStream(DATA_PATH
                        +tessdata / eng.traineddata);                //传输的字节从到了
                字节[] buf中=新的字节[1024];
                INT LEN;
                //而((lenf = gin.read(浅黄色))大于0){
                而((LEN = in.read(BUF))大于0){
                    out.write(BUF,0,LEN);
                }
                附寄();
                //gin.close();
                out.close();                Log.v(TAG,复制+郎+traineddata);
            }赶上(IOException异常五){
                Log.e(TAG,无法复制+郎+traineddata+ e.toString());
            }
        }
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.ocrexample);        _image =(ImageView的)findViewById(R.id.image);
        _field =(的TextView)findViewById(R.id.field);
        _button =(按钮)findViewById(R.id.button);
         _path = DATA_PATH +/ocr.jpg
    }    公共无效takepic(视图v){
            startCameraActivity();
    }    保护无效startCameraActivity()
    {
        档案文件=新的文件(_path);
        乌里outputFileUri = Uri.fromFile(文件);        最终意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);        startActivityForResult(意向,0);
    }    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
    {
        Log.i(TAG,结果code:+结果code);        如果(结果code == -1){
            onPhotoTaken();
        }其他{
            Log.v(TAG,用户取消);
        }
    }    @覆盖
    保护无效的onSaveInstanceState(捆绑outState){
        outState.putBoolean(OCRExample.PHOTO_TAKEN,_taken);
    }    @覆盖
    保护无效onRestoreInstanceState(捆绑savedInstanceState){
        Log.i(TAGonRestoreInstanceState());
        如果(savedInstanceState.getBoolean(OCRExample.PHOTO_TAKEN)){
            onPhotoTaken();
        }
    }    保护无效onPhotoTaken(){
        _taken = TRUE;        BitmapFactory.Options选项=新BitmapFactory.Options();
        options.inSampleSize = 4;        位图位图= BitmapFactory.de codeFILE(_path,期权);        尝试{
            ExifInterface EXIF​​ =新ExifInterface(_path);
            INT exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);            Log.v(TAG,东方+ exifOrientation);            INT旋转= 0;            开关(exifOrientation){
            案例ExifInterface.ORIENTATION_ROTATE_90:
                旋转= 90;
                打破;
            案例ExifInterface.ORIENTATION_ROTATE_180:
                旋转= 180;
                打破;
            案例ExifInterface.ORIENTATION_ROTATE_270:
                旋转= 270;
                打破;
            }            Log.v(TAG,旋转+旋转);            如果(旋转!= 0){                //获取宽度放大器;给定图像的高度。
                INT W = bitmap.getWidth();
                INT H = bitmap.getHeight();                //设置pre旋转
                矩阵MTX =新的Matrix();
                。MTX preRotate(旋转);                //旋转位图
                位图= Bitmap.createBitmap(位图,0,0,W,H,MTX,假);
            }            //转换为ARGB_8888,苔丝要求
            位= bitmap.copy(Bitmap.Config.ARGB_8888,真);        }赶上(IOException异常五){
            Log.e(TAG,无法正确的方向:+ e.toString());
        }        // _image.setImageBitmap(位图);        Log.v(TAGbaseApi之前);        TessBaseAPI baseApi =新TessBaseAPI();
        baseApi.setDebug(真);
        baseApi.init(DATA_PATH,郎);
        baseApi.setImage(位图);        串recognizedText = baseApi.getUTF8Text();        baseApi.end();        _field.setText(recognizedText); //循环完成。
    }
}

我收到以下错误:

  07-17 14:22:49.124:E / AndroidRuntime(15702):致命异常:主要
07-17 14:22:49.124:E / AndroidRuntime(15702):java.lang.ExceptionInInitializerError
07-17 14:22:49.124:E / AndroidRuntime(15702):在com.MyApp.OCRExample.onPhotoTaken(OCRExample.java:210)
07-17 14:22:49.124:E / AndroidRuntime(15702):在com.MyApp.OCRExample.onActivityResult(OCRExample.java:134)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.Activity.dispatchActivityResult(Activity.java:4723)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.ActivityThread.access $ 1100(ActivityThread.java:134)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1263)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.os.Handler.dispatchMessage(Handler.java:99)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.os.Looper.loop(Looper.java:137)
07-17 14:22:49.124:E / AndroidRuntime(15702):在android.app.ActivityThread.main(ActivityThread.java:4697)
07-17 14:22:49.124:E / AndroidRuntime(15702):在java.lang.reflect.Method.invokeNative(本机方法)
07-17 14:22:49.124:E / AndroidRuntime(15702):在java.lang.reflect.Method.invoke(Method.java:511)
07-17 14:22:49.124:E / AndroidRuntime(15702):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:787)
07-17 14:22:49.124:E / AndroidRuntime(15702):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-17 14:22:49.124:E / AndroidRuntime(15702):在dalvik.system.NativeStart.main(本机方法)
07-17 14:22:49.124:E / AndroidRuntime(15702):java.lang.UnsatisfiedLinkError中:产生的原因无法加载lept:findLibrary返回null
07-17 14:22:49.124:E / AndroidRuntime(15702):在java.lang.Runtime.loadLibrary(Runtime.java:365)
07-17 14:22:49.124:E / AndroidRuntime(15702):在java.lang.System.loadLibrary(System.java:535)
07-17 14:22:49.124:E / AndroidRuntime(15702):在com.google code.tesseract.android.TessBaseAPI< clinit>(TessBaseAPI.java:47)

什么是从我的项目缺少什么?线210(发生错误)在我的code是这样的:

  TessBaseAPI baseApi =新TessBaseAPI();


解决方案

该错误消息的所有状态,你需要的leptonica(特别liblept.so)未找到,请确保您有可在运行时库(检查进口正确库项目)

I am following this tutorial to include tesseract in my android app. Below is my activity code:

package com.MyApp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.googlecode.tesseract.android.TessBaseAPI;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class OCRExample extends Activity {
    protected Button _button;
    protected ImageView _image;
    protected TextView _field;
    protected String _path;
    protected boolean _taken;

    protected static final String PHOTO_TAKEN = "photo_taken";
    private static final int TAKE_PHOTO_CODE = 1;
    public static final String PACKAGE_NAME = "com.datumdroid.android.ocr.simple";
    public static final String DATA_PATH = Environment
            .getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";
    public static final String lang = "eng";

    private static final String TAG = "SimpleAndroidOCR.java";

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
        for (String path : paths) {
            File dir = new File(path);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                    return;
                } else {
                    Log.v(TAG, "Created directory " + path + " on sdcard");
                }
            }

        }       

        if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
            try {
                AssetManager assetManager = getAssets();
                InputStream in = assetManager.open("tessdata/eng.traineddata");
                //GZIPInputStream gin = new GZIPInputStream(in);
                OutputStream out = new FileOutputStream(DATA_PATH
                        + "tessdata/eng.traineddata");

                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                //while ((lenf = gin.read(buff)) > 0) {
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                //gin.close();
                out.close();

                Log.v(TAG, "Copied " + lang + " traineddata");
            } catch (IOException e) {
                Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
            }
        }       
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.ocrexample);

        _image = ( ImageView ) findViewById( R.id.image );
        _field = ( TextView ) findViewById( R.id.field );
        _button = ( Button ) findViewById( R.id.button );        
         _path = DATA_PATH + "/ocr.jpg";    
    }

    public void takepic(View v){    
            startCameraActivity();      
    }

    protected void startCameraActivity()
    {
        File file = new File(_path);
        Uri outputFileUri = Uri.fromFile(file);

        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        Log.i(TAG, "resultCode: " + resultCode);

        if (resultCode == -1) {
            onPhotoTaken();
        } else {
            Log.v(TAG, "User cancelled");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(OCRExample.PHOTO_TAKEN, _taken);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.i(TAG, "onRestoreInstanceState()");
        if (savedInstanceState.getBoolean(OCRExample.PHOTO_TAKEN)) {
            onPhotoTaken();
        }
    }

    protected void onPhotoTaken() {
        _taken = true;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

        try {
            ExifInterface exif = new ExifInterface(_path);
            int exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            Log.v(TAG, "Orient: " + exifOrientation);

            int rotate = 0;

            switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            }

            Log.v(TAG, "Rotation: " + rotate);

            if (rotate != 0) {

                // Getting width & height of the given image.
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();

                // Setting pre rotate
                Matrix mtx = new Matrix();
                mtx.preRotate(rotate);

                // Rotating Bitmap
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            }

            // Convert to ARGB_8888, required by tess
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        } catch (IOException e) {
            Log.e(TAG, "Couldn't correct orientation: " + e.toString());
        }

        // _image.setImageBitmap( bitmap );

        Log.v(TAG, "Before baseApi");

        TessBaseAPI baseApi = new TessBaseAPI();
        baseApi.setDebug(true);
        baseApi.init(DATA_PATH, lang);
        baseApi.setImage(bitmap);

        String recognizedText = baseApi.getUTF8Text();

        baseApi.end();

        _field.setText(recognizedText);     // Cycle done.
    }   
}

I am getting the following errors:

07-17 14:22:49.124: E/AndroidRuntime(15702): FATAL EXCEPTION: main
07-17 14:22:49.124: E/AndroidRuntime(15702): java.lang.ExceptionInInitializerError
07-17 14:22:49.124: E/AndroidRuntime(15702):    at com.MyApp.OCRExample.onPhotoTaken(OCRExample.java:210)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at com.MyApp.OCRExample.onActivityResult(OCRExample.java:134)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.Activity.dispatchActivityResult(Activity.java:4723)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.ActivityThread.access$1100(ActivityThread.java:134)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.os.Looper.loop(Looper.java:137)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at android.app.ActivityThread.main(ActivityThread.java:4697)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at dalvik.system.NativeStart.main(Native Method)
07-17 14:22:49.124: E/AndroidRuntime(15702): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load lept: findLibrary returned null
07-17 14:22:49.124: E/AndroidRuntime(15702):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at java.lang.System.loadLibrary(System.java:535)
07-17 14:22:49.124: E/AndroidRuntime(15702):    at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:47)

What is missing from my project? Line 210 (where the error occurs) in my code is this:

TessBaseAPI baseApi = new TessBaseAPI();

解决方案

The error message states all that you need, the leptonica (specifically liblept.so) is not found, make sure that you have that library available at runtime (check library projects imported correctly)

这篇关于用错误的tesseract Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆