我正在尝试开发使用tess-two库将Image转换为Text的android应用程序,遵循本教程:
http://gaut.am/making-an-ocr-android-app-using-tesseract/
但是在拍摄照片后应用程序将关闭。
我的代码是:
protected Button _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_image = ( ImageView ) findViewById( R.id.imageView1 );
_field = ( TextView ) findViewById( R.id.textView1 );
_button = ( Button ) findViewById( R.id.button1 );
_path = Environment.getExternalStorageDirectory() + "/ImageOCR/make_machine_example.jpg";
_button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCameraActivity();
}
} );
}
protected void startCameraActivity()
{
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i( "MakeMachine", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "MakeMachine", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
_field.setVisibility( View.GONE );
//--------------------------------------------------------
ExifInterface exif;
try {
exif = new ExifInterface(_path);
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);
String DATA_PATH = Environment.getExternalStorageDirectory() + "/tesseract/";
String lang = "eng";
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init(DATA_PATH, lang);
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
Toast.makeText(this, recognizedText, 4000).show();
baseApi.end();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//---------------------------------------------------
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( MainActivity.PHOTO_TAKEN , _taken );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState)
{
Log.i( "MakeMachine", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( MainActivity.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
此处显示英语语言数据的路径:
/storage/sdcard0/tesseract/tessdata
并且logCat窗口显示以下日志:
03-20 02:24:25.094: D/libEGL(32009): loaded /system/lib/egl/libEGL_mali.so
03-20 02:24:25.104: D/libEGL(32009): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-20 02:24:25.104: D/libEGL(32009): loaded /system/lib/egl/libGLESv2_mali.so
03-20 02:24:25.109: D/(32009): Device driver API match
03-20 02:24:25.109: D/(32009): Device driver API version: 10
03-20 02:24:25.109: D/(32009): User space API version: 10
03-20 02:24:25.109: D/(32009): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
03-20 02:24:25.144: D/OpenGLRenderer(32009): Enabling debug mode 0
03-20 02:24:33.689: W/IInputConnectionWrapper(32009): showStatusIcon on inactive InputConnection
03-20 02:24:41.469: I/MakeMachine(32009): resultCode: -1
03-20 02:24:41.469: I/System.out(32009): Not a DRM File, opening notmally
03-20 02:24:41.469: I/System.out(32009): buffer returned
03-20 02:24:41.499: D/dalvikvm(32009): GC_FOR_ALLOC freed 60K, 8% free 12302K/13319K, paused 25ms, total 26ms
03-20 02:24:41.514: I/dalvikvm-heap(32009): Grow heap (frag case) to 20.515MB for 7990288-byte allocation
03-20 02:24:41.534: D/dalvikvm(32009): GC_CONCURRENT freed 3K, 5% free 20101K/21127K, paused 12ms+2ms, total 22ms
03-20 02:24:41.534: D/dalvikvm(32009): WAIT_FOR_CONCURRENT_GC blocked 6ms
03-20 02:24:41.959: D/dalvikvm(32009): GC_FOR_ALLOC freed 37K, 5% free 20077K/21127K, paused 10ms, total 10ms
03-20 02:24:41.989: I/dalvikvm-heap(32009): Grow heap (frag case) to 28.108MB for 7990288-byte allocation
03-20 02:24:42.019: D/dalvikvm(32009): GC_CONCURRENT freed <1K, 4% free 27880K/28935K, paused 13ms+2ms, total 27ms
03-20 02:24:42.019: D/dalvikvm(32009): WAIT_FOR_CONCURRENT_GC blocked 14ms
03-20 02:24:42.134: D/dalvikvm(32009): GC_FOR_ALLOC freed <1K, 4% free 27880K/28935K, paused 12ms, total 12ms
03-20 02:24:42.144: I/dalvikvm-heap(32009): Grow heap (frag case) to 35.728MB for 7990288-byte allocation
03-20 02:24:42.174: D/dalvikvm(32009): GC_CONCURRENT freed <1K, 3% free 35683K/36743K, paused 12ms+2ms, total 28ms
03-20 02:24:42.174: D/dalvikvm(32009): WAIT_FOR_CONCURRENT_GC blocked 1ms
03-20 02:24:42.189: D/dalvikvm(32009): Trying to load lib /data/data/com.example.imageocr/lib/liblept.so 0x42664f40
03-20 02:24:42.199: D/dalvikvm(32009): Added shared lib /data/data/com.example.imageocr/lib/liblept.so 0x42664f40
03-20 02:24:42.204: D/dalvikvm(32009): Trying to load lib /data/data/com.example.imageocr/lib/libtess.so 0x42664f40
03-20 02:24:42.259: D/dalvikvm(32009): Added shared lib /data/data/com.example.imageocr/lib/libtess.so 0x42664f40
03-20 02:24:42.259: I/Tesseract(native)(32009): Attempting Init() with dir=/storage/sdcard0/tesseract/, lang=eng
03-20 02:24:42.269: A/libc(32009): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 32009 (xample.imageocr)
答案 0 :(得分:0)
如果您需要使用其他应用来通过相机捕获图像并将其转换为文本,您可以使用我的应用:https://play.google.com/store/apps/details?id=sunbulmh.ocr
以下是您可以在应用中使用以获取OCR服务的示例代码:
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("sunbulmh.ocr", PackageManager.GET_ACTIVITIES);
Intent LaunchIntent = pm.getLaunchIntentForPackage("sunbulmh.ocr");
LaunchIntent.setFlags(0);
startActivityForResult(LaunchIntent,5);
} catch (NameNotFoundException e) {
Uri URLURI = Uri.parse("http://play.google.com/store/apps/details?id=sunbulmh.ocr");
Intent intent = new Intent(Intent.ACTION_VIEW,URLURI);
startActivity(intent);
}
然后,在onActivityResult()中获得结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 5){
String ocr_txt = data.getStringExtra(Intent.EXTRA_TEXT);
// ocr_txt contains the recognized text.
}
}
}