java中对图片按比例缩放
2009-02-17 17:23
375 查看
需要对图片进行处理,使之能够适当的显示在画面中。找到一些资料后,处理也比较简单代码:
public static Icon getFixedBoundIcon(String filePath, int height, int width) throws Exception{
double Ratio=0.0; //缩放比例
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F+" is not image file error in getFixedBoundIcon!");
Icon ret = new ImageIcon(filePath);
BufferedImage Bi = ImageIO.read(F);
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();
} File ThF = new File(filePath+"_"+height +"_"+width);
Image Itemp = Bi.getScaledInstance (width,height,Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage)Itemp, "jpg", ThF);
ret = new ImageIcon(ThF.getPath());
}catch (Exception ex) {
}
}
return ret;
}
public static Icon getFixedBoundIcon(String filePath, int height, int width) throws Exception{
double Ratio=0.0; //缩放比例
File F = new File(filePath);
if (!F.isFile())
throw new Exception(F+" is not image file error in getFixedBoundIcon!");
Icon ret = new ImageIcon(filePath);
BufferedImage Bi = ImageIO.read(F);
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();
} File ThF = new File(filePath+"_"+height +"_"+width);
Image Itemp = Bi.getScaledInstance (width,height,Bi.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage)Itemp, "jpg", ThF);
ret = new ImageIcon(ThF.getPath());
}catch (Exception ex) {
}
}
return ret;
}
相关文章推荐
- Java 缩放图片工具类,创建缩略图、伸缩图片比例
- java swing的背景图片按比例缩放
- java读取jpg图片旋转按比例缩放
- java图片处理,裁剪,合图,等比例缩放
- JAVA 使用springMVC 上传多张图片或文件,并对图片进行按比例缩放处理
- Java 缩放图片工具类,创建缩略图、伸缩图片比例
- java处理图片按比例缩放功能
- java图片处理,裁剪,合图,等比例缩放
- Java 缩放图片工具类,创建缩略图、伸缩图片比例
- java swing的背景图片按比例缩放
- java swing的背景图片按比例缩放
- Java实现图片等比例缩略图
- Android ImageView图片代码实现按屏幕宽度等比例缩放
- jQuery实现等比例缩放大图片
- Android imageView图片按比例缩放
- 图片等比例缩放
- 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
- java按比例压缩图片的源代码,用java如何把图片处理到指定大小
- 如何实现php图片等比例缩放
- 图片按比例缩放