package util;
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class TuPian{
public void jpgTset() throws Exception{
File _file = new File("d:ProjectTestpic11.jpg"); //讀入文件
Image src = javax.imageio.ImageIO.read(_file); //構造Image對象
int wideth=src.getWidth(null); //得到源圖寬
int height=src.getHeight(null); //得到源圖長
System.out.println("長度為"+String.valueOf(wideth));
System.out.println("寬度為"+String.valueOf(height));
BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null); //繪制縮小後的圖
// 輸出到文件流
FileOutputStream out=new FileOutputStream("d:ProjectTestpic2newfile.jpg");
// File file = new File("newFile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //JPEG編碼
out.close();
}
public static void main(String[] args){
try{
new TuPian().jpgTset();
}catch(Exception e){
e.printStackTrace();
}
}
}