1

所以我正在关注教程,下面是我的代码。我一直在试图弄清楚我需要对数据路径做什么。有没有人有关于如何拍摄我拍摄的位图照片并将其加载到 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");
}
}
4

1 回答 1

2

看看我的例子:

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(调用相机意图)

OCRTask (调用TessBaseApi,这是您主要关心的问题)

EditTextOCRCallback(完成后将文本添加到我的OCRTask

文件管理器(实用方法)

希望能帮助到你。

于 2013-04-25T02:08:35.300 回答