我正在android中构建一个基于OCR的应用程序,用户可以在其中拍摄并剪切它。图像转换为位图然后转换为文本,但我无法将图像转换为文本。我认为getResults()中存在错误.TessOCR类的代码仅来自堆栈溢出。请帮我解决一下这个。 谢谢。
`private void processing1() {
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(Main2Activity.this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TessOCR(assetManager);
new TessOCR(assetManager).getResults(thePic);
intent = getIntent();
Bundle bd = intent.getExtras();
if(bd != null)
{
String getName = (String) bd.get("key");
tv.setText(getName);
}
Toast toast = Toast.makeText(Main2Activity.this, "SCANNED", Toast.LENGTH_SHORT);
toast.show();
new TessOCR(assetManager).onDestroy();
}
});
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE) {
picUri = data.getData();
performCrop();
} else if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
thePic = extras.getParcelable("data");
picView.setImageBitmap(thePic);
}
}
}
private void performCrop() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}`
我的TessOCR课程在
之下 `public static final String DATA_PATH = Environment
.getExternalStorageDirectory().toString() + "/AndroidOCR/";
public static final String lang = "eng";
private static final String TAG = "TESSERACT";
private AssetManager assetManager;
private TessBaseAPI mTess;
Context context;
public TessOCR(AssetManager assetManager) {
Log.i(TAG, DATA_PATH);
this.assetManager = assetManager;
String[] paths = new String[]{DATA_PATH, DATA_PATH + "tessdata/"};
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
}
}
}
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
OutputStream out = new FileOutputStream(new File(DATA_PATH + "tessdata/", lang + ".traineddata"));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
}
}
mTess = new TessBaseAPI();
mTess.setDebug(true);
mTess.init(DATA_PATH, lang);
}
public String getResults(Bitmap bitmap) {
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
Intent intent = new Intent(context, Main2Activity.class);
intent.putExtra(result,"key");
return result;
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
}`