【问题标题】:starting OCR project in android studio在 android studio 中启动 OCR 项目
【发布时间】:2015-11-30 16:15:57
【问题描述】:

我想在 android studio 上做一个简单的 OCR 项目,但是我开始有问题,因为不知道我是否正确添加了 tess-two,我关注了这个网站:http://www.codeproject.com/Articles/840623/Android-Character-Recognition 我只想要最简单的方法和正确安装NDK的简单方法

【问题讨论】:

    标签: android android-ndk


    【解决方案1】:

    我已经从库中构建了 jar 文件。关注my gist下载并使用库:

    1. 从名为 libs.zip 的以下链接下载 tesseract 库。 https://www.dropbox.com/s/9fwqz88sck3xlk4/libs.zip?dl=0

    2. 解压 zip 文件夹。

      • 如果您使用的是 Eclipse,请将所有文件和文件夹从 libs 文件夹复制到项目中的 libs 文件夹中。
      • 如果您使用的是 Android Studio,则将所有文件夹从 libs 文件夹复制到项目中的 src/main/jniLibs 文件夹并复制 classes.jar 到 libs 文件夹。
    3. 在您的下载文件夹中添加包含文本的图像并命名为 a.png。

    4. 在项目的 assets 文件夹中创建名为 tessdata 的文件夹。

    5. 从以下链接下载名为 eng.traineddata 的文件,并将其复制到第四步创建的 tessdata 文件夹中。 https://www.dropbox.com/s/7xdnfzp8qsy4ll9/eng.traineddata?dl=0

    6. 为清单“android.permission.WRITE_EXTERNAL_STORAGE”添加权限

    7. 复制并粘贴以下代码。

    try{
      bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/a.png");
    
      // _path = path to the image to be OCRed
      ExifInterface exif = new ExifInterface(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/a.png");
      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) {
          int w = bitmap.getWidth();
          int h = bitmap.getHeight();
    
          // Setting pre rotate
          Matrix mtx = new Matrix();
          mtx.preRotate(rotate);
    
          // Rotating Bitmap & convert to ARGB_8888, required by tess
          bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
      }
      bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
      TessBaseAPI baseApi = new TessBaseAPI();
    
      // tesseract reads language from tesseract folder, create it if not exists.
      File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract/tessdata");
      if(!f.exists()){
          f.mkdirs();
      }
    
      // copy the eng lang file from assets folder if not exists.
      File f1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract/tessdata/eng.traineddata");
      if(!f1.exists()){
          InputStream in = getAssets().open("tessdata/eng.traineddata");
          FileOutputStream fout = new FileOutputStream(f1);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
              fout.write(buf, 0, len);
            }
            in.close();
            fout.close();
      }
    
      // DATA_PATH = Path to the storage and data path must contain tessdata subdirectory
      // lang = for which the language data exists, usually "eng"
      // Eg. baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata", "eng");
      baseApi.init(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract", "eng");
    
      baseApi.setImage(bitmap);
      String recognizedText = baseApi.getUTF8Text();
      baseApi.end();
      Toast.makeText(getApplicationContext(), recognizedText, Toast.LENGTH_LONG).show();
      System.out.println("Text is>>>>>>>>>>>>>>>>>" + recognizedText);
    }catch(Exception e){
      e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      相关资源
      最近更新 更多