package com.github.axet.lookup;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import com.github.axet.lookup.common.FontFamily;
import com.github.axet.lookup.common.FontSymbol;
import com.github.axet.lookup.common.FontSymbolLookup;
import com.github.axet.lookup.common.ImageBinary;
import com.github.axet.lookup.common.ImageBinaryGrey;
public class OCR extends OCRCore {
public OCR(float threshold) {
* 1 - exact match. 0 - not match. -1 - opposite difference
public void setThreshold(float threshold) {
this.threshold = threshold;
public float getThreshold() {
* Load fonts / symbols from a class directory or jar file
* class name, corresponded to the resources.
* com.example.MyApp.class
* path to the fonts folder. directory should only contain
* folders with fonts which to load
public void loadFontsDirectory(File path) {
System.out.println("Not a Directory");
public void recursive(File path){
File[] str = path.listFiles();
if(!s.getName().endsWith("db"));
//System.out.println(s.getName());
//loadFont(new File(path, s));
* Load specified font family to load
* class name, corresponded to the resources.
* com.example.MyApp.class
* path to the fonts folder. directory should only contain
* folders with fonts which to load.
* name of the font to load
public void loadFont(File path) {
int i = path.getName().lastIndexOf ( "." );
String symbol = i == -1 ? path.getName() : path.getName().substring ( 0, i );
symbol = URLDecoder.decode(symbol, "UTF-8");
} catch (UnsupportedEncodingException ee) {
throw new RuntimeException(ee);
is = new FileInputStream(path);
String name = path.getName();
loadFontSymbol(name, symbol, is);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
public void loadFontSymbol(String fontName, String fontSymbol, InputStream is) {
BufferedImage b = Capture.load(is);
FontFamily ff = fontFamily.get(fontName);
ff = new FontFamily(fontName);
fontFamily.put(fontName, ff);
// b = prepareImageCrop(b);
FontSymbol f = new FontSymbol(ff, fontSymbol, b);
public String recognize(BufferedImage bi) {
// bi = prepareImage(bi);
ImageBinary i = new ImageBinaryGrey(bi);
public String recognize(ImageBinary i) {
List<FontSymbol> list = getSymbols();
return recognize(i, 0, 0, i.getWidth() - 1, i.getHeight() - 1, list);
* use font in the specified folder only
public String recognize(BufferedImage bi, String fontSet) {
ImageBinary i = new ImageBinaryGrey(bi);
return recognize(i, fontSet);
public String recognize(ImageBinary i, String fontSet) {
List<FontSymbol> list = getSymbols(fontSet);
return recognize(i, 0, 0, i.getWidth() - 1, i.getHeight() - 1, list);
public String recognize(ImageBinary i, int x1, int y1, int x2, int y2) {
List<FontSymbol> list = getSymbols();
return recognize(i, x1, y1, x2, y2, list);
public String recognize(ImageBinary i, int x1, int y1, int x2, int y2, String fontFamily) {
List<FontSymbol> list = getSymbols(fontFamily);
return recognize(i, x1, y1, x2, y2, list);
public String recognize(ImageBinary i, int x1, int y1, int x2, int y2, List<FontSymbol> list) {
List<FontSymbolLookup> all = findAll(list, i, x1, y1, x2, y2);
Collections.sort(all, new BiggerFirst());
// big images eat small ones
for (int k = 0; k < all.size(); k++) {
FontSymbolLookup kk = all.get(k);
for (int j = k + 1; j < all.size(); j++) {
FontSymbolLookup jj = all.get(j);
// sort top/bottom/left/right
Collections.sort(all, new Left2Right());
for (FontSymbolLookup s : all) {
int maxCX = Math.max(cx, s.getWidth());
// if distance betten end of previous symbol and begining of the
// current is larger then a char size, then it is a space
if (s.x - (x + cx) >= maxCX)
// if we drop back, then we have a end of line