API说明:
http://www.apihome.cn/api/java/BufferedImage.html
package testimg;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestBufferImage {
private String originalImagePath;
private String newImagePath;
private BufferedImage originalImage;
@Before
public void init() throws Exception{
originalImagePath = "F:/temp/ocr/QQ.png";
newImagePath = "F:/temp/ocr/QQ_new.png";
originalImage = ImageIO.read(new FileInputStream(originalImagePath));
}
@After
public void after(){
}
@Test
public void zoomInImage() throws Exception{
Integer times = 5; //放大倍数
int width = originalImage.getWidth()*times;
int height = originalImage.getHeight()*times;
BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0,0,width,height,null);
g.dispose();
ImageIO.write(newImage, "png", new File(newImagePath));
}
@Test
public void binaryImage() throws IOException{
int width = originalImage.getWidth();
int height = originalImage.getHeight();
//重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
BufferedImage binaryImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = originalImage.getRGB(i, j);
binaryImage.setRGB(i, j, rgb);
}
}
ImageIO.write(binaryImage, "png", new File(newImagePath));
}
/**
* 可设置黑白二值的阈值
* 纯白色为 -1 纯黑色 -16777216 即灰度图后数值 越小越黑
* 设置一个 负数, rgb值小于等于这个的设置为 纯黑, 大于的设置纯白
* **/
@Test
public void binaryImageSelectable() throws Exception{
int setValue = -4802890; //设定的阈值
int WHITE = -1;
int BLACK = -16777216;
int width = originalImage.getWidth();
int height = originalImage.getHeight();
BufferedImage binaryImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = originalImage.getRGB(i, j);
if(rgb<=setValue)
binaryImage.setRGB(i, j, BLACK);
else
binaryImage.setRGB(i, j, WHITE);
}
}
File newImageFile = new File(newImagePath);
ImageIO.write(binaryImage, "png", newImageFile);
//识别文字
ITesseract instance = new Tesseract();
String result = instance.doOCR(newImageFile);
System.out.println(result);
}
/**
* 纯白色为 -1 纯黑色 -16777216
* **/
@Test
public void testGetRGB() throws Exception{
originalImage = ImageIO.read(new FileInputStream("F:/temp/ocr/QQ.png"));
int width = originalImage.getWidth();
int height = originalImage.getHeight();
System.out.println("width="+width+", height="+height);
int x = 237;
int y = 64;
int rgb = originalImage.getRGB(x, y);
System.out.println("rgb="+rgb);
}
@Test
public void testGray() throws Exception{
int width = originalImage.getWidth();
int height = originalImage.getHeight();
//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = originalImage.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
ImageIO.write(grayImage, "png", new File(newImagePath));
}
}