Tess4j:tess4j java

时间:2016-02-09 15:16:34

标签: java ocr tesseract tess4j

我正在使用tess4j.jar编写程序。 该程序从图像中提取文本及其位置。 我收到这个错误:

Exception in thread "main" java.lang.Error: Invalid memory access
at net.sourceforge.tess4j.TessAPI1.TessBaseAPIRecognize(Native Method)
at TesseractUtility.TessBoxForLogo.run(TessBoxForLogo.java:50)

有趣的是,每张图片都没有出现。有人知道我在哪里有错误吗?

这是我的代码:

public static ArrayList<Info> run(String imageName, List<String[]> wordsToFind){
    int WayToGetInfo = TessAPI1.TessPageIteratorLevel.RIL_TEXTLINE;
    wordsToFind = ParsData.addAnomalie(wordsToFind);
    //Prepare lists
    ArrayList<Info> infos = new ArrayList<Info>();
    ArrayList<Rect> rectangles = new ArrayList<Rect>();

    //Buff image
    File imageFile = new File(imageName);
    BufferedImage image = null;
    ByteBuffer buf = null;
    int w = 0;
    int h = 0;
    try {
        image = ImageIO.read(new FileInputStream(imageFile));
        w = image.getWidth();
        h = image.getHeight();

        buf = ImageIOHelper.convertImageData(image);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    //Get image dimensions
    int bpp = image.getColorModel().getPixelSize();
    int bytespp = bpp / 8;
    int bytespl = (int) Math.ceil(w * bytespp);

    //Tess4 preparation
    TessBaseAPI handler = TessAPI1.TessBaseAPICreate();
    String treiningDataPath = "C:\\Users\\Karmen\\Documents\\Bitbucket\\Podjetje\\lib\\Tess4J\\tessdata";
    String lang = "slv";
    TessAPI1.TessBaseAPIInit3(handler, treiningDataPath, lang);
    TessAPI1.TessBaseAPISetPageSegMode(handler, TessAPI1.TessPageSegMode.PSM_AUTO_ONLY);
    TessAPI1.TessBaseAPISetImage(handler, buf, w, h, bytespp, bytespl);

    TessAPI1.TessBaseAPIRecognize(handler, null);

    TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handler);
    Pointer str = TessAPI1.TessResultIteratorGetUTF8Text(ri, WayToGetInfo);
    TessAPI1.TessPageIterator pi = TessAPI1.TessResultIteratorGetPageIterator(ri);
    TessAPI1.TessPageIteratorBegin(pi);

    //Iterate thru rows
    while (TessAPI1.TessPageIteratorNext(pi,WayToGetInfo) == TessAPI1.TRUE){
        str = TessAPI1.TessResultIteratorGetUTF8Text(ri, WayToGetInfo);
        if (str!=null){
            float confidence = TessAPI1.TessResultIteratorConfidence(ri, WayToGetInfo);
            String word = str.getString(0);
            IntBuffer leftB = IntBuffer.allocate(1);
            IntBuffer topB = IntBuffer.allocate(1);
            IntBuffer rightB = IntBuffer.allocate(1);
            IntBuffer bottomB = IntBuffer.allocate(1);

            TessAPI1.TessPageIteratorBoundingBox(pi, WayToGetInfo, leftB, topB, rightB, bottomB);
            int left = leftB.get();
            int top = topB.get();
            int right = rightB.get();
            int bottom = bottomB.get();
            Rect rectangle1 = new Rect((int) (left-0.05*left), 
                                       (int) (top-0.05*top), 
                                       (int) ((right+0.05*right)-(left-0.05*left)),
                                       (int) ((bottom+0.5*bottom)-(top-0.05*top)));
            rectangles.add(rectangle1);

            String kword = "";
            for (String[] s:wordsToFind){
                //System.out.println(s[1]);
                if (word.toLowerCase().contains(s[1].toLowerCase())
                        ){
                    Rect rect = new Rect(left, top, right-left,bottom-top);
                    infos.add(copmare(word, s, rect, confidence));
                    kword = s[0];
                }
            }       
        }
    }
    draw(imageName, rectangles);
    return infos;   
}

2 个答案:

答案 0 :(得分:1)

在运行tesseract

之前将图像转换为灰度

答案 1 :(得分:0)

该异常通常是由于Tesseract无法找到tessdata文件夹。数据路径应设置为该文件夹的父级。

String treiningDataPath = "C:\\Users\\Karmen\\Documents\\Bitbucket\\Podjetje\\lib\\Tess4J\\";