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();
}
}
}
分享到:
相关推荐
天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR开源版V5.0.0天若OCR...
OCR字符识别技术开发源代码,WINDOWS应用程序开发,字符识别技术
Aspose.OCR是一款字符识别组件,它使得开发人员可以添加OCR功能到他们的ASP.NET Web应用程序、web服务和windows应用程序中。它提供了一个简单的类集用于控制字符识别。Aspose.OCR目的是为那些需要在他们自己的应用...
用Visual Basic 6.0编写的OCR识别程序,对数字的识别率高
ocr文字识别工具
本地OCR服务,可代替在线OCR,突破OCR收费壁障,识别准确率与在线百度OCR基本无差! 支持的语言:中文、英文、日文、韩文 说明:本系统已完成打包,包内集成运行所需环境,电脑无需安装任何环境,解压双击即可运行。...
天若OCR文字识别.exe
OCR文字识别程序OCR文字识别程序OCR文字识别程序OCR文字识别程序OCR文字识别程序
javaocr的几个插件,包括javaocr-plugin-morphology-1.0.jar、javaocr-plugin-moment-1.0.jar、javaocr-plugin-fir-1.0.jar、javaocr-plugin-cluster-1.0.jar、javaocr-plugin-awt-1.0.jar、javaocr-core-1.0.jar
Nicomsoft OCR识别引擎SDK,支持32位、64位,支持VC VB JS、等开发语言,很好的识别引擎,识别能力不输清华文通OCR!
坛子里有破解版的Asprise-OCR-Java,其原理就是利用开启CloseWin.exe窗口扫描有没有Asprise弹出过期的窗口,更具信息框的标题栏来判断是否要关闭此信息窗口。2011年7月以后的使用版无论是点击确定还是关闭按钮,都要...
OCR文字识别程序OCR文字识别程序OCR文字识别程序OCR文字识别程序
用vb写的一个OCR工具,使用onenote的OCR接口。
OCR识别OCR识别OCR识别OCR识别OCR识别OCR识别
ocrserver, 由Golang编写的OCR服务器,可以通过 Docker 轻松部署 ocrserver 简单的OCR服务器,作为一个小的工作样本的 gosseract 。现在试试这里的 https://ocr-example.herokuapp.com/插件,现在部署你自己的。 ...
基于yolo的开源框架darknet的ocr系统。
Aspose.OCR是一款字符识别组件,它使得开发人员可以添加OCR功能到他们的ASP.NET Web应用程序、web服务和windows应用程序中。它提供了一个简单的类集用于控制字符识别。Aspose.OCR目的是为那些需要在他们自己的应用...
清华文通ocr软件文件,很好用的图文识别OCR软件 清华文通ocr软件文件,很好用的图文识别OCR软件
Aspose.OCR很好的实例,十分全面.Aspose.OCR是一款字符识别组件,它使得开发人员可以添加OCR功能到他们的ASP.NET Web应用程序、web服务和windows应用程序中。它提供了一个简单的类集用于控制字符识别