Android 定义资产文件夹的数据路径时出错

Android 定义资产文件夹的数据路径时出错,android,ocr,tesseract,Android,Ocr,Tesseract,试图在android应用程序中实现ocr,这里使用tut进行ocr,另一个用于surfaceview摄像头集成,并尝试将两者集成。有多个问题我无法解决的问题之一是将资产文件夹中的文件路径传递给基本uri方法 代码如下: protected void onPhotoTaken() { _taken = true; BitmapFactory.Options options = new BitmapFactory.Options(); options

试图在android应用程序中实现ocr,这里使用tut进行ocr,另一个用于surfaceview摄像头集成,并尝试将两者集成。有多个问题我无法解决的问题之一是将资产文件夹中的文件路径传递给基本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

非常感谢您的帮助。

您在assets文件夹中是否有文件夹
testdata
?是的,我在资产中有一个名为tessdata的文件夹,其中包含eng.traineddata文件以及一些其他多维数据集文件请参阅本教程@Dixit Patel我已经尝试过了,但是`AssetManager AssetManager=getAssets();请尝试{baseApi.init(assetManager.list(“tessdata”).toString(),lang);}catch(IOException e){//TODO自动生成的catch块e.printStackTrace();}`但是assetManager.list将包含列表的全部数据。。我只是需要一个路径来发送,就像我尝试过的那样file:///android_asset/tessdata/ …如果我使用baseApi.init(“/mnt/sdcard/CardBoost/”,lang),它不会导致任何问题;我也尝试过baseApi.init(“file:///android_asset/",郎",;但这也无济于事