方法1:
Image src = ToolKit.getToolKit().createImage("文件路径");
然
后先生成一个BufferedImage bi作为画布.
BufferedImage bi = new BufferedImage(目标宽, 目标高,BufferedImage.TYPE_INT_RGB);
得到它的Graphics对象:
Graphics g = bi.getGraphics();
然后往这个画而上画原图就行了:
g.grawImage(src,0,0,目标宽,目标高,null);
找一个编码类,如JPEGEncoder,GIFEncoder
把bi编码输出就行了.
方法2:
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class Test {
public static void main (String argv[]) {
try {
File fi = new File("C:/1.jpg"); //大图文件
File fo = new File("C:/2.jpg"); //将要转换出的小图文件
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nw = 120;
int nh = (nw * h) / w;
if(nh>120) {
nh = 120;
nw = (nh * w) / h;
}
double sx = (double)nw / w;
double sy = (double)nh / h;
transform.setToScale(sx,sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid, "jpeg", fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
* 照片缩放处理
*/
public class ScaleImageImpl {
/**
* 照片缩放处理
* @param imgWidth 照片的宽度
* @param imgHeight 照片的高度
* @param fromdir 照片的原始目录
* @param todir 处理后的照片存放目录
* @param imgfile 原始照片
* @param sysimgfile 处理后的照片文件名前缀
* @return
* @throws Exception
*/
public boolean createScaleImage(String imgWidth, String imgHeight,
String fromdir, String todir, String imgfile, String sysimgfile) throws Exception {
boolean isSuc = true;
int width = 0;
int height = 0;
if (imgWidth == null || "".equals(imgWidth)) {
width = 120;
} else {
width = Integer.parseInt(imgWidth);
}
if (imgHeight == null || "".equals(imgHeight)) {
height = 120;
} else {
height = Integer.parseInt(imgHeight);
}
File F = new File(fromdir, imgfile);
if (!F.isFile()) {
throw new Exception(F + " 不是照片文件!");
}
BufferedImage Bi = ImageIO.read(F);
double sx = width*1.0 / Bi.getWidth();
double sy = height*1.0 / Bi.getHeight();
String ext = "jpg";
File ThF = new File(todir, sysimgfile + "." + ext);
Image Itemp = null;
//Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, ext, ThF);
F.delete();
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage());
}
return isSuc;
}
在改写上面的方法后
/**
* 照片缩放处理
* @param imgWidth 照片的宽度
* @param imgHeight 照片的高度
* @param filename 修改图片文件
* @return
* @throws Exception
*/
public boolean createScaleImage2(String imgWidth, String imgHeight,
String filename) throws Exception {
boolean isSuc = true;
int width = 0;
int height = 0;
if (imgWidth == null || "".equals(imgWidth)) {
width = 120;
} else {
width = Integer.parseInt(imgWidth);
}
if (imgHeight == null || "".equals(imgHeight)) {
height = 120;
} else {
height = Integer.parseInt(imgHeight);
}
File F = new File(filename);
if (!F.isFile()) {
throw new Exception(F + " 不是照片文件!");
}
BufferedImage Bi = ImageIO.read(F);
double sx = width*1.0 / Bi.getWidth();
double sy = height*1.0 / Bi.getHeight();
String ext = "jpg";
Image Itemp = null;
//Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, ext, F);
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in createScaleImage.: " + ex.getMessage());
}
return isSuc;
}