简易的Java缩略图生成程序

在dev2dev看到有人发帖子问,就随手写了一个,贴到这里来以后找起来方便。

java 代码
  1. /**  
  2.  
  3. * @param imageFilename 图片文件名  
  4. * @param width 要缩放的宽度  
  5. * @param height 要缩放的高度  
  6. * @throws IOException  
  7. */  
  8. private static void resize(String imageFilename, int width, int height) throws IOException {   
  9. File inputFile = new File(imageFilename);   
  10.     BufferedImage input = ImageIO.read(inputFile);   
  11.     BufferedImage output = new BufferedImage(width, height,   
  12.             BufferedImage.TYPE_INT_RGB);   
  13.     Graphics2D g2d = output.createGraphics();   
  14.     g2d.setBackground(Color.WHITE);   
  15.     g2d.fillRect(00, width, height);   
  16.     int oWidth = input.getWidth();   
  17.     int oHeight = input.getHeight();   
  18.     int dx = oWidth < width ? (width - oWidth) / 2 : 0;   
  19.     int dy = oHeight < height ? (height - oHeight) / 2 : 0;   
  20.     int dw = Math.min(oWidth, width);   
  21.     int dh = Math.min(oHeight, height);   
  22.     g2d.drawImage(input, dx, dy, dw, dh, null);   
  23.     File outputFile = new File(inputFile.getName().substring(0, inputFile.getName().lastIndexOf('.')) + "_thumbnail.jpg");   
  24.     ImageIO.write(output, "jpg", outputFile);   
  25. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值