android - 使用 tess-two 库从 android 中的位图中提取数字

标签 android ocr tess-two

我想从位图中提取数字。我正在使用 tess-two 库,但它无法正确识别。

示例代码:

    @Override
        public void onClick(View v) {
        switch (v.getId()){
        case R.id.b2:
        InputStream is = null;
        try {
        is = getApplicationContext().getAssets().open("zak.jpeg");
        } catch (IOException e1) {
        e1.printStackTrace();
        }
         final Drawable drw = Drawable.createFromStream(is, null);
         bmp = ((BitmapDrawable) drw).getBitmap();

        TessBaseAPI baseApi = new TessBaseAPI();
           bmp =BITMAP_RESIZER(bmp,bmp.getWidth(),bmp.getHeight());
            bmp =convertToGrayscale(bmp);
             bmp =RemoveNoise(bmp);
             iv.setImageBitmap(bmp);

          baseApi.init("/mnt/sdcard/Download/", "eng");
          baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST,"1234567890");
          baseApi.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST,"!@#$%^&*   ()_+=-[]}{" +";:'\"\\|~`,./<>?");
          baseApi.setDebug(true);
         baseApi.setImage(bmp);

        String recognizedText = baseApi.getUTF8Text();
        tv.setText(" numbers : "+recognizedText.trim());
        Log.d("karim", recognizedText);
        baseApi.end();
             break;

Bitmap转灰度的方法:

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

         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,  Bitmap.Config.ARGB_8888);
         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;
     }

去除Bitmap噪声的方法:

  public Bitmap RemoveNoise(Bitmap bmap) {
        for (int x = 0; x < bmap.getWidth(); x++) {
        for (int y = 0; y < bmap.getHeight(); y++) {
        int pixel = bmap.getPixel(x, y);
        int R = Color.red(pixel);
        int G = Color.green(pixel);
        int B = Color.blue(pixel);
        if (R < 162 && G < 162 && B < 162)
        bmap.setPixel(x, y, Color.BLACK);
        }
    }
        for (int  x = 0; x < bmap.getWidth(); x++) {
        for (int y = 0; y < bmap.getHeight(); y++) {
        int pixel = bmap.getPixel(x, y);
        int R = Color.red(pixel);
        int G = Color.green(pixel);
        int B = Color.blue(pixel);
        if (R > 162 && G > 162 && B > 162)
              bmap.setPixel(x, y, Color.WHITE);
        }
    }
       return bmap;
    }

调整位图大小的方法:

   public Bitmap BITMAP_RESIZER(Bitmap bitmap,int newWidth,int newHeight) 
      {    
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

    }

The incorrect result.

我该如何解决这个问题?

最佳答案

也许这有点晚了,但无论如何,如果我理解正确的话,你想要的只是数字作为输出。

您提供的白名单没问题,但 tesseract 会强制将字母与白名单中指定的数字匹配。没有办法让它忽略某些字符,但您可以为整个字母表设置白名单,然后在代码中手动将字母与数字分开。

关于android - 使用 tess-two 库从 android 中的位图中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755076/

相关文章:

android - 如何在我的 Android 应用程序中获取 BroadcastReceiver for Action :android. intent.action.MAIN 和 android.intent.category.HOME

android - 媒体播放器 : Should have subtitle controller already set: KitKat

Android:按下按钮时拉出表情符号键盘

android - ListView 占用了 LinearLayout 中的所有空间

ios - 如何设置图像大小以提高 OCR 输出?

c++ - 使用 tesseract 进行字符识别

安卓NDK : iostream file not found

python - OCR手写数据显示svm.train()中的错误

android - 将 tess-two(Tesseract Tools for Android)库集成到 Android 工作室并构建 ndk

android - 获取数据目录路径(android)