1

有人可以解释到底发生了什么以及我如何纠正它。所以我知道 tesseract 必须有一个图像作为目标,大概这将是我捕获并尝试保存的位图。该保存的位置将是我的 _path 变量。现在 tesseract 的 DATA_PATH 是什么?图像是否需要存储在名为“tesseract”的文件夹中?我是否创建该文件夹并在其中存储某种培训?我正在寻找解释而不是代码示例。

http://gaut.am/making-an-ocr-android-app-using-tesseract/ - 我正在尝试遵循本教程并检查其他人以尝试了解他们所有人使用的路径。

public class MainActivity extends Activity {
private static ImageView imageView;
protected String _path;
// protected static Bitmap bit;
static File myDir;
protected static Bitmap mImageBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView) this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);

    _path =  Environment.getExternalStorageDirectory() + "/images/test.bmp";

    Toast t = Toast.makeText(getApplicationContext(), "HEELLLLLLOO", 100000);
    t.show();
    photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // CALL THE PICTURE
            dispatchTakePictureIntent(0);

        }
    });
}

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    imageView.setImageBitmap(mImageBitmap);

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

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

    imageView.setImageBitmap(bitmap);

    //_path = path to the image to be OCRed
    ExifInterface exif;
    try {
        exif = new ExifInterface(_path);

    int exifOrientation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_NORMAL);
    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;
    }
    if (rotate != 0) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        // Setting pre rotate
        Matrix mtx = new Matrix();
        mtx.preRotate(rotate);
        // Rotating Bitmap & convert to ARGB_8888, required by tess
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    }



    TessBaseAPI baseApi = new TessBaseAPI();
    // DATA_PATH = Path to the storage
    // lang for which the language data exists, usually "eng"
    baseApi.init(_path, "eng");  //THIS SHOULD BE DATA_PATH ?
    baseApi.setImage(bitmap);
    String recognizedText = baseApi.getUTF8Text();
    System.out.println(recognizedText);
    baseApi.end();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    handleSmallCameraPhoto(data);
}

private void dispatchTakePictureIntent(int actionCode) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, actionCode);
}

protected static void identifyunicode() {
    // DATA_PATH = Path to the storage
    // lang for which the language data exists, usually "eng"

    /*
     * TessBaseAPI baseApi = new TessBaseAPI();
     * baseApi.init(myDir.toString(), "eng"); // myDir + //
     * "/tessdata/eng.traineddata" // must be present baseApi.setImage(bit);
     * String recognizedText = baseApi.getUTF8Text(); // Log or otherwise //
     * display this // string... baseApi.end();
     */
}

}

4

1 回答 1

1

DataPath是您从中复制tessdata文件的路径Assets

import java.io.File;
import java.util.ArrayList;
import android.os.Environment;

public class Utils {

    public static boolean isSDCardMounted() {
        boolean isMounted = false;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            isMounted = true;
        } else if (Environment.MEDIA_BAD_REMOVAL.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_CHECKING.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_NOFS.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_REMOVED.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_UNMOUNTABLE.equals(state)) {
            isMounted = false;
        } else if (Environment.MEDIA_UNMOUNTED.equals(state)) {
            isMounted = false;
        }
        return isMounted;
    }

    public static boolean isDirectoryExists(final String filePath) {
        boolean isDirectoryExists = false;
        File mFilePath = new File(filePath);
        if(mFilePath.exists()) {
            isDirectoryExists = true;
        } else {
            isDirectoryExists = mFilePath.mkdirs();
        }
        return isDirectoryExists;
    }

    public static boolean deleteFile(final String filePath) {
        boolean isFileExists = false;
        File mFilePath = new File(filePath);
        if(mFilePath.exists()) {
            mFilePath.delete();
            isFileExists = true;
        }
        return isFileExists;
    }

    public static String getDataPath() {
        String returnedPath = "";
        final String mDirName = "tesseract";
        final String mDataDirName = "tessdata";
        if(isSDCardMounted()) {
            final String mSDCardPath = Environment.getExternalStorageDirectory() + File.separator + mDirName;
            if(isDirectoryExists(mSDCardPath)) {
                final String mSDCardDataPath = Environment.getExternalStorageDirectory() + File.separator + mDirName + 
                        File.separator + mDataDirName;
                isDirectoryExists(mSDCardDataPath);
                return mSDCardPath;
            }
        }
        return returnedPath;
    }
}

Activity班级

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;
import com.googlecode.tesseract.android.TessBaseAPI;

public class AndroidCommonTest extends Activity {
    private static final String TAG = AndroidCommonTest.class.getSimpleName();
    private TextView txtGotTime = null;
    private final int START_CODE = 101;
    private String mDirPath = null;
    private Uri mOutPutUri = null;
    private static final String lang = "eng";
    private String mPath = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtGotTime = (TextView) findViewById(R.id.txtGotTime);

        mDirPath = Utils.getDataPath();
        mPath = mDirPath + File.separator + "test.jpg";
        android.util.Log.i(TAG, "mDirPath: " + mDirPath + " mPath: " + mPath);

        if (!(new File(mDirPath + File.separator + "tessdata" + File.separator + lang + ".traineddata")).exists()) {
            try {
                AssetManager assetManager = getAssets();
                InputStream in = assetManager.open("tessdata" + File.separator + lang + ".traineddata");
                OutputStream out = new FileOutputStream(mDirPath + File.separator 
                        + "tessdata" + File.separator + lang + ".traineddata");
                byte[] buf = new byte[8024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (IOException e) {
                android.util.Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
            }
        } else {
            processImage(mDirPath + File.separator + "six.jpg", 0);
        }
    }

    public void getTime(View view) {
        android.util.Log.i(TAG, "mDirPath: " + mDirPath + " mPath: " + mPath);
        if(mDirPath != null && mDirPath.length() > 0) {
            mOutPutUri = Uri.fromFile(new File(mPath));
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mOutPutUri);
            startActivityForResult(intent, START_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == START_CODE) {
            if(resultCode == Activity.RESULT_OK) {
                int rotation = -1;
                long fileSize = new File(mPath).length();
                android.util.Log.i(TAG, "fileSize " + fileSize);

                //Suppose Device Supports ExifInterface
                ExifInterface exif;
                try {
                    exif = new ExifInterface(mPath);
                    int exifOrientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
                    switch (exifOrientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90 :
                            rotation = 90;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180 :
                            rotation = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_270 :
                            rotation = 270;
                            break;
                        case ExifInterface.ORIENTATION_NORMAL:
                        case ExifInterface.ORIENTATION_UNDEFINED:
                            rotation = 0;
                            break;
                    }
                    android.util.Log.i(TAG, "Exif:rotation " + rotation);

                    if (rotation != -1) {
                        processImage(mPath, rotation);
                    } else {
                        //Device Does Not Support ExifInterface
                        Cursor mediaCursor = getContentResolver().query(mOutPutUri,
                                new String[] { MediaStore.Images.ImageColumns.ORIENTATION, 
                                MediaStore.MediaColumns.SIZE },
                                null, null, null);
                        if (mediaCursor != null && mediaCursor.getCount() != 0 ) {
                            while(mediaCursor.moveToNext()){
                                long size = mediaCursor.getLong(1);
                                android.util.Log.i(TAG, "Media:size " + size);
                                if(size == fileSize){
                                    rotation = mediaCursor.getInt(0);
                                    break;
                                }
                            }
                            android.util.Log.i(TAG, "Media:rotation " + rotation);
                            processImage(mPath, rotation);
                        } else {
                            android.util.Log.i(TAG, "Android Problem");
                            txtGotTime.setText("Android Problem");
                        }
                    }
                }
                catch (IOException exception) {
                    exception.printStackTrace();
                }
            } else if(resultCode == Activity.RESULT_CANCELED) {
                android.util.Log.i(TAG, "RESULT_CANCELED");
                txtGotTime.setText("RESULT_CANCELED");
            }
        }
    }

    private void processImage(final String filePath, final int rotation) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        options.inPurgeable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

            TessBaseAPI baseApi = new TessBaseAPI();
            baseApi.setDebug(true);
            baseApi.init(mDirPath, lang);
            baseApi.setPageSegMode(100);
            baseApi.setPageSegMode(7);
            baseApi.setImage(bitmap);
            String recognizedText = baseApi.getUTF8Text();
            android.util.Log.i(TAG, "recognizedText: 1 " + recognizedText);
            baseApi.end();
            if(lang.equalsIgnoreCase("eng")) {
                recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
            }
            android.util.Log.i(TAG, "recognizedText: 2 " + recognizedText.trim());
            txtGotTime.setText(recognizedText.trim());
        }
    }

    private void saveImageAndroid(final Bitmap passedBitmap) {
        try {
            FileOutputStream mFileOutStream = new FileOutputStream(mDirPath  + File.separator + "savedAndroid.jpg");
            passedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

tessdata文件夹放入Assets.

不要忘记给出参考路径Tess Library

谢谢。

于 2013-04-26T04:22:28.243 回答