packageutil;
importjavaioFile;
importjavaioFileOutputStream;
importjavaawtGraphics;
importjavaawtImage;
importjavaawtimageBufferedImage;
importcomsunimagecodecjpegJPEGCodec;
importcomsunimagecodecjpegJPEGImageEncoder;
publicclassTuPian{
publicvoidjpgTset()throwsException{
File_file=newFile("d:ProjectTestpicjpg");//读入文件
Imagesrc=javaximageioImageIOread(_file);//构造Image对象
intwideth=srcgetWidth(null);//得到源图宽
intheight=srcgetHeight(null);//得到源图长
Systemoutprintln("长度为"+StringvalueOf(wideth));
Systemoutprintln("宽度为"+StringvalueOf(height));
BufferedImagetag=newBufferedImage(wideth/height/BufferedImageTYPE_INT_RGB);
taggetGraphics()drawImage(srcwideth/height/null);//绘制缩小后的图
//输出到文件流
FileOutputStreamout=newFileOutputStream("d:ProjectTestpicnewfilejpg");
//Filefile=newFile("newFilejpg");
JPEGImageEncoderencoder=JPEGCodeccreateJPEGEncoder(out);
encoderencode(tag);//JPEG编码
outclose();
}
publicstaticvoidmain(String[]args){
try{
newTuPian()jpgTset();
}catch(Exceptione){
eprintStackTrace();
}
}
}