java图片缩放代码

2022-04-18 教育 67阅读
直接给你一个类,直接套用就好了

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;
}

}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com