几天一直在研究OCR技术,据我了解的情况,国内最专业的OCR软件只有2家,清华TH-OCR和汉王OCR,看了很多的OCR技术发现好多对英文与数字的支持都很好,可惜很多都不支持中文字符。Asprise-OCR,Tesseract 3.0以前的版本,都不支持中文,其实我用了下Asprise-OCR算是速度比较的快了,可惜他鄙视中文,这个没有办法,正好这段时间知名的开源OCR引擎Tesseract 3.0版本发布了,他给我们带来的好消息就是支持中文,相关的下载项目网站是:
虽然速度不是很客观可是毕竟人家开始支持中文也算是不错的,一个英文的语言包大概是1.8M,中文简体的语言包是39.5M,中文繁体的语言包是53M,这样就知道为什么识别中文慢的原因了
packagecom.ocr;
importjava.awt.Graphics2D;
importjava.awt.color.ColorSpace;
importjava.awt.geom.AffineTransform;
importjava.awt.image.AffineTransformOp;
importjava.awt.image.BufferedImage;
importjava.awt.image.ColorConvertOp;
importjava.awt.image.ColorModel;
importjava.awt.image.MemoryImageSource;
importjava.awt.image.PixelGrabber;
/**
*
*图像过滤,增强OCR识别成功率
*
*/
publicclassImageFilter {
privateBufferedImageimage;
privateintiw,ih;
privateint[]pixels;
publicImageFilter(BufferedImage image) {
this.image= image;
iw= image.getWidth();
ih= image.getHeight();
pixels=newint[iw*ih];
}
/**图像二值化*/
publicBufferedImage changeGrey() {
PixelGrabber pg =newPixelGrabber(image.getSource(), 0, 0,iw,ih,pixels, 0,iw);
try{
pg.grabPixels();
}catch(InterruptedException e) {
e.printStackTrace();
}
//设定二值化的域值,默认值为100
intgrey = 100;
//对图像进行二值化处理,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for(inti = 0; i
intred, green, blue;
intalpha = cm.getAlpha(pixels[i]);
if(cm.getRed(pixels[i]) > grey) {
red = 255;
}else{
red = 0;
}
if(cm.getGreen(pixels[i]) > grey) {
green = 255;
}else{
green = 0;
}
if(cm.getBlue(pixels[i]) > grey) {
blue = 255;
}else{
blue = 0;
}
pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
}
//将数组中的象素产生一个图像
returnImageIOHelper.imageProducerToBufferedImage(newMemoryImageSource(iw,ih,pixels, 0,iw));
}
/**提升清晰度,进行锐化*/
publicBufferedImage sharp() {
PixelGrabber pg =newPixelGrabber(image.getSource(), 0, 0,iw,ih,pixels, 0,iw);
try{
pg.grabPixels();
}catch(InterruptedException e) {
e.printStackTrace();
}
//象素的中间变量
inttempPixels[] =newint[iw*ih];
for(inti = 0; i
tempPixels[i] =pixels[i];
}
//对图像进行尖锐化处理,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for(inti = 1; i
for(intj = 1; j
intalpha = cm.getAlpha(pixels[i *iw+ j]);
//对图像进行尖锐化
intred6 = cm.getRed(pixels[i *iw+ j + 1]);
intred5 = cm.getRed(pixels[i *iw+ j]);
intred8 = cm.getRed(pixels[(i + 1) *iw+ j]);
intsharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);
intgreen5 = cm.getGreen(pixels[i *iw+ j]);
intgreen6 = cm.getGreen(pixels[i *iw+ j + 1]);
intgreen8 = cm.getGreen(pixels[(i + 1) *iw+ j]);
intsharpGreen = Math.abs(green6 - green5) + Math.abs(green8 - green5);
intblue5 = cm.getBlue(pixels[i *iw+ j]);
intblue6 = cm.getBlue(pixels[i *iw+ j + 1]);
intblue8 = cm.getBlue(pixels[(i + 1) *iw+ j]);
intsharpBlue = Math.abs(blue6 - blue5) + Math.abs(blue8 - blue5);
if(sharpRed > 255) {
sharpRed = 255;
}
if(sharpGreen > 255) {
sharpGreen = 255;
}
if(sharpBlue > 255) {
sharpBlue = 255;
}
tempPixels[i *iw+ j] = alpha << 24 | sharpRed << 16 | sharpGreen << 8 | sharpBlue;
}
}
//将数组中的象素产生一个图像
returnImageIOHelper.imageProducerToBufferedImage(newMemoryImageSource(iw,ih, tempPixels, 0,iw));
}
/**中值滤波*/
publicBufferedImage median() {
PixelGrabber pg =newPixelGrabber(image.getSource(), 0, 0,iw,ih,pixels, 0,iw);
try{
pg.grabPixels();
}catch(InterruptedException e) {
e.printStackTrace();
}
//对图像进行中值滤波,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for(inti = 1; i
for(intj = 1; j
intred, green, blue;
intalpha = cm.getAlpha(pixels[i *iw+ j]);
// int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
intred4 = cm.getRed(pixels[i *iw+ j - 1]);
intred5 = cm.getRed(pixels[i *iw+ j]);
intred6 = cm.getRed(pixels[i *iw+ j + 1]);
// int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
//水平方向进行中值滤波
if(red4 >= red5) {
if(red5 >= red6) {
red = red5;
}else{
if(red4 >= red6) {
red = red6;
}else{
red = red4;
}
}
}else{
if(red4 > red6) {
red = red4;
}else{
if(red5 > red6) {
red = red6;
}else{
red = red5;
}
}
}
// int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
intgreen4 = cm.getGreen(pixels[i *iw+ j - 1]);
intgreen5 = cm.getGreen(pixels[i *iw+ j]);
intgreen6 = cm.getGreen(pixels[i *iw+ j + 1]);
// int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
//水平方向进行中值滤波
if(green4 >= green5) {
if(green5 >= green6) {
green = green5;
}else{
if(green4 >= green6) {
green = green6;
}else{
green = green4;
}
}
}else{
if(green4 > green6) {
green = green4;
}else{
if(green5 > green6) {
green = green6;
}else{
green = green5;
}
}
}
// int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
intblue4 = cm.getBlue(pixels[i *iw+ j - 1]);
intblue5 = cm.getBlue(pixels[i *iw+ j]);
intblue6 = cm.getBlue(pixels[i *iw+ j + 1]);
// int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
//水平方向进行中值滤波
if(blue4 >= blue5) {
if(blue5 >= blue6) {
blue = blue5;
}else{
if(blue4 >= blue6) {
blue = blue6;
}else{
blue = blue4;
}
}
}else{
if(blue4 > blue6) {
blue = blue4;
}else{
if(blue5 > blue6) {
blue = blue6;
}else{
blue = blue5;
}
}
}
pixels[i *iw+ j] = alpha << 24 | red << 16 | green << 8 | blue;
}
}
//将数组中的象素产生一个图像
returnImageIOHelper.imageProducerToBufferedImage(newMemoryImageSource(iw,ih,pixels, 0,iw));
}
/**线性灰度变换*/
publicBufferedImage lineGrey() {
PixelGrabber pg =newPixelGrabber(image.getSource(), 0, 0,iw,ih,pixels, 0,iw);
try{
pg.grabPixels();
}catch(InterruptedException e) {
e.printStackTrace();
}
//对图像进行进行线性拉伸,Alpha值保持不变
ColorModel cm = ColorModel.getRGBdefault();
for(inti = 0; i
intalpha = cm.getAlpha(pixels[i]);
intred = cm.getRed(pixels[i]);
intgreen = cm.getGreen(pixels[i]);
intblue = cm.getBlue(pixels[i]);
//增加了图像的亮度
red = (int) (1.1 * red + 30);
green = (int) (1.1 * green + 30);
blue = (int) (1.1 * blue + 30);
if(red >= 255) {
red = 255;
}
if(green >= 255) {
green = 255;
}
if(blue >= 255) {
blue = 255;
}
pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
}
//将数组中的象素产生一个图像
returnImageIOHelper.imageProducerToBufferedImage(newMemoryImageSource(iw,ih,pixels, 0,iw));
}
/**转换为黑白灰度图*/
publicBufferedImage grayFilter() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op =newColorConvertOp(cs,null);
returnop.filter(image,null);
}
/**平滑缩放*/
publicBufferedImage scaling(doubles) {
AffineTransform tx =newAffineTransform();
tx.scale(s, s);
AffineTransformOp op =newAffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
returnop.filter(image,null);
}
publicBufferedImage scale(Float s) {
intsrcW =image.getWidth();
intsrcH =image.getHeight();
intnewW = Math.round(srcW * s);
intnewH = Math.round(srcH * s);
//先做水平方向上的伸缩变换
BufferedImage tmp=newBufferedImage(newW, newH,image.getType());
Graphics2D g= tmp.createGraphics();
for(intx = 0; x < newW; x++) {
g.setClip(x, 0, 1, srcH);
//按比例放缩
g.drawImage(image, x - x * srcW / newW, 0,null);
}
//再做垂直方向上的伸缩变换
BufferedImage dst =newBufferedImage(newW, newH,image.getType());
g = dst.createGraphics();
for(inty = 0; y < newH; y++) {
g.setClip(0, y, newW, 1);
//按比例放缩
g.drawImage(tmp, 0, y - y * srcH / newH,null);
}
returndst;
}
}
packagecom.ocr;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.Toolkit;
importjava.awt.image.BufferedImage;
importjava.awt.image.DataBufferByte;
importjava.awt.image.ImageProducer;
importjava.awt.image.WritableRaster;
importjava.io.File;
importjava.io.IOException;
importjava.util.Iterator;
importjava.util.Locale;
importjavax.imageio.IIOImage;
importjavax.imageio.ImageIO;
importjavax.imageio.ImageReader;
importjavax.imageio.ImageWriteParam;
importjavax.imageio.ImageWriter;
importjavax.imageio.metadata.IIOMetadata;
importjavax.imageio.stream.ImageInputStream;
importjavax.imageio.stream.ImageOutputStream;
importjavax.swing.JOptionPane;
importcom.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;
publicclassImageIOHelper {
publicImageIOHelper() {
}
publicstaticFile createImage(File imageFile, String imageFormat) {
File tempFile =null;
try{
Iterator readers = ImageIO.getImageReadersByFormatName(imageFormat);
ImageReader reader = readers.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
// Read the stream metadata
IIOMetadata streamMetadata = reader.getStreamMetadata();
// Set up the writeParam
TIFFImageWriteParam tiffWriteParam =newTIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
// Get tif writer and set output to file
Iterator writers = ImageIO.getImageWritersByFormatName("tiff");
ImageWriter writer = writers.next();
BufferedImage bi = reader.read(0);
IIOImage image =newIIOImage(bi,null, reader.getImageMetadata(0));
tempFile = tempImageFile(imageFile);
ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);
writer.setOutput(ios);
writer.write(streamMetadata, image, tiffWriteParam);
ios.close();
writer.dispose();
reader.dispose();
}catch(Exception exc) {
exc.printStackTrace();
}
returntempFile;
}
publicstaticFile createImage(BufferedImage bi) {
File tempFile =null;
try{
tempFile = File.createTempFile("tempImageFile",".tif");
tempFile.deleteOnExit();
TIFFImageWriteParam tiffWriteParam =newTIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
// Get tif writer and set output to file
Iterator writers = ImageIO.getImageWritersByFormatName("tiff");
ImageWriter writer = writers.next();
IIOImage image =newIIOImage(bi,null,null);
tempFile = tempImageFile(tempFile);
ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);
writer.setOutput(ios);
writer.write(null, image, tiffWriteParam);
ios.close();
writer.dispose();
}catch(Exception exc) {
exc.printStackTrace();
}
returntempFile;
}
publicstaticFile tempImageFile(File imageFile) {
String path = imageFile.getPath();
StringBuffer strB =newStringBuffer(path);
strB.insert(path.lastIndexOf('.'), 0);
returnnewFile(strB.toString().replaceFirst("(?<=//.)(//w+)$","tif"));
}
publicstaticBufferedImage getImage(File imageFile) {
BufferedImage al =null;
try{
String imageFileName = imageFile.getName();
String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
Iterator readers = ImageIO.getImageReadersByFormatName(imageFormat);
ImageReader reader = readers.next();
if(reader ==null) {
JOptionPane.showConfirmDialog(null,
"Need to install JAI Image I/O package./nhttps://jai-imageio.dev.java.net");
returnnull;
}
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
reader.setInput(iis);
al = reader.read(0);
reader.dispose();
}catch(IOException ioe) {
System.err.println(ioe.getMessage());
}catch(Exception e) {
System.err.println(e.getMessage());
}
returnal;
}
publicstaticBufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage =newBufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0,null);
returnbufferedImage;
}
publicstaticBufferedImage imageProducerToBufferedImage(ImageProducer imageProducer) {
returnimageToBufferedImage(Toolkit.getDefaultToolkit().createImage(imageProducer));
}
publicstaticbyte[] image_byte_data(BufferedImage image) {
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
returnbuffer.getData();
}
}
packagecom.ocr;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
importorg.jdesktop.swingx.util.OS;
publicclassOCR {
privatefinalStringLANG_OPTION="-l";
privatefinalStringEOL= System.getProperty("line.separator");
privateStringtessPath=newFile("tesseract").getAbsolutePath();
//private String tessPath="C://Program Files (x86)//Tesseract-OCR//";
publicString recognizeText(File imageFile, String imageFormat)throwsException {
File tempImage = ImageIOHelper.createImage(imageFile, imageFormat);
File outputFile =newFile(imageFile.getParentFile(),"output");
StringBuffer strB =newStringBuffer();
List cmd =newArrayList();
if(OS.isWindowsXP()) {
cmd.add(tessPath+"//tesseract");
//cmd.add(tessPath + "//Tesseract-OCR");
}elseif(OS.isLinux()) {
cmd.add("tesseract");
}else{
//cmd.add(tessPath + "//Tesseract-OCR");
cmd.add(tessPath+"//tesseract");
}
cmd.add("");
cmd.add(outputFile.getName());
cmd.add(LANG_OPTION);
cmd.add("chi_sim");
cmd.add("eng");
ProcessBuilder pb =newProcessBuilder();
pb.directory(imageFile.getParentFile());
cmd.set(1, tempImage.getName());
pb.command(cmd);
pb.redirectErrorStream(true);
Process process = pb.start();
//tesseract.exe 1.jpg 1 -l chi_sim
intw = process.waitFor();
// delete temp working files
tempImage.delete();
if(w == 0) {
BufferedReader in =newBufferedReader(newInputStreamReader(newFileInputStream(outputFile
.getAbsolutePath()
+".txt"),"UTF-8"));
String str;
while((str = in.readLine()) !=null) {
strB.append(str).append(EOL);
}
in.close();
}else{
String msg;
switch(w) {
case1:
msg ="Errors accessing files. There may be spaces in your image's filename.";
break;
case29:
msg ="Cannot recognize the image or its selected region.";
break;
case31:
msg ="Unsupported image format.";
break;
default:
msg ="Errors occurred.";
}
tempImage.delete();
thrownewRuntimeException(msg);
}
newFile(outputFile.getAbsolutePath() +".txt").delete();
returnstrB.toString();
}
}
packagecom.ocr;
importjava.io.File;
publicclassTest {
/**
*@paramargs
*/
publicstaticvoidmain(String[] args) {
//TODOAuto-generated method stub
OCR ocr=newOCR();
try{
String maybe =newOCR().recognizeText(newFile("E://temp//222.jpg"),"jpg");
System.out.println(maybe);
}catch(Exception e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
由于可以第三方包加起来有点大,告诉大家一个网站去里面找你想要的包吧,需要相关包的留下邮件吧
java 目录结构如上图
效果图:
解析出来的效果