缩放图片后,图片不完整问题,图片右边和下边一部分不显示.

xwjrun 2008-07-05 10:20:07
缩放图片后,缩略图不完整,右边和下边有一部分没有显示.
不清楚是哪的问题.大家帮忙看看.
		 public static void scale(String srcImageFile, String result, int reswidth, int resheight) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
int scaleNum = 0; //缩放比例
int scaledWidth = 0; //缩放后的宽
int scaledHeight = 0; //缩放后的高
int x = 0;
int y = 0;

if(reswidth>0&&resheight>0&&(width>reswidth||height>resheight)){
if(height>=width){
scaleNum = height / resheight;
}else if(width>height){
scaleNum = width / reswidth;
}
scaledWidth = width / scaleNum;
scaledHeight = height / scaleNum;

}else{
scaledWidth = width;
scaledHeight = height;
}
x = (reswidth-scaledWidth)/2;
y = (resheight-scaledHeight)/2;
System.out.println("width :"+width+" height :"+height);
System.out.println("scaledWidth :"+scaledWidth+" scaledHeight :"+scaledHeight);
System.out.println("reswidth :"+reswidth+" resheight :"+resheight);

Image image = src.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(reswidth, resheight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = tag.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0,0,reswidth, resheight);

g.drawImage(image, x<0?0:x, y<0?0:y, null); // 绘制缩小后的图
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
...全文
390 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwjrun 2008-07-06
  • 打赏
  • 举报
回复
怎么没人帮帮忙。
xwjrun 2008-07-05
  • 打赏
  • 举报
回复
哪位兄弟帮忙看看.

81,094

社区成员

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

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