我正在尝试使用 tess-two 库来识别来自图像的文本。
这是我的代码:
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// recognize text
Bitmap temp = loadJustTakenImage(); //loads taken image from sdcard
Bitmap rotatedImage = rotateIfNeeded(temp); // rotate method i found in some tutorial
String text1 = recognizeText(rotatedImage);
}
});
识别文字方法:
(tessdata 文件夹与 eng.traineddata 和其他文件一起在下载中)
private String recognizeText(Bitmap bitmap) {
// TODO Auto-generated method stub
TessBaseAPI baseApi = new TessBaseAPI();
// DATA_PATH = Path to the storage
// lang = for which the language data exists, usually "eng"
baseApi.init(Environment.getExternalStorageDirectory().toString()
+ "/Download/", "eng");
// Eg. baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata",
// "eng");
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
return recognizedText;
}
旋转图像方法:
private Bitmap rotateIfNeeded(Bitmap bitmap) {
ExifInterface exif = null;
try {
exif = new ExifInterface(directoryPath+"/"+currentFileName+".jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int exifOrientation = exif
.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap & convert to ARGB_8888, required by tess
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
}
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
return bitmap;
}
例如,我收到的文字真是一团糟
对于这张图片:
我得到了这个文本:
,7‘
有时我只是得到一个空字符串。
我究竟做错了什么?