我正在使用 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;
}