1

试图在 android 应用程序中实现 ocr,在这里使用 tut http://gaut.am/making-an-ocr-android-app-using-tesseract/用于 ocr 和另一个用于表面视图相机集成并尝试将它们集成。有多个问题我无法解决的问题之一是将资产文件夹中的文件路径传递给基本 uri 方法。

这是代码:

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("file:///android_asset/tessdata/", 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());
        }

        // Cycle done.
    }

这是logcat的输出

07-10 11:19:35.943: W/dalvikvm(24471): threadid=1: thread exiting with uncaught exception (group=0x40ab8228)
07-10 11:19:35.943: E/AndroidRuntime(24471): FATAL EXCEPTION: main
07-10 11:19:35.943: E/AndroidRuntime(24471): java.lang.IllegalArgumentException: Data path must contain subfolder tessdata!
07-10 11:19:35.943: E/AndroidRuntime(24471):    at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:215)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at com.example.cardboost.Home$4.onPictureTaken(Home.java:191)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:692)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at android.os.Looper.loop(Looper.java:154)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at android.app.ActivityThread.main(ActivityThread.java:4944)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at java.lang.reflect.Method.invoke(Method.java:511)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 11:19:35.943: E/AndroidRuntime(24471):    at dalvik.system.NativeStart.main(Native Method)
07-10 11:21:38.263: W/ActivityThread(24753): Application com.example.cardboost is waiting for the debugger on port 8100...
07-10 11:24:38.949: W/jdwp(24753): Debugger is telling the VM to exit with code=1

任何帮助将不胜感激谢谢。

4

0 回答 0