日期:2014-05-20 浏览次数:20618 次
BufferedImage target = new BufferedImage(syswidth,sysheight,BufferedImage.TYPE_3BYTE_BGR); AffineTransform transform = new AffineTransform(); transform.setToScale(sx,sy); //操作 AffineTransformOp op = new AffineTransformOp(transform,null); op.filter(source,target);
------解决方案--------------------
public static Icon getFixedBoundIcon(String filePath,int height,int width) throws Exception
{
double ratio = 0.0;
File file = new File(filePath);
if(!file.isFile())
{
throw new Exception(file+" is not image file error in getFixedBoundIcon!");
}
Icon ret = new ImageIcon(filePath);
BufferedImage bi = ImageIO.read(file);
if(bi.getHeight()>height||bi.getWidth()>width)
{
if(bi.getHeight()>bi.getWidth())
{
ratio = (new Integer(height)).doubleValue()/bi.getHeight();
}
else
{
ratio = (new Integer(width)).doubleValue()/bi.getWidth();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0,lastLength);
String fileType = filePath.substring(lastLength);
File zoomFile = new File(subFilePath+fileType);
Image temp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),null);
temp = op.filter(bi, null);
ImageIO.write((BufferedImage)temp, "jpg", zoomFile);
ret = new ImageIcon(zoomFile.getPath());
}
return ret;
}
------解决方案--------------------
http://blog.csdn.net/fancyerII/archive/2010/08/25/5837991.aspx