package zhch.illq.picture;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageProcess {
/**
*
*
* @param srcImgFileName
* @throws IOException
*/
public void zoomImage(String srcImgFileName) throws IOException {
//
File _file = new File(srcImgFileName);
// Image
BufferedImage src = javax.imageio.ImageIO.read(_file);
int width = src.getWidth();
int height = src.getHeight();
//
BufferedImage tag = new BufferedImage(width / 2, height / 2, BufferedImage.TYPE_INT_RGB);
//
tag.getGraphics().drawImage(src, 0, 0, width / 2, height / 2, null);
FileOutputStream out = new FileOutputStream("d:\\temp\\targetIMG1-4.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
// 2
tag = new BufferedImage(width * 2, height * 2, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width * 2, height * 2, null);
out = new FileOutputStream("d:\\temp\\targetIMGx2.jpg");
encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
/**
*
*
* @param srcImageFile
* @throws IOException
*/
public void cut(String srcImageFile) throws IOException {
Image img;
ImageFilter cropFilter;
String dir = null;
//
BufferedImage src = ImageIO.read(new File(srcImageFile));
int destWidth = src.getWidth() / 3;
int destHeight = src.getHeight() / 3;
//
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//
cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); //
g.dispose();
//
dir = "d:\\temp\\cut_image_" + i + "_" + j + ".jpg";
File f = new File(dir);
ImageIO.write(tag, "JPEG", f);
}
}
}
public static void main(String[] args) throws IOException {
String imgFileName = "d:\\temp\\soe-258.jpg";
ImageProcess iZoom = new ImageProcess();
iZoom.zoomImage(imgFileName);
iZoom.cut(imgFileName);
}
}