在网上找了两个图片的缩放类,在这里分享一下:
package manager.util;
import java.util.Calendar;
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.image.AffineTransformOp;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
public class UploadImg {
String fromFileStr;
String saveToFileStr;
String sysimgfile;
int width;
int height;
String suffix;
/**
* @param fromFileStr
* 原始图片完整路径
* @param saveToFileStr
* 缩略图片保存路径
* @param sysimgfilenNow
* 处理后的图片文件名前缀
*
*/
public UploadImg(String fromFileStr, String saveToFileStr, String sysimgfile,String suffix,int width,int height) {
this.fromFileStr = fromFileStr;
this.saveToFileStr = saveToFileStr;
this.sysimgfile = sysimgfile;
this.width=width;
this.height=height;
this.suffix=suffix;
}
public boolean createThumbnail() throws Exception {
// fileExtNmae是图片的格式 gif JPG 或png
// String fileExtNmae="";
double Ratio = 0.0;
File F = new File(fromFileStr);
if (!F.isFile())
throw new Exception(F
+ " is not image file error in CreateThumbnail!");
File ThF = new File(saveToFileStr, sysimgfile +"."+suffix);
BufferedImage Bi = ImageIO.read(F);
Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
if ((Bi.getHeight() > width) || (Bi.getWidth() > height)) {
if (Bi.getHeight() > Bi.getWidth())
Ratio = (double)width / Bi.getHeight();
else
Ratio = (double)height / Bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, suffix, ThF);
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: "
+ ex.getMessage());
}
return (true);
}
public static void main(String[] args) {
UploadImg UI;
boolean ss = false;
try {
UI = new UploadImg("d://FairyTaleVector040.png", "d://temp", "ps_low1","png",650,650);
ss = UI.createThumbnail();
if (ss) {
System.out.println("Success");
} else {
System.out.println("Error");
}
} catch (Exception e) {
System.out.print(e.toString());
}
}
}
这个类,可以对jpg、png、gif三种图片缩放,缩放按等比例缩放
package manager.util;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Thumbnail {
/**
* 生成缩略图
* fromFileStr:原图片路径
* saveToFileStr:缩略图路径
* width:缩略图的宽
* height:缩略图的高
*/
public static void saveImageAsJpg(String fromFileStr, String saveToFileStr,
int width, int height,boolean equalProportion) throws Exception {
BufferedImage srcImage;
String imgType="JPEG";
if(fromFileStr.toLowerCase().endsWith(".png")){
imgType="PNG";
}
File fromFile=new File(fromFileStr);
File saveFile=new File(saveToFileStr);
srcImage=ImageIO.read(fromFile);
if(width>0||height>0){
srcImage=resize(srcImage,width,height,equalProportion);
}
ImageIO.write(srcImage,imgType,saveFile);
} /**
* 将原图片的BufferedImage对象生成缩略图
* source:原图片的BufferedImage对象
* targetW:缩略图的宽
* targetH:缩略图的高
*/
public static BufferedImage resize(BufferedImage source,int targetW,int targetH,boolean equalProportion){
int type=source.getType();
BufferedImage target=null;
double sx=(double)targetW/source.getWidth();
double sy=(double)targetH/source.getHeight();
//这里想实现在targetW,targetH范围内实现等比例的缩放
//如果不需要等比例的缩放则下面的if else语句注释调即可
if(equalProportion){
if(sx>sy){
sx=sy;
targetW=(int)(sx*source.getWidth());
}else{
sy=sx;
targetH=(int)(sx*source.getHeight());
}
}
if(type==BufferedImage.TYPE_CUSTOM){
ColorModel cm=source.getColorModel();
WritableRaster raster=cm.createCompatibleWritableRaster(targetW,targetH);
boolean alphaPremultiplied=cm.isAlphaPremultiplied();
target=new BufferedImage(cm,raster,alphaPremultiplied,null);
}else{
target=new BufferedImage(targetW,targetH,type);
Graphics2D g=target.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source,AffineTransform.getScaleInstance(sx,sy));
g.dispose();
}
return target;
} public static void main(String[] args){
try{
Thumbnail.saveImageAsJpg("D://FairyTaleVector040.png", "D://temp//test.png", 480, 480,true);
}catch(Exception e){
e.printStackTrace();
}
}
}
这个类只能对jpg图片缩放,但是提供了两种缩放方式:等比例和不等比例
以上内容来自: http://blog.csdn.net/tianhandigeng1498/article/details/6053835