SuperNotCalledException修改了simpleOCR

时间:2014-03-26 23:31:05

标签: java android android-intent android-fragments

我修改了simpleOCR(https://github.com/GautamGupta/Simple-Android-OCR)和Tesseract 为我的申请。一切都很好,直到我突然开始得到SuperNotCalledException 我的LogCat中的消息。但是,super.onCreate(savedInstanceState);显然位于cameraOCR.class的onCreate方法(不是第一行)中。当我将super.onCreate(savedInstanceState);放在cameraOCR.class的onCreate的第一行时,启动cameraOCR的类将指向一个空白的白色窗口,而不是OCR的相机应用程序(应该发生在我的cameraOCR类中)。请查看我的代码,我不知道SuperNotCalledExcpetin在哪里出错。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;

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.googlecode.tesseract.android.TessBaseAPI;

public class cameraOCR extends Activity {
    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";


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

    }




    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);


    _path = DATA_PATH + "/ocr.jpg";


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

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "onRestoreInstanceState()");
    if (savedInstanceState.getBoolean(cameraOCR.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 ) {
        Intent InputPage = new Intent("InputPage");
        InputPage.putExtra("ocrText", recognizedText);
        startActivity(InputPage);

    }

    // Cycle done.
}

}

logcat的:

03-26 22:51:17.291: V/SimpleAndroidOCR.java(661): ERROR: Creation of directory /mnt/sdcard/SimpleAndroidOCR/ on sdcard failed
03-26 22:51:17.301: D/AndroidRuntime(661): Shutting down VM
03-26 22:51:17.311: W/dalvikvm(661): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-26 22:51:17.355: E/AndroidRuntime(661): FATAL EXCEPTION: main
03-26 22:51:17.355: E/AndroidRuntime(661): android.app.SuperNotCalledException: Activity {com.shreyash.automaticsummarization/com.shreyash.automaticsummarization.cameraOCR} did not call through to super.onCreate()
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1921)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.os.Looper.loop(Looper.java:137)
03-26 22:51:17.355: E/AndroidRuntime(661):      at android.app.ActivityThread.main(ActivityThread.java:4340)
03-26 22:51:17.355: E/AndroidRuntime(661):      at java.lang.reflect.Method.invokeNative(Native Method)
03-26 22:51:17.355: E/AndroidRuntime(661):      at java.lang.reflect.Method.invoke(Method.java:511)
03-26 22:51:17.355: E/AndroidRuntime(661):      at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-26 22:51:17.355: E/AndroidRuntime(661):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-26 22:51:17.355: E/AndroidRuntime(661):      at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

查看您的日志。你有这个:

ERROR: Creation of directory /mnt/sdcard/SimpleAndroidOCR/ on sdcard failed

在onCreate中:

Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;  //  <-- When you reach this line : super.onCreate(...) was not called !