java - native : Could not initialize Tesseract API with language=eng

标签 java android ocr tesseract

请原谅任何不好的英语,因为这是我第一次在 stackoverflow 上发布问题。

我想使用 tesseract OCR 引擎创建一个 OCR Android 应用程序,但遇到以下错误,我尝试四处搜索,但没有找到任何解决方案,非常感谢您的帮助。谢谢。

我正在尝试的代码:

TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init(Environment.getExternalStorageDirectory().toString()+"/", `"eng");`

我已经在设备根目录下创建了tessdata文件夹,里面有eng.traineddata文件,但是访问该函数时提示如下错误。

Could not initialize Tesseract API with language=eng!

我使用的是 Android 6.0.1,API 23

非常感谢任何帮助!先谢谢了~

最佳答案

试试这个代码。它允许您拍照并显示文本。此代码中存在小错误。在记事本中键入的字母上尝试此代码 忽略 tessdata 文件夹中放置的各种文件。我正在尝试阅读数学方程,因此我需要那些。我已经注释掉了其他文件,它不应该打扰你。如果你愿意尝试,可以尝试Mobile Vision API。 希望这有帮助:)

public class MainActivity extends AppCompatActivity {
    String imgPath;
    Bitmap imgBitmap;
    Uri imgUri;
    InputStream trainDataInputStream;
    OutputStream trainDataOutputStream;
    AssetManager assetManager;
    String externalDataPath;
    TextView t;
    String[] fileToBeCopied = {"eng.cube.bigrams", "eng.cube.fold", "eng.cube.lm", "eng.cube.nn", "eng.cube.params", "eng.cube.size", "eng.cube.word-freq", "eng.tesseract_cube.nn", "eng.traineddata","equ.traineddata"};
    ProgressDialog pDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (TextView) findViewById(R.id.text);
        new CopyFile().execute();
        //placeFileFromAssetsToExternalStorage();
        takePicture();

    }

    class CopyFile extends AsyncTask {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Fetching image...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[0]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[1]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[2]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[3]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[4]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[5]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[6]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[7]);
            placeFileFromAssetsToExternalStorage(fileToBeCopied[8]);
            //placeFileFromAssetsToExternalStorage(fileToBeCopied[9]);

            return null;
        }


        @Override
        protected void onPostExecute(Object o) {
            pDialog.dismiss();

        }
    }

    private void takePicture() {
        File photoFile = null;
        Intent iPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (iPicture.resolveActivity(getPackageManager()) != null) {

            try {
                photoFile = createImageFile();
            } catch (Exception e) {
                e.printStackTrace();
            }

            //if photo file is created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.scorpio.fileprovider", photoFile);
                System.out.println(imgPath);
                iPicture.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(iPicture, 1);

            }
        }
    }

    private File createImageFile() {
        File imgFile = null;
        String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File storageDir = Environment.getExternalStorageDirectory();
        try {
            imgFile = File.createTempFile(fileStamp, ".jpeg", storageDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        imgPath = imgFile.getAbsolutePath();
        return imgFile;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            galleryAddPic();
        }
    }

    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imgPath);
        System.out.println("Image path ->" + imgPath);
        Uri contentUri = Uri.fromFile(f);
        imgUri = contentUri;
        System.out.println("Image uri " + imgUri);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        ocrImage();
    }


    public void ocrImage() {
        try {
            //getting image for ocr
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            imgBitmap = BitmapFactory.decodeFile(imgPath, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imgPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        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 = imgBitmap.getWidth();
            int h = imgBitmap.getHeight();

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

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            imgBitmap = Bitmap.createBitmap(imgBitmap, 0, 0, w, h, mtx, false);
        }
        imgBitmap = imgBitmap.copy(Bitmap.Config.ARGB_8888, true);
        TessBaseAPI baseApi = new TessBaseAPI();
        baseApi.init(externalDataPath, "eng");
        baseApi.setImage(imgBitmap);
        String ocrResult = baseApi.getUTF8Text();
        System.out.println(ocrResult);
        baseApi.end();
        t.setText(ocrResult);

    }

    public void placeFileFromAssetsToExternalStorage(String filename) {
        System.out.println("Running DataRunnable class ");
        assetManager = getResources().getAssets();
        externalDataPath = Environment.getExternalStorageDirectory() + "/tessdata";
        System.out.println("external data path " + externalDataPath);
        //creating eng.trainedData
        File f = new File(externalDataPath);
        try {
            if (!f.exists()) {
                f.mkdir();
            }
            externalDataPath = externalDataPath + "/" + filename;
            f = new File(externalDataPath);

            if (!f.exists())
                f.createNewFile();

            externalDataPath = Environment.getExternalStorageDirectory().toString();

            trainDataInputStream = assetManager.open(filename);
            trainDataOutputStream = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = trainDataInputStream.read(buffer)) != -1) {
                trainDataOutputStream.write(buffer, 0, read);
            }
            trainDataOutputStream.flush();
            trainDataOutputStream.close();
            trainDataInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

关于java - native : Could not initialize Tesseract API with language=eng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42826779/

相关文章:

Java 下载大文件意外停止

java - Mockito.when 给出 InvalidUseOfMatchersException : Misplaced or misused argument matcher detected here

android - AndEngine中如果触摸是sprite或body,如何检查触摸事件?

android - Android 应用程序中的两个微调器

android - 每 1 小时重复通知

c# - OCR线检测

java - 嵌套 GridBagLayout - 如何将子级的列与父级的列对齐

java - 如何为selenium java代码添加循环条件

image - 使用 Image Magick 创建用于 OCR 的双色图像

python - 如何使用python从图像中提取文本或数字