我已经接受了答案,但这是该答案的结果:
public Page recognizeTextBlocks(Path path) {
log.info("TessBaseAPIGetComponentImages");
File image = new File(path.toString());
Leptonica leptInstance = Leptonica.INSTANCE;
Pix pix = leptInstance.pixRead(image.getPath());
Page blocks = new Page(pix.w,pix.h);
api.TessBaseAPIInit3(handle, datapath, language);
api.TessBaseAPISetImage2(handle, pix);
api.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO_OSD);
PointerByReference pixa = null;
PointerByReference blockids = null;
Boxa boxes = api.TessBaseAPIGetComponentImages(handle, TessPageIteratorLevel.RIL_BLOCK, FALSE, pixa, blockids);
int boxCount = leptInstance.boxaGetCount(boxes);
for (int i = 0; i < boxCount; i++) {
Box box = leptInstance.boxaGetBox(boxes, i, L_CLONE);
if (box == null) {
continue;
}
api.TessBaseAPISetRectangle(handle, box.x, box.y, box.w, box.h);
Pointer utf8Text = api.TessBaseAPIGetUTF8Text(handle);
String ocrResult = utf8Text.getString(0);
Block block = null;
if(ocrResult == null || (ocrResult.replace("\n", "").replace(" ","")).length() == 0){
block = new ImageBlock(new Rectangle(box.x, box.y, box.w, box.h));
}else{
block = new TextBlock(new Rectangle(box.x, box.y, box.w, box.h), ocrResult);
}
blocks.add(block);
api.TessDeleteText(utf8Text);
int conf = api.TessBaseAPIMeanTextConf(handle);
log.debug(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box.x, box.y, box.w, box.h, conf, ocrResult));
}
//release Pix resource
PointerByReference pRef = new PointerByReference();
pRef.setValue(pix.getPointer());
leptInstance.pixDestroy(pRef);
return blocks;
}
注意:类 Block、ImageBlock 和 TextBlock 来自我的项目,不是 tess4j 或 tesseract 的一部分