OCR resource

http://www.velocityreviews.com/forums/t126795-java-ocr-.html

I know that it's quite a long time that those posts are here but I found them while looking for an OCR solution in Java, and I would like to share the FREE answer I have created.

I browsed lots of posts while searching for OCR in Java, and all was linking to Asprise / javaocr, but those are unaffordable for non-commercial project.

So I searched for OCR software, without language prereq, in the purpose to interface it with Java.

-I discovered GOCR (http://jocr.sourceforge.net/) which is an ocr in command line. It was a beginning ^^ I downloaded and used Windows version. After few tests I was able to figure how to use it but I've to feed it with PPM images.

-Here come the second software nconvert (http://pagesperso-orange.fr/pierre.g..._nconvert.html) which can convert images to PPM.

So I have done 2 static classes to act like OCR.

The main part is the class OCR, which take a screenshot of the screen, put the proper color (I've made gorc work only with Black letters on White background), write the image to the disk and then call nconvert and gorc.

By parsing outputstream of GOCR process you should have your text recognized. There is the "replace" thing in return because I work on numbers and gorc make some mistakes with 1-l and O-0 ^^

That's not a Strong OCR facility but it can help with little application. Hope it'll help and lot of thanks to nconvert and gocr


package t3x.tnn.utility;

import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class OCR {
	static public String recognize(Point hg, Point bd, Color color, boolean isColorEcriture){
		String res = null;
		File fImg = new File("screenshot.png");
		while(res == null){
			BufferedImage img = ScreenHandler.getScreen(hg, bd);
			if(isColorEcriture)
				img = changeWithColorEcriture(img, color);
			else
				img = changeWithColorFond(img, color);
			try {
				ImageIO.write(img, "PNG", fImg);
				Process p = Runtime.getRuntime().exec("nconvert -out ppm -o text.ppm screenshot.png");
				p.waitFor();
				p.destroy();
				p = Runtime.getRuntime().exec("gocr045 text.ppm");
				p.waitFor();
				if(p.getInputStream().available()>0)
					res = IOHandler.getResponse(p.getInputStream());
				p.destroy();
			}catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(fImg.exists())
			fImg.delete();
		File texte = new File("text.ppm");
		if(texte.exists())
			texte.delete();
		return res.replace("l", "1").replace("O", "0").trim();
	}

	private static BufferedImage changeWithColorEcriture(BufferedImage bi, Color ecriture) {
		if (bi != null) {                       
			int w = bi.getWidth();
			int h = bi.getHeight();
			int pixel;
			BufferedImage bitmp = new BufferedImage(w, h, bi.getType());
			BufferedImage biOut = new BufferedImage(w, h, bi.getType());

			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					pixel = bi.getRGB(x, y);
					if(pixel != ecriture.getRGB())
						pixel = Color.BLUE.getRGB();
					else
						pixel = Color.BLACK.getRGB();
					bitmp.setRGB(x, y, pixel); 
				}
			}

			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					pixel = bitmp.getRGB(x, y);
					if(pixel == Color.BLUE.getRGB())
						pixel = Color.WHITE.getRGB();
					biOut.setRGB(x, y, pixel);
				}
			}

			return biOut;
		} else {
			return bi;
		}
	}
	
	private static BufferedImage changeWithColorFond(BufferedImage bi, Color fond) {
		if (bi != null) {                       
			int w = bi.getWidth();
			int h = bi.getHeight();
			int pixel;
			BufferedImage bitmp = new BufferedImage(w, h, bi.getType());
			BufferedImage biOut = new BufferedImage(w, h, bi.getType());

			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					pixel = bi.getRGB(x, y);
					if(pixel == fond.getRGB())
						pixel = Color.BLUE.getRGB();
					else
						pixel = Color.WHITE.getRGB();
					bitmp.setRGB(x, y, pixel); 
				}
			}

			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					pixel = bitmp.getRGB(x, y);
					if(pixel == Color.BLUE.getRGB())
						pixel = Color.WHITE.getRGB();
					biOut.setRGB(x, y, pixel);
				}
			}

			return biOut;
		} else {
			return bi;
		}
	}
}

package t3x.tnn.utility;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

public class ScreenHandler {

	public static Color getPixelColor(Point p){
		return getPixelColor(p.x, p.y);
	}

	public static BufferedImage getScreen(Point hg, Point bd){
		checkNano();
		return nano.createScreenCapture(new Rectangle(hg, new Dimension(bd.x-hg.x, bd.y-hg.y)));
	}
	
	public static boolean areImagesEqual(BufferedImage img1, BufferedImage img2){
		int[] timg1 = getPixels(img1);
		int[] timg2 = getPixels(img2);
		for(int i = 0 ; i < timg1.length; i++){
			if(timg1[i]!=timg2[i]){
				return false;
			}
		}
		return true;
	}
	
	public static Color analyse(Point depart, int deviation, Color fond){
		for(int i= depart.x; i < depart.x+deviation; i++){
			Color col = ScreenHandler.getPixelColor(i, depart.y);
			if(!col.equals(fond))
				return col;
		}
		//IOHandler.abort("[ScreenHandler.analyse] : Aucune couleur de jeu trouvée");
		return null;
	}
///////////////////////////////////////////////////////////////////////////////////
	private static Robot nano;
	
	private static Color getPixelColor(int x, int y){
		checkNano();
		return nano.getPixelColor(x, y);
	}
	
	private static int[] getPixels(BufferedImage img){
		return img.getRaster().getPixels(img.getRaster().getMinX(), img.getRaster().getMinY(),  img.getRaster().getWidth(), img.getRaster().getHeight(), new int[ img.getRaster().getWidth()*img.getRaster().getHeight()*10]);
	}
	
	private static void checkNano(){
		if(nano == null)
			try {
				nano = new Robot();
			} catch (AWTException e) {
				e.printStackTrace();
			}
	}
}

你可能感兴趣的:(java,windows,.net)