我正在开展OCR项目,但我遇到了TessBase API的问题; 首先,我不确切知道这个API究竟是什么以及为什么要使用它,尽管我编写的代码是在搜索时看到但仍然有错误
在那里,我必须将它作为一个lib包含在我的项目中
public void executeOCR() {
***TessBaseAPI baseApi = new TessBaseAPI();***
baseApi.setDebug(true);
baseApi.init(DATA_PATH, "eng");
Bitmap bmps = croppedBmp.copy(Bitmap.Config.ARGB_8888, true);
baseApi.setImage(bmps);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
// Toast.makeText(Crop_Activity.this, recognizedText, Toast.LENGTH_LONG)
// .show();
resultEditText.setText(recognizedText);
}
错误完全在粗线上:
***TessBaseAPI baseApi = new TessBaseAPI();***
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.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.googlecode.tesseract.android.TessBaseAPI;
public class Crop_Activity extends Activity {
Bitmap bmp;
RelativeLayout outerLayout, rectLayout;
ImageView originalImage;
Button captureButton, cropButton, sendButton, addButton;
EditText resultEditText;
PointF startpoint, endpoint;
Button translate;
String storage_path = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
storage_path = Environment.getExternalStorageDirectory().toString()
+ "/SimpleAndroidOCR/";
createDirectories();
if (!(new File(storage_path + "tessdata/eng.traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("Files/eng.traineddata.mp3");
OutputStream out = new FileOutputStream(storage_path
+ "tessdata/eng.traineddata");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_image_layout);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
outerLayout = (RelativeLayout) findViewById(R.id.outerLayout);
rectLayout = (RelativeLayout) findViewById(R.id.rect_relativeLayout);
originalImage = (ImageView) findViewById(R.id.origional_image);
captureButton = (Button) findViewById(R.id.captureButton);
captureButton.setOnClickListener(captureButton_onClick);
cropButton = (Button) findViewById(R.id.cropButton);
cropButton.setOnClickListener(cropButton_onClick);
addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(addButton_onClick);
rectLayout.bringToFront();
outerLayout.setOnTouchListener(new MyListener());
outerLayout.setOnClickListener(new MyListener());
resultEditText = (EditText) findViewById(R.id.resultEditText);
translate = (Button) findViewById(R.id.transbtn);
translate.setOnClickListener(translate_onClick);
}
private void createDirectories() {
String[] paths = new String[] { storage_path,
storage_path + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
return;
}
}
}
}
String DATA_PATH = "/mnt/sdcard/SimpleAndroidOCR/";
public void executeOCR() {
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, "eng");
Bitmap bmps = croppedBmp.copy(Bitmap.Config.ARGB_8888, true);
baseApi.setImage(bmps);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
// Toast.makeText(Crop_Activity.this, recognizedText, Toast.LENGTH_LONG)
// .show();
resultEditText.setText(recognizedText);
}
View.OnClickListener cropButton_onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
setCroppedImage();
executeOCR();
}
};
View.OnClickListener captureButton_onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
};
View.OnClickListener addButton_onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!resultEditText.getText().toString().equals("")) {
SharedData.result.add(resultEditText.getText().toString());
Toast.makeText(Crop_Activity.this, "Data added",
Toast.LENGTH_SHORT).show();
}
}
};
// View.OnClickListener sendButton_onClick = new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
//
// Intent i = new Intent(Crop_Activity.this, SendActivity.class);
//
// startActivity(i);
// }
// };
public static void launchGoogleTranslateApp(Activity activity) {
}
View.OnClickListener translate_onClick = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Intent LaunchIntent = getPackageManager()
// .getLaunchIntentForPackage(
// "com.google.android.apps.translate");
Intent LanuchIntent = new Intent();
startActivity(LanuchIntent);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
originalImage.setImageBitmap(bmp);
}
}
public void updateRect() {
int left = (int) Math.min(startpoint.x, endpoint.x);
int right = (int) Math.max(startpoint.x, endpoint.x);
int top = (int) Math.min(startpoint.y, endpoint.y);
int bottom = (int) Math.max(startpoint.y, endpoint.y);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(right
- left, bottom - top);
lp.leftMargin = left;
lp.topMargin = top;
rectLayout.setLayoutParams(lp);
}
Bitmap croppedBmp;
public void setCroppedImage() {
try {
Bitmap origionalBitmap = ((BitmapDrawable) originalImage
.getDrawable()).getBitmap();
double w = originalImage.getWidth();
double h = originalImage.getHeight();
double xFactor = origionalBitmap.getWidth() / w;
double yFactor = origionalBitmap.getHeight() / h;
int left = (int) Math.min(startpoint.x, endpoint.x);
int right = (int) Math.max(startpoint.x, endpoint.x);
int top = (int) Math.min(startpoint.y, endpoint.y);
int bottom = (int) Math.max(startpoint.y, endpoint.y);
croppedBmp = Bitmap.createBitmap(origionalBitmap,
(int) (left * xFactor), (int) (top * yFactor),
(int) (right * xFactor) - (int) (left * xFactor),
(int) (bottom * yFactor) - (int) (top * yFactor));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(Crop_Activity.this, "Error in Crop",
Toast.LENGTH_LONG).show();
}
}
class MyListener implements OnTouchListener, View.OnClickListener {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_MOVE) {
endpoint = new PointF(e.getX(), e.getY());
updateRect();
} else if (e.getAction() == MotionEvent.ACTION_DOWN) {
startpoint = new PointF(e.getX(), e.getY());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
0, 0);
rectLayout.setLayoutParams(lp);
} else if (e.getAction() == MotionEvent.ACTION_UP) {
endpoint = new PointF(e.getX(), e.getY());
updateRect();
cropButton.setEnabled(true);
}
return false;
}
public void onClick(View v) {
}
}
class getTextFromImage extends AsyncTask<String, String, String> {
protected String doInBackground(String... arg0) {
try {
executeOCR();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(Crop_Activity.this, "Error in OCR",
Toast.LENGTH_LONG).show();
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
}
}
答案 0 :(得分:1)
TessBaseApi是一类用于执行OCR的tesseract api。你可以在这个github repo上找到适用于Android的库的端口https://github.com/rmtheis/tess-two它很容易导入你的proyect你可以找到回购中的指令。