0

我已经能够将我的代码导出到应用程序中,并使用本教程 中的 Tess-Two 文件将 tesseract 集成到其中。但是现在我遇到了问题;当图片被发送到 tesseract 时,它每次只返回相同的随机字符:'f' wig, W fin. A. " 5' ' ,{ >>zv' ' ~" > ';>'. 似乎是什么问题?

ImageView iv;
TextView tv;
protected Bitmap _image; 
protected Bitmap bmp; 
public static String The_path = "/mnt/sdcard/DCIM/100MEDIA/TESS.jpg";
public static String lang = "eng";
public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";
private int pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_AUTO;

@Override
public void onCreate(Bundle savedInstanceState) {
    try {

        AssetManager assetManager = getAssets();
        InputStream in = assetManager.open("tessdata/eng.traineddata");
        //GZIPInputStream gin = new GZIPInputStream(in);
        OutputStream out = new FileOutputStream(DATA_PATH + "tessdata/eng.traineddata");

        //Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        //while ((len = gin.read(buf)) > 0) {
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        //gin.close();
        out.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    };  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    iv = (ImageView) findViewById(R.id.imageView1);
    tv = (TextView) findViewById(R.id.textView1);

    Button but = (Button) findViewById(R.id.button1);
    Button but2 = (Button) findViewById(R.id.button2);
    but.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            //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);
        }
    });

    but2.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            TessBaseAPI baseApi = new TessBaseAPI();
            baseApi.setDebug(true);
            baseApi.setPageSegMode(pageSegmentationMode);
            baseApi.init(DATA_PATH, lang); 
            // DATA_PATH = Path to the storage
            // lang for which the language data exists, usually "eng"
             try {     
                  baseApi.setImage(ReadFile.readBitmap(bmp));
                  String recognizedText = baseApi.getUTF8Text();
                  txt(recognizedText);

             } catch(RuntimeException e) {

             }
             baseApi.end();

        }

        private void txt(String txt) {
            // TODO Auto-generated method stub
            tv.setText(txt);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inSampleSize = 4;

    Bitmap bitmap = BitmapFactory.decodeFile(The_path, options);
    try {
        ExifInterface exif = new ExifInterface(The_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) {

            // Getting width & height of the given image.
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
        }
    bitmap = toGrayscale(bitmap);
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    } catch (IOException e) {

    }
    _image = (Bitmap) data.getExtras().get("data");
    iv.setImageBitmap(_image);
    bmp = bitmap;

}  public static Bitmap toGrayscale(Bitmap bmpOriginal){        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

这是添加灰度函数后的新日志猫:(接收一组新的重复出现的随机字符)

            01-26 10:21:36.973: D/libEGL(1389): loaded /system/lib/egl/libEGL_adreno200.so
            01-26 10:21:36.973: D/libEGL(1389): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
            01-26 10:21:36.973: D/libEGL(1389): loaded /system/lib/egl/libGLESv2_adreno200.so
            01-26 10:21:36.983: I/Adreno200-EGL(1389): <eglInitialize:269>: EGL 1.4 QUALCOMM build: Nondeterministic AU_full_mako_PARTNER-ANDROID/JB-MR1-                DEV_CL2946718_release_AU (CL2946718)
            01-26 10:21:36.983: I/Adreno200-EGL(1389): Build Date: 11/04/12 Sun
            01-26 10:21:36.983: I/Adreno200-EGL(1389): Local Branch: 
            01-26 10:21:36.983: I/Adreno200-EGL(1389): Remote Branch: m/partner-android/jb-mr1-dev
            01-26 10:21:36.983: I/Adreno200-EGL(1389): Local Patches: NONE
            01-26 10:21:36.983: I/Adreno200-EGL(1389): Reconstruct Branch: NOTHING
            01-26 10:21:37.043: D/OpenGLRenderer(1389): Enabling debug mode 0
            01-26 10:21:45.502: E/BitmapFactory(1389): Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/DCIM/100MEDIA/TESS.jpg: open failed: ENOENT (No such file or directory)
            01-26 10:21:45.502: E/JHEAD(1389): can't open '/mnt/sdcard/DCIM/100MEDIA/TESS.jpg'
            01-26 10:21:45.512: D/AndroidRuntime(1389): Shutting down VM
            01-26 10:21:45.512: W/dalvikvm(1389): threadid=1: thread exiting with uncaught exception (group=0x417b0930)
            01-26 10:21:45.522: E/AndroidRuntime(1389): FATAL EXCEPTION: main
            01-26 10:21:45.522: E/AndroidRuntime(1389): java.lang.RuntimeException:                 Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.cam.calc/cam.calc.Main}: java.lang.NullPointerException
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.os.Handler.dispatchMessage(Handler.java:99)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.os.Looper.loop(Looper.java:137)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.ActivityThread.main(ActivityThread.java:5039)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at java.lang.reflect.Method.invokeNative(Native Method)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at java.lang.reflect.Method.invoke(Method.java:511)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at dalvik.system.NativeStart.main(Native Method)
            01-26 10:21:45.522: E/AndroidRuntime(1389): Caused by: java.lang.NullPointerException
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at cam.calc.Main.onActivityResult(Main.java:388)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     at     android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
            01-26 10:21:45.522: E/AndroidRuntime(1389):     ... 11 more
4

1 回答 1

1

您需要在 OCR 之前处理图像,至少将图像转换为黑白(灰度)可以增强结果:

bitmap = toGrayscale(bitmap);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

您可以使用 OpenCV 或任何图像处理库,对于像灰度这样的简单任务,它的使用范围很广

import android.graphics.*;

public static Bitmap toGrayscale(Bitmap bmpOriginal)
    {        
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }

此外,如果图像太大,您可能需要裁剪图像并获取包含您要识别的字符和数字的区域,这也使 OCR 引擎的工作更容易/更准确,有时它会阻止您出现 OutOfMemoryException。

于 2013-01-26T08:37:16.817 回答