找回密码
 入驻
搜索
查看: 259924|回复: 10

生成缩小jpg图片程序

[复制链接]
发表于 2007-5-27 15:51:19 | 显示全部楼层 |阅读模式
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
import java.awt.Image;

public class ZoomPicture{
public static void main(String arg[]){

String filePath = "g:/图片.jpg"; // 图片的位置

int height=50;
int width=150;
Icon icon = null;
try{
icon = getFixedBoundIcon(filePath,height,width);
}catch(Exception e){
System.out.println("exception : " + e);
}
System.out.println(" ### " + icon); //生成新图片的位置;
}

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();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0,lastLength);
String fileType = filePath.substring(lastLength);
File zoomFile = new File(subFilePath+"_"+height +"_"+width+fileType);
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", zoomFile);
ret = new ImageIcon(zoomFile.getPath());
}catch (Exception ex) {
System.out.println("######## here error : " + ex);
}
}
return ret;
}
}
发表于 2007-5-27 15:54:36 | 显示全部楼层
??看得出你很喜欢JAVA

[ 本帖最后由 众香之主 于 2007-5-27 21:21 编辑 ]
回复

使用道具 举报

发表于 2007-5-27 15:57:32 | 显示全部楼层
:titter
回复

使用道具 举报

发表于 2007-5-27 15:59:55 | 显示全部楼层
不喜欢JAVA局限性太大,不像C#是中层次高级语言的超级无敌霸王3000,C++是底层高级语言的超级无敌霸王3000
回复

使用道具 举报

发表于 2007-5-27 16:03:02 | 显示全部楼层
:handshake
回复

使用道具 举报

 楼主| 发表于 2007-5-27 21:38:48 | 显示全部楼层
java 是一种在任何操作系统都能做的事
C#就是行了。
回复

使用道具 举报

发表于 2007-5-27 21:49:11 | 显示全部楼层
原帖由 众香之主 于 2007-5-27 21:38 发表
java 是一种在任何操作系统都能做的事
C#就是行了。

是啊就得个跨平台的优势,不过在开发面上比较狭窄,JAVA一般就是做JSP有优势,还有一个手机小程序
回复

使用道具 举报

发表于 2007-5-27 21:51:06 | 显示全部楼层
:handshake :handshake
回复

使用道具 举报

发表于 2007-5-27 21:53:37 | 显示全部楼层
我刚学``不喜欢JAVA
觉得好麻烦
回复

使用道具 举报

发表于 2007-5-27 22:02:46 | 显示全部楼层
:smile :smile
回复

使用道具 举报

发表于 2007-5-27 22:07:55 | 显示全部楼层
原帖由 流动的希土 于 2007-5-27 21:53 发表
我刚学``不喜欢JAVA
觉得好麻烦

我也报了选修,不喜欢JAVA,是因为不麻烦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 入驻

本版积分规则

QQ|Archiver|手机版|小黑屋|思明论坛

GMT+8, 2024-9-28 15:32 , Processed in 0.013856 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表