3

我正在尝试开发一个从屏幕截图中提取文本的应用程序,并使用这些数据(数字和文本)做一些事情。它有效,但不像我预期的那样,它根本不准确。奇怪的是,我的应用程序和google vision api 网站上的“try api”以不同的方式识别相同分辨率的相同屏幕截图

例如:我的应用程序将带有“410”作为文本的屏幕截图识别为“A10”。否则 google api 站点正确识别 410

我注意到 google Keep OCR 比我的应用更好,它使用相同的 api?作为 google Keep 或 google vison api 站点,我可以做些什么来改善我的应用程序中的文本识别?

这是我的代码:

ocr类

public class Ocr extends AppCompatActivity {

private static int RESULT_LOAD_IMAGE = 1;

String TAG = "MAIN ACTIVITY";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonLoadImage = (Button) findViewById(R.id.upload_btn);

    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        /**
         * on click method of the upload button
         * @param arg0
         */
        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

}


/**
 * In this method, we check if the activity that was triggered was indeed Image Gallery
 * (It is common to trigger different intents from the same activity and expects result from each).
 * For this we used RESULT_LOAD_IMAGE integer that we passed previously to startActivityForResult() method
 * @param requestCode
 * @param resultCode
 * @param data
 */

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

            ImageView imageView = (ImageView) findViewById(R.id.imgView);

            imageView.setImageBitmap(bitmap);

            // imageBitmap is the Bitmap image you're trying to process for text
            if (bitmap != null) {

                TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();

                if (!textRecognizer.isOperational()) {
                    // Note: The first time that an app using a Vision API is installed on a
                    // device, GMS will download a native libraries to the device in order to do detection.
                    // Usually this completes before the app is run for the first time.  But if that
                    // download has not yet completed, then the above call will not detect any text,
                    // barcodes, or faces.
                    // isOperational() can be used to check if the required native libraries are currently
                    // available.  The detectors will automatically become operational once the library
                    // downloads complete on device.
                    Log.w(TAG, "Detector dependencies are not yet available.");

                    // Check for low storage.  If there is low storage, the native library will not be
                    // downloaded, so detection will not become operational.
                    IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
                    boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;

                    if (hasLowStorage) {
                        Toast.makeText(this, "Low Storage", Toast.LENGTH_LONG).show();
                        Log.w(TAG, "Low Storage");
                    }
                }


                Frame imageFrame = new Frame.Builder()
                        .setBitmap(bitmap)
                        .build();

                SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);


                for (int i = 0; i < textBlocks.size(); i++) {

                    TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));

                    String text = textBlock.getValue();

                    Toast.makeText(this, text , Toast.LENGTH_SHORT).show();

                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 }
}

安卓清单

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.prova.ocr">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.gms.vision.DEPENDENCIES"
        android:value="ocr" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

摇篮

apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
    applicationId "com.prova.ocr"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.google.android.gms:play-services-vision:10.0.0+'
}
4

0 回答 0