楼上正解........
我自己也写了一个,
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ReplicateScaleFilter;
import
import
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.swing.*;
public class ScaleImage{
String name;
public ScaleImage(String fullPathName)
{
this.name = fullPathName;
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage(this.name);
try{
ReplicateScaleFilter rsf = new ReplicateScaleFilter(100,100);
FilteredImageSource fis = new FilteredImageSource(image.getSource(),rsf);
MediaTracker mt = new MediaTracker(new JFrame());
final Image tempImage = tk.createImage(fis);
mt.addImage(tempImage, 0);
mt.waitForID(0);
int width = tempImage.getWidth(null);
int height = tempImage.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(tempImage, 0, 0, width,height,null);
FileOutputStream fos = new FileOutputStream(new File(this.name));
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(bi);
fos.close();
}catch(Exception e)
{
System.out.println("error");
}
}
public static void main(String args[])
{
new ScaleImage("D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\Online\\uploadFiles\\2.jpg");
//就是图片的路径,处理完后会覆盖原来的图片
}
}