java bufferdimage 图片处理


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));
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值