Fuente: https://github.com/tesseract-ocr/tesseract
A continuación, mediante una simple aplicación consola de java les mostrare paso a paso como hacer uso de este poderoso motor.
Contenido:
- Descargar la libreria.
- Estructura del proyecto en eclipse
- Escanear una imagen y definicion de la region a realizar la extraccion del texto.
- Desarrollo de la aplicacion.
- Ejecucion y verificacion de los resultados.
1. Descargar la libreria. Aquí: https://code.google.com/p/tesseract-ocr/downloads/list
2. Estructura del proyecto en eclipse. Ver imagen a continuacion de como se debería ver tu proyecto.
3. Para mi ejemplo he escaneado el documento de identidad en mi país y he definido manualmente la región de extracción donde se encuentra el numero identificador. La región encerrada con el recuadro rojo, es la región a obtener mediante el motor tesseract.
4. Solo cree un archivo donde tengo un metodo principal y alli defino el lenguaje a utilizar, en mi caso español, creo un objeto Rectangle con las coordenadas de la región a realizar la extracción e imprimo por consola el texto procesado.
public class TesseractUtil {
/**
* Region
*/
private static Rectangle DEFAULT_RECTANGLE_NUMERO_CEDULA_SIZE = new Rectangle(
340, 118, 300, 60);
private static String TYPE_DEFAULT_IMAGE = "jpg";
private static String DEFAULT_LANGUAJE = "spa";
public static void main(String[] args) throws IOException,
TesseractException {
File file = new File("C:\\Users\\usuario\\Pictures\\williams.jpg");
System.out.println("Identificador: " + getIdentificadorCedula(file));
}
public static String getIdentificadorCedula(File imageFile)
throws IOException, TesseractException {
Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
instance.setLanguage(DEFAULT_LANGUAJE);
BufferedImage image = bufferedImage(imageFile);
makeGray(image);
String result = instance.doOCR(image,
DEFAULT_RECTANGLE_NUMERO_CEDULA_SIZE);
result = cleanIdentificadorCedula(result);
return result;
}
public static BufferedImage bufferedImage(File file) throws IOException {
return ImageIO.read(file);
}
public static String cleanIdentificadorCedula(String identificadorCedula) {
String finalString = StringUtils.deleteWhitespace(identificadorCedula)
.trim().toUpperCase();
return finalString.replaceAll("[^\\d]", "");
}
public static BufferedImage cropImage(BufferedImage src, Rectangle rect) {
BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width,
rect.height);
return dest;
}
public static void saveImage(String outputFolder, String filename,
BufferedImage image, String type) throws IOException {
File folder = new File(outputFolder);
if (!folder.exists()) {
folder.mkdir();
}
folder = new File(outputFolder);
File save_path = new File(folder.getPath() + "\\" + filename + "."
+ TYPE_DEFAULT_IMAGE);
ImageIO.write(image, type, save_path);
}
public static void makeGray(BufferedImage img) {
for (int x = 0; x < img.getWidth(); ++x)
for (int y = 0; y < img.getHeight(); ++y) {
int rgb = img.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb & 0xFF);
int gray = (r + g + b) / 3;
img.setRGB(x, y, gray);
}
}
}
5. Salida por la consola:
Identificador: 55555555

