所以我按照教程进行操作,下面是我的代码。我试图弄清楚数据路径需要什么。有没有人有一个例子或建议如何拍摄我拍摄的位图照片并将其加载到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");
}
}
答案 0 :(得分:2)
看看我的例子:
我所做的是调用相机,拍照,获取照片并将其传递给调用OCRTask
TessBaseAPI
课程(AsyncTask)
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)
希望它有所帮助。