日期:2014-05-20 浏览次数:20805 次
package ImageBinaryGray;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageDemo {
/***************************图像二值化*****************************/
public void binaryImage() throws IOException{
//File file = new File(System.getProperty("user.dir")+"/src.jpg");
File file = new File("D://ANDROID//javawork//imageprocesing//binaryzation//dest2.jpg");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
//File newFile = new File(System.getProperty("user.dir")+"/src.jpg");
File newFile = new File("D://ANDROID//javawork//imageprocesing//binaryzation//dest4.jpg");
ImageIO.write(grayImage, "jpg", newFile);
}
/***************************************************************/
/****************************图像灰度化处理*********************************/
public void grayImage() throws IOException{
//File file = new File(System.getProperty("user.dir")+"/src.jpg");
File file = new File("D://ANDROID//javawork//imageprocesing//binaryzation//src.jpg");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
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 = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
//File newFile = new File(System.getProperty("user.dir")+"/src.jpg");
File newFile = new File("D://ANDROID//javawork//imageprocesing//binaryzation//dest2.jpg");
ImageIO.write(grayImage, "jpg", newFile);
}
/*****************************************************************************/
}
</code>
Test.java
[code]
import ImageBinaryGray.*;
import java.io.IOException;
class Test{
public static void main(String[] args) throws IOException {
ImageDemo demo = new ImageDemo();
demo.grayImage();
demo.binaryImage();
}
}