2015-03-31 97 views
-2

您好新的Android應用程序開發,我試圖實現一個簡單的OCR應用程序在這裏找到:https://github.com/GautamGupta/Simple-Android-OCR 我使用android studio,我發現一些錯誤,必須做我固定的權限,但現在我得到這個錯誤:運行時錯誤:致命異常:主java.lang.ExceptionInInitializerError

03-31 20:07:55.010  822-822/com.example.zakaria.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.ExceptionInInitializerError 
      at engenoid.tessocrtest.MainActivity.onPhotoTaken(MainActivity.java:210) 
      at engenoid.tessocrtest.MainActivity.onActivityResult(MainActivity.java:134) 
      at android.app.Activity.dispatchActivityResult(Activity.java:5515) 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3429) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476) 
      at android.app.ActivityThread.access$1200(ActivityThread.java:157) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:176) 
      at android.app.ActivityThread.main(ActivityThread.java:5317) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load lept from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.zakaria.myapp-10.apk,libraryPath=/data/app-lib/com.example.zakaria.myapp-10]: findLibrary returned null 
      at java.lang.Runtime.loadLibrary(Runtime.java:365) 
      at java.lang.System.loadLibrary(System.java:535) 
      at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:44) 
            at engenoid.tessocrtest.MainActivity.onPhotoTaken(MainActivity.java:210) 
            at engenoid.tessocrtest.MainActivity.onActivityResult(MainActivity.java:134) 
            at android.app.Activity.dispatchActivityResult(Activity.java:5515) 
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3429) 
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476) 
            at android.app.ActivityThread.access$1200(ActivityThread.java:157) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:176) 
            at android.app.ActivityThread.main(ActivityThread.java:5317) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
            at dalvik.system.NativeStart.main(Native Method) 

這裏是主要的活動:

package engenoid.tessocrtest; 

import android.app.Activity; 
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.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.example.zakaria.myapp.R; 
import com.googlecode.tesseract.android.TessBaseAPI; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class MainActivity extends Activity { 
    public static final String PACKAGE_NAME = "com.datumdroid.android.ocr.simple"; 
    public static final String DATA_PATH = Environment 
      .getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/"; 

    // You should have the trained data file in assets folder 
    // You can get them at: 
    // http://code.google.com/p/tesseract-ocr/downloads/list 
    public static final String lang = "eng"; 

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

    protected Button _button; 
    // protected ImageView _image; 
    protected EditText _field; 
    protected String _path; 
    protected boolean _taken; 

    protected static final String PHOTO_TAKEN = "photo_taken"; 

    @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"); 
       } 
      } 

     } 

     // lang.traineddata file with the app (in assets folder) 
     // You can get them at: 
     // http://code.google.com/p/tesseract-ocr/downloads/list 
     // This area needs work and optimization 
     if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) { 
      try { 

       AssetManager assetManager = getAssets(); 
       InputStream in = assetManager.open("tessdata/" + lang + ".traineddata"); 
       //GZIPInputStream gin = new GZIPInputStream(in); 
       OutputStream out = new FileOutputStream(DATA_PATH 
         + "tessdata/" + lang + ".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.main); 

     // _image = (ImageView) findViewById(R.id.image); 
     _field = (EditText) findViewById(R.id.field); 
     _button = (Button) findViewById(R.id.button); 
     _button.setOnClickListener(new ButtonClickHandler()); 

     _path = DATA_PATH + "/ocr.jpg"; 
    } 

    public class ButtonClickHandler implements View.OnClickListener { 
     public void onClick(View view) { 
      Log.v(TAG, "Starting Camera app"); 
      startCameraActivity(); 
     } 
    } 

    // Simple android photo capture: 
    // http://labs.makemachine.net/2010/03/simple-android-photo-capture/ 

    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(MainActivity.PHOTO_TAKEN, _taken); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     Log.i(TAG, "onRestoreInstanceState()"); 
     if (savedInstanceState.getBoolean(MainActivity.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(); 

     // You now have the text in recognizedText var, you can do anything with it. 
     // We will display a stripped out trimmed alpha-numeric version of it (if lang is eng) 
     // so that garbage doesn't make it to the display. 

     Log.v(TAG, "OCRED TEXT: " + recognizedText); 

     if (lang.equalsIgnoreCase("eng")) { 
      recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); 
     } 

     recognizedText = recognizedText.trim(); 

     if (recognizedText.length() != 0) { 
      _field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText); 
      _field.setSelection(_field.getText().toString().length()); 
     } 


    } 

} 

這是清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.zakaria.myapp" > 



    <uses-permission android:name="android.permission.CAMERA"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
    <uses-feature 
     android:name="android.hardware.camera.flash" 
     android:required="false" /> 
    <uses-feature android:name="android.hardware.camera" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name="engenoid.tessocrtest.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

因此,如果我需要發佈任何其他事情,請提前告訴我,並提前致謝!

+0

看起來你錯過了一個.so文件「lept」 – 2015-03-31 20:18:51

+0

@GabeSechan這個文件應該添加在Android的一邊,這意味着智能手機或項目文件? – 2015-03-31 20:36:29

+0

你們是怎麼解決這個問題的? – Nisfan 2015-06-08 07:42:04

回答

0

@GabeSechan你是對的我通過應用程序文件,並應該是一個文件的名稱,做一些進一步的谷歌搜索,我瞭解到,該文件應該是在生成tess-two庫時生成的在這個應用程序中,我想我沒有那樣做。 非常感謝。