0

我是 tess-two 库的新手。我可以添加该库并从可绘制的图像中获取图像,甚至可以进行转换,但是我得到了错误的文本,如下所示:

这是我的完整代码:

Bitmap image;
private TessBaseAPI mTess;
String datapath = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //init image
    image = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);

    //initialize Tesseract API
    String language = "eng";
    datapath = getFilesDir()+ "/tesseract/";
    mTess = new TessBaseAPI();

    checkFile(new File(datapath + "tessdata/"));

    mTess.init(datapath, language);
}

private void checkFile(File file) {
    if (!file.exists()&& file.mkdirs()){
        copyFiles();
    }
    if(file.exists()) {
        String datafilepath = datapath+ "/tessdata/eng.traineddata";
        File datafile = new File(datafilepath);

        if (!datafile.exists()) {
            copyFiles();
        }
    }
}


public void processImage(View view){
    String OCRresult = null;
    mTess.setImage(image);     
    OCRresult = mTess.getUTF8Text();
    TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
    OCRTextView.setText(OCRresult);
}

private void copyFiles() {
    try {
        String filepath = datapath + "/tessdata/eng.traineddata";
        AssetManager assetManager = getAssets();

        InputStream instream = assetManager.open("tessdata/eng.traineddata");
        OutputStream outstream = new FileOutputStream(filepath);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = instream.read(buffer)) != -1) {
            outstream.write(buffer, 0, read);
        }


        outstream.flush();
        outstream.close();
        instream.close();

        File file = new File(filepath);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我收到如下文字:

mmmm.and,mmm,1111 等

任何帮助表示赞赏。

4

2 回答 2

2

我遇到过同样的问题。它已在 2 分钟前修复它,您必须将图像调整为更大的尺寸。我使用缩略图库来完成这项工作:

BufferedImage bigger = Thumbnails.of(oldImage).size(700, 500).asBufferedImage();

我希望它对我糟糕的英语有所帮助并道歉。

注意:有关调整大小的更多信息在这里

于 2017-02-07T03:09:46.480 回答
0

可能存在的问题:

  1. 不正确的 OCR 文本
  2. 在训练数据中添加关键字按照教程Tesseract 教程页面
于 2016-12-29T13:00:52.953 回答