use of org.bytedeco.javacpp.BytePointer in project Java-TestNG-Appium-Selenium by sceiler.
the class OCR method getText.
public String getText(File screenshotFile) {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(this.getClass().getClassLoader().getResource("").getPath(), "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
api.SetDebugVariable("debug_file", "/dev/null");
// Open input image with leptonica library
PIX image = pixRead(screenshotFile.getAbsolutePath());
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
String result = outText.getString().toLowerCase(Locale.ROOT);
// System.out.println("OCR output:\n" + outText.getString());
// Destroy used object and release memory
api.End();
outText.deallocate();
return result;
}
use of org.bytedeco.javacpp.BytePointer in project kurento-java by Kurento.
the class BrowserTest method ocr.
public String ocr(BufferedImage imgBuff) {
String parsedOut = null;
try {
// Color image to pure black and white
for (int x = 0; x < imgBuff.getWidth(); x++) {
for (int y = 0; y < imgBuff.getHeight(); y++) {
Color color = new Color(imgBuff.getRGB(x, y));
int red = color.getRed();
int green = color.getBlue();
int blue = color.getGreen();
if (red + green + blue > OCR_COLOR_THRESHOLD) {
// Black
red = green = blue = 0;
} else {
// White
red = green = blue = 255;
}
Color col = new Color(red, green, blue);
imgBuff.setRGB(x, y, col.getRGB());
}
}
// OCR recognition
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imgBuff, "png", baos);
byte[] imageBytes = baos.toByteArray();
TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
ByteBuffer imgBB = ByteBuffer.wrap(imageBytes);
PIX image = pixReadMem(imgBB, imageBytes.length);
api.SetImage(image);
// Get OCR result
BytePointer outText = api.GetUTF8Text();
// Destroy used object and release memory
api.End();
api.close();
outText.deallocate();
pixDestroy(image);
// OCR corrections
parsedOut = outText.getString().replaceAll("l", "1").replaceAll("Z", "2").replaceAll("O", "0").replaceAll("B", "8").replaceAll("G", "6").replaceAll("S", "8").replaceAll("'", "").replaceAll("‘", "").replaceAll("\\.", ":").replaceAll("E", "8").replaceAll("o", "0").replaceAll("fl", "0").replaceAll("fi", "6").replaceAll("§", "5").replaceAll("I", "1").replaceAll("T", "7").replaceAll("’", "").replaceAll("U", "0").replaceAll("D", "0");
if (parsedOut.length() > 7) {
parsedOut = parsedOut.substring(0, 7) + ":" + parsedOut.substring(8, parsedOut.length());
}
parsedOut = parsedOut.replaceAll("::", ":");
// Remove last part (number of frames)
int iSpace = parsedOut.lastIndexOf(" ");
if (iSpace != -1) {
parsedOut = parsedOut.substring(0, iSpace);
}
} catch (IOException e) {
log.warn("IOException in OCR", e);
}
return parsedOut;
}
use of org.bytedeco.javacpp.BytePointer in project workstation by JaneliaSciComp.
the class FFMpegLoader method allocateFrame.
private void allocateFrame(Frame f) throws Exception {
// Allocate video frame and an AVFrame structure for the RGB image
if ((picture = av_frame_alloc()) == null) {
throw new Exception("avcodec_alloc_frame() error: Could not allocate raw picture frame.");
}
if ((picture_rgb = av_frame_alloc()) == null) {
throw new Exception("avcodec_alloc_frame() error: Could not allocate RGB picture frame.");
}
int width = getImageWidth() > 0 ? getImageWidth() : _video_codec.width();
int height = getImageHeight() > 0 ? getImageHeight() : _video_codec.height();
_image.setHeight(height);
_image.setWidth(width);
int fmt = getPixelFormat();
// Determine required buffer size and allocate buffer
int size = avpicture_get_size(fmt, width, height);
_buffer_rgb = new BytePointer(av_malloc(size));
// Assign appropriate parts of buffer to image planes in picture_rgb
// Note that picture_rgb is an AVFrame, but AVFrame is a superset of AVPicture
avpicture_fill(new AVPicture(picture_rgb), _buffer_rgb, fmt, width, height);
// Assign to the frame so the memory can be deleted later
f.buffer_rgb = _buffer_rgb;
f.picture = picture;
f.picture_rgb = picture_rgb;
f.imageBytes.add(new byte[width * height]);
}
use of org.bytedeco.javacpp.BytePointer in project cs-actions by CloudSlang.
the class OcrService method extractAllText.
private static String extractAllText(TessBaseAPI api) throws Exception {
final BytePointer outText;
final String result;
outText = api.GetUTF8Text();
if (outText == null) {
throw new Exception(TESSERACT_PARSE_ERROR);
}
result = outText.getString();
outText.deallocate();
return result;
}
Aggregations