所以我正在学习教程,下面是我的代码。我一直在努力弄清楚数据路径需要做什么。有没有人对如何拍摄我拍摄的位图照片并将其加载到tesseract中进行分析的示例或建议?所有帮助表示赞赏。

package com.example.cameraocr;

import java.io.File;

import com.googlecode.tesseract.android.TessBaseAPI;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
    private static ImageView imageView;

@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);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}
protected static void identifyunicode() {
    // DATA_PATH = Path to the storage
    // lang for which the language data exists, usually "eng"

    File myDir = getExternalFilesDir(Environment.MEDIA_MOUNTED);
    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(myDir, "eng");
}
}

最佳答案

看一下我的例子:

https://github.com/akiwarheit/plug-notes-android/blob/master/src/com/plug/note/NoteEditorActivity.java

我所做的就是打电话给相机,拍照,获取照片并将其传递给我的OCRTask类(一个AsyncTask),该类调用TessBaseAPI

  public void callCamera() {
    Log.d(TAG, "Starting camera...");
    Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, REQUEST_OCR);
  }


https://github.com/akiwarheit/plug-notes-android/blob/master/src/com/plug/note/OCRTask.java

(如果我将整个OCRTask类代码发布在这里有点长,那么也许可以在Github中阅读它)?

然后处理结果

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    /* bunch of other codes */
    if (requestCode == REQUEST_OCR) {
      if (resultCode == RESULT_OK) {
        Bitmap x = (Bitmap) data.getExtras().get("data");
        new OCRTask(this, x, this).execute();
      }
    }
  }


我刚刚将识别出的文本添加到了我的EditText

  @Override
  public void onFinishRecognition(String recognizedText) {
    noteView.setText(noteView.getText() + " " + recognizedText);
  }




这是课程

NoteEditor (calls the Camera intent)

OCRTask (calls the TessBaseApi, this is your main concern)

OCRCallback (Adds the text to my EditText after OCRTask finishes)

FileManager (util method)

希望能帮助到你。

关于java - Android tesseract数据路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16204204/

10-09 06:14