面试题:代码性能问题,请高手指教。

阿_布 2010-05-11 07:17:34
一段给图片加水印的代码,指出其中的性能问题,小弟水平有限,看不出其中的问题,请高手赐教!!万分感谢!!!
代码如下:

import java.io.File;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.io.FileOutputStream;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import javax.imageio.ImageIO;

public class TestImage{

public final static void pressImage(String pressImg,String targetImg,int x,int y){
try{
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src,0,0,width,height,null);
//水印图片文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int height_biao = src_biao.getHeight(null);
int width_biao = src_biao.getWidth(null);
g.drawImage(src_biao,width-width_biao-x,height-height_biao-y,width_biao,height_biao,null);
g.dispose();
FileOutputStream out = new FileOutputStream("new"+targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}

}
...全文
173 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
laker_914 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 curacfyh 的回复:]

Java code

FileOutputStream out = new FileOutputStream("new"+targetImg);



改为

Java code

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("new" + targetImg));



加个缓……
[/Quote]

同意!!!
阿_布 2010-05-12
  • 打赏
  • 举报
回复
高手快来啊!
zfq642773391 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 curacfyh 的回复:]
Java code

FileOutputStream out = new FileOutputStream("new"+targetImg);



改为


Java code

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("new" + targetImg));

……
[/Quote]
加buffered理论上可能快些,但有时buffered的开销还不如不加
springbird 2010-05-11
  • 打赏
  • 举报
回复
学习,帮顶,等待高手解决
kaynezhang 2010-05-11
  • 打赏
  • 举报
回复
g.drawImage(src_biao,width-width_biao-x,height-height_biao-y,width_biao,height_biao,null);
g.dispose();
增加水印这里可以进行像素操作,难道是这个意思?


OopsJeff 2010-05-11
  • 打赏
  • 举报
回复
学习. 留位绑定
justlearn 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 curacfyh 的回复:]
Java code

FileOutputStream out = new FileOutputStream("new"+targetImg);



改为


Java code

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("new" + targetImg));

……
[/Quote]
确实,很可能是流的关系
ditouye 2010-05-11
  • 打赏
  • 举报
回复
帮顶……
24K純帥 2010-05-11
  • 打赏
  • 举报
回复
木有看出问题,学习。。
mingchaoyan 2010-05-11
  • 打赏
  • 举报
回复
帮顶~~~
amdgaming 2010-05-11
  • 打赏
  • 举报
回复
这个也 是 面试题?
justlearn 2010-05-11
  • 打赏
  • 举报
回复
我觉得就是这句吧
g.drawImage(src,0,0,width,height,null);
既然width和heigth用的就是src的宽和高,这里没必要取出来,再设置了
直接使用
g.drawImage(src,0,0,null);

个人意见,仅供参考
fengboxjtu556 2010-05-11
  • 打赏
  • 举报
回复
每一句加个输出时间,就知道性能出在哪里了
curacfyh 2010-05-11
  • 打赏
  • 举报
回复

FileOutputStream out = new FileOutputStream("new"+targetImg);


改为


BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("new" + targetImg));


加个缓存。
阿_布 2010-05-11
  • 打赏
  • 举报
回复
大家发表一下看法啊!别都这么低调啊!
justlearn 2010-05-11
  • 打赏
  • 举报
回复
留个记号
dellfish 2010-05-11
  • 打赏
  • 举报
回复
java的,不太懂。。。
cw550284 2010-05-11
  • 打赏
  • 举报
回复
顶起!!!!!!!!!!!!!!!
xiesisi3 2010-05-11
  • 打赏
  • 举报
回复
学习,帮顶。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧