当我识别ABBYY名片中的文字时,它可以正常工作。当我用照片尝试时,我把它当作失败了。它确实适用于ABBYY的演示,因此它不是我的硬件。
有谁知道这是为什么?
我用来拍照的代码:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUESTCODE_PHOTO);
if(requestCode == REQUESTCODE_PHOTO){
if( resultCode == Activity.RESULT_OK){
RecognizerManager.recognizeText((Bitmap)data.getExtras().get("data"), this);
}
}
public static void recognizeText(final Bitmap bitmap, final RecognitionCallback listener){
RecognitionConfiguration config = new RecognitionConfiguration();
config.setRecognitionLanguages(Engine.getInstance().getLanguagesAvailableForOcr());
config.setRecognitionMode(RecognitionMode.FULL);
config.setImageProcessingOptions(RecognitionConfiguration.ImageProcessingOptions.FIND_ALL_TEXT);
RecognitionManager recManager = Engine.getInstance().getRecognitionManager(config);
try {
Object o = recManager.recognizeText(bitmap, listener);
Log.i("RESULT!", o.toString());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RecognitionFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
我使用以下代码让它工作。
在我的识别活动中拍照:
final File photo = new File( Environment.getExternalStorageDirectory(), genPhotoFileName() );
RecognizeActivity.this.imageUri = Uri.fromFile( photo );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra( MediaStore.EXTRA_OUTPUT, imageUri );
startActivityForResult(intent, REQUESTCODE_PHOTO);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUESTCODE_PHOTO){
if( resultCode == Activity.RESULT_OK){
if(this.imageUri != null){
try{
final AssetFileDescriptor assetFileDescriptor =
getApplicationContext().getContentResolver().openAssetFileDescriptor(this.imageUri, "r" );
long imageFileSize = assetFileDescriptor.getLength();
if( imageFileSize == AssetFileDescriptor.UNKNOWN_LENGTH ) {
throw new IOException( "UNKNOWN_LENGTH" );
}
InputStream is = assetFileDescriptor.createInputStream();
byte[] imageData = new byte[(int) imageFileSize];
is.read( imageData );
if(this.bitmap != null){
this.bitmap.recycle();
this.bitmap = null;
System.gc();
}
this.bitmap = BitmapFactory.decodeByteArray( imageData, 0, (int) imageFileSize, new Options() );
RecognizerManager.recognizeText(this.bitmap, this);
} catch(Exception e){
e.printStackTrace();
}
} else{
Log.e("ERROR", "img = null");
}
}
}
}
我正在回收位图,因为如果我不这样做,我会拍几张照片后得到OutOfMemory异常。
照片处理:
public static void recognizeText(final Bitmap bitmap, final RecognitionCallback progressListener){
final RecognitionConfiguration config = new RecognitionConfiguration();
config.setImageResolution(0);
config.setImageProcessingOptions( RecognitionConfiguration.ImageProcessingOptions.PROHIBIT_VERTICAL_CJK_TEXT);
for(RecognitionLanguage e : Engine.getInstance().getLanguagesAvailableForOcr()){
if(e.equals(RecognitionLanguage.English) || e.equals(RecognitionLanguage.Dutch)){
final Set<RecognitionLanguage> languages = EnumSet.noneOf( RecognitionLanguage.class );
languages.add(e);
config.setRecognitionLanguages(languages);
}
}
RecognitionManager recManager = Engine.getInstance().getRecognitionManager(config);
try {
Object o = recManager.recognizeText(bitmap, progressListener);
Log.i("RESULT!", o.toString());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RecognitionFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
如果你在同一个活动中实现RecognitionCallback,你应该首先实现它,然后调用:recognText(this.bitmap,this)而不是RecognizerManager.recognizeText(this.bitmap,this);也不要忘记实现所需的3种方法:onPrebuiltWordsInfoReady,onRecognitionProgress,onRotationTypeDetected
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUESTCODE_PHOTO){
if( resultCode == Activity.RESULT_OK){
if(this.imageUri != null){
try{
final AssetFileDescriptor assetFileDescriptor =
getApplicationContext().getContentResolver().openAssetFileDescriptor(this.imageUri, "r" );
long imageFileSize = assetFileDescriptor.getLength();
if( imageFileSize == AssetFileDescriptor.UNKNOWN_LENGTH ) {
throw new IOException( "UNKNOWN_LENGTH" );
}
InputStream is = assetFileDescriptor.createInputStream();
byte[] imageData = new byte[(int) imageFileSize];
is.read( imageData );
if(this.bitmap != null){
this.bitmap.recycle();
this.bitmap = null;
System.gc();
}
this.bitmap = BitmapFactory.decodeByteArray( imageData, 0, (int) imageFileSize, new Options() );
// CALL recognizeText(this.bitmap, this); instead of RecognizerManager.recognizeText(this.bitmap, this);
recognizeText(this.bitmap, this);
} catch(Exception e){
e.printStackTrace();
}
} else{
Log.e("ERROR", "img = null");
}
}
}
}