请问CSDN上传图片时的图片裁剪怎么实现的。想学习学习?
解决方案 »
- 关于微博分享的问题
- 这个jsp哪地方错了?请高手给写下注释,谢谢,新手
- SQL语句问题
- 修练 JAVA
- 从数据库中取出来的分类问题!!!
- JSP中文 问题
- lookup时遇到一个问题:NoInitialContextException: Need to specify class name in environment or system property怎么办?
- 100分相送!!!!毕业5个月了,工作半年,熟练使用VB,VC,JAVA编程语言。想在武汉找工作,
- 弄了个论坛,欢迎大家光临!:)up有分!
- 求助:连接数达到150后服务器变得很慢怎么办?
- 数据源配置问题
- AJAX +jsp 实现浮动层 (jsp传给js参数)! 高分 !!
楼主【nhy520】截止到2008-07-13 10:09:37的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:0 每贴平均分数:0
回帖的总数量:30 得分贴总数量:4 回帖的得分率:13%
结贴的总数量:1 结贴的总分数:0
无满意结贴数:1 无满意结贴分:0
未结的帖子数:1 未结的总分数:0
结贴的百分比:50.00 % 结分的百分比:---------------------
无满意结贴率:100.00% 无满意结分率:---------------------
楼主加油
public static void shrinkImage(int w, int h, String oldFile, String newFile,String path) throws Exception {
// 参数w表示缩小后的宽度 h缩小后的高度,任何一个为0 则按另一方比例缩小。
String filePath =path;
Arith arith = new Arith();
filePath = filePath.replace("\\","\\\\");
filePath += "\\\\";
double wd = 0;
double hd = 0;
String ws = "", hs = "";
try{
File myFile = new File(filePath + oldFile); //指定文件名含路径
Image src = javax.imageio.ImageIO.read(myFile); //构造Image对象
int wideth=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
if((w>0 && h==0) && w < wideth){
// 计算出宽度的缩放比例。
wd = arith.div(Double.parseDouble(String.valueOf(w)),Double.parseDouble(String.valueOf(wideth)),10);
hd = arith.mul(wd,Double.parseDouble(String.valueOf(height)));
hd = arith.round(hd,0); //四舍五入保留小数点0位
hs = String.valueOf(hd);
hs = hs.substring(0, hs.indexOf("."));
h = Integer.parseInt(hs) ; //按比例缩小高度
}else if((h > 0 && w == 0) && h < height){
// 计算出高度的缩放比例。
hd = arith.div(Double.parseDouble(String.valueOf(h)),Double.parseDouble(String.valueOf(height)),10);
wd = arith.mul(hd,Double.parseDouble(String.valueOf(wideth)));
wd = arith.round(wd,0); //四舍五入保留小数点0位
ws = String.valueOf(wd);
ws = ws.substring(0, ws.indexOf("."));
w = Integer.parseInt(ws) ; //按比例缩小高度
}else if(w >= wideth || h >= height){
w = wideth;
h = height;
}
java.awt.image.BufferedImage tag = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,w,h,null); //绘制缩小后的图
FileOutputStream out=new FileOutputStream(filePath + newFile); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //JPEG编码
out.close();
}catch(Exception e){
System.out.println("===errorMessage====" + e.toString());
}
}