0

当我从 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();
        }
    }
4

2 回答 2

0

我使用以下代码让它工作。

在我的 RecognizeActivy 中拍照:

            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();
    }
}
于 2012-09-01T19:56:06.370 回答
0

如果您在同一个活动中实现 RecognitionCallback,您应该先实现它,然后调用:recognitionText(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");
                }
            }
        }
    }
于 2014-08-26T22:22:10.673 回答