直接给你一个类,直接套用就好了
importjava.awt.Graphics2D;
importjava.awt.RenderingHints;
importjava.awt.geom.AffineTransform;
importjava.awt.image.BufferedImage;
importjava.awt.image.ColorModel;
importjava.awt.image.WritableRaster;
importjava.io.File;
importjavax.imageio.ImageIO;
publicclassResize{
BufferedImagebufImage;
intwidth;
intheight;
publicResize(){
//TODOAuto-generatedconstructorstub
}
publicResize(StringsrcPath,intwidth,intheight){
this.width=width;
this.height=height;
try{
this.bufImage=ImageIO.read(newFile(srcPath));
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticBufferedImagerize(BufferedImagesrcBufImage,intwidth,intheight){
BufferedImagebufTarget=null;
doublesx=(double)width/srcBufImage.getWidth();
doublesy=(double)height/srcBufImage.getHeight();
inttype=srcBufImage.getType();
if(type==BufferedImage.TYPE_CUSTOM){
ColorModelcm=srcBufImage.getColorModel();
WritableRasterraster=cm.createCompatibleWritableRaster(width,
height);
booleanalphaPremultiplied=cm.isAlphaPremultiplied();
bufTarget=newBufferedImage(cm,raster,alphaPremultiplied,null);
}else
bufTarget=newBufferedImage(width,height,type);
Graphics2Dg=bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage,AffineTransform.getScaleInstance(sx,sy));
g.dispose();
returnbufTarget;
}
}