`

Java实现按照要求的尺寸对图进行的等比缩放

    博客分类:
  • Java
阅读更多
         这个是在参考网上一些代码基础上进行编写的,主要是首先实现图片的等比缩放,然后在进行截取,比如一张1024*768的图要截成263*150的,首先是等比缩放后变成263*197的,然后在对这个图片的高度进行截取,变成163*150的
java 代码
  1. import java.awt.Color;   
  2. import java.awt.Graphics;   
  3. import java.awt.Graphics2D;   
  4. import java.awt.Rectangle;   
  5. import java.awt.RenderingHints;   
  6. import java.awt.geom.AffineTransform;   
  7. import java.awt.image.BufferedImage;   
  8. import java.awt.image.ColorModel;   
  9. import java.awt.image.WritableRaster;   
  10. import java.io.File;   
  11. import java.io.FileInputStream;   
  12. import java.io.FileOutputStream;   
  13. import java.io.IOException;   
  14. import java.io.InputStream;   
  15. import javax.imageio.ImageIO;   
  16. import com.sun.image.codec.jpeg.JPEGCodec;   
  17. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  18.   
  19. /**  
  20.  * 图片工具类,完成图片的截取  
  21.  *   
  22.  * @author inc062977  
  23.  *   
  24.  */  
  25. public class ImageHepler {   
  26.     /**  
  27.      * 实现图像的等比缩放  
  28.      * @param source  
  29.      * @param targetW  
  30.      * @param targetH  
  31.      * @return  
  32.      */  
  33.     private static BufferedImage resize(BufferedImage source, int targetW,   
  34.             int targetH) {   
  35.         // targetW,targetH分别表示目标长和宽   
  36.         int type = source.getType();   
  37.         BufferedImage target = null;   
  38.         double sx = (double) targetW / source.getWidth();   
  39.         double sy = (double) targetH / source.getHeight();   
  40.         // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放   
  41.         // 则将下面的if else语句注释即可   
  42.         if (sx < sy) {   
  43.             sx = sy;   
  44.             targetW = (int) (sx * source.getWidth());   
  45.         } else {   
  46.             sy = sx;   
  47.             targetH = (int) (sy * source.getHeight());   
  48.         }   
  49.         if (type == BufferedImage.TYPE_CUSTOM) { // handmade   
  50.             ColorModel cm = source.getColorModel();   
  51.             WritableRaster raster = cm.createCompatibleWritableRaster(targetW,   
  52.                     targetH);   
  53.             boolean alphaPremultiplied = cm.isAlphaPremultiplied();   
  54.             target = new BufferedImage(cm, raster, alphaPremultiplied, null);   
  55.         } else  
  56.             target = new BufferedImage(targetW, targetH, type);   
  57.         Graphics2D g = target.createGraphics();   
  58.         // smoother than exlax:   
  59.         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,   
  60.                 RenderingHints.VALUE_INTERPOLATION_BICUBIC);   
  61.         g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));   
  62.         g.dispose();   
  63.         return target;   
  64.     }   
  65.   
  66.     /**  
  67.      * 实现图像的等比缩放和缩放后的截取  
  68.      * @param inFilePath 要截取文件的路径  
  69.      * @param outFilePath 截取后输出的路径  
  70.      * @param width 要截取宽度  
  71.      * @param hight 要截取的高度  
  72.      * @param proportion  
  73.      * @throws Exception  
  74.      */  
  75.        
  76.     public static void saveImageAsJpg(String inFilePath, String outFilePath,   
  77.             int width, int hight, boolean proportion)throws Exception {   
  78.          File file = new File(inFilePath);   
  79.          InputStream in = new FileInputStream(file);   
  80.          File saveFile = new File(outFilePath);   
  81.   
  82.         BufferedImage srcImage = ImageIO.read(in);   
  83.         if (width > 0 || hight > 0) {   
  84.             // 原图的大小   
  85.             int sw = srcImage.getWidth();   
  86.             int sh = srcImage.getHeight();   
  87.             // 如果原图像的大小小于要缩放的图像大小,直接将要缩放的图像复制过去   
  88.             if (sw > width && sh > hight) {   
  89.                 srcImage = resize(srcImage, width, hight);   
  90.             } else {   
  91.                 String fileName = saveFile.getName();   
  92.                 String formatName = fileName.substring(fileName   
  93.                         .lastIndexOf('.') + 1);   
  94.                 ImageIO.write(srcImage, formatName, saveFile);   
  95.                 return;   
  96.             }   
  97.         }   
  98.         // 缩放后的图像的宽和高   
  99.         int w = srcImage.getWidth();   
  100.         int h = srcImage.getHeight();   
  101.         // 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取   
  102.         if (w == width) {   
  103.             // 计算X轴坐标   
  104.             int x = 0;   
  105.             int y = h / 2 - hight / 2;   
  106.             saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);   
  107.         }   
  108.         // 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取   
  109.         else if (h == hight) {   
  110.             // 计算X轴坐标   
  111.             int x = w / 2 - width / 2;   
  112.             int y = 0;   
  113.             saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);   
  114.         }   
  115.         in.close();   
  116.     }   
  117.     /**  
  118.      * 实现缩放后的截图  
  119.      * @param image 缩放后的图像  
  120.      * @param subImageBounds 要截取的子图的范围  
  121.      * @param subImageFile 要保存的文件  
  122.      * @throws IOException  
  123.      */  
  124.     private static void saveSubImage(BufferedImage image,   
  125.             Rectangle subImageBounds, File subImageFile) throws IOException {   
  126.         if (subImageBounds.x < 0 || subImageBounds.y < 0  
  127.                 || subImageBounds.width - subImageBounds.x > image.getWidth()   
  128.                 || subImageBounds.height - subImageBounds.y > image.getHeight()) {   
  129.             System.out.println("Bad   subimage   bounds");   
  130.             return;   
  131.         }   
  132.         BufferedImage subImage = image.getSubimage(subImageBounds.x,subImageBounds.y, subImageBounds.width, subImageBounds.height);   
  133.         String fileName = subImageFile.getName();   
  134.         String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);   
  135.         ImageIO.write(subImage, formatName, subImageFile);   
  136.     }   
  137.   
  138.   
  139. }   
分享到:
评论
9 楼 xyheritrix 2012-09-19  
算法明显有问题
8 楼 likehibernate 2008-07-23  
只能缩小不能放大啊!
7 楼 norwolfli 2008-06-28  
考走了,正好想用这个功能,真是太及时了。谢谢!
6 楼 form_rr 2008-06-28  
透明的png图片缩放后的图片就不透明了!
怎么解决啊?
5 楼 yangli 2007-10-24  
用的着,顶了
4 楼 badpeas 2007-10-24  
顶下,有时间来看看!
3 楼 pengpai 2007-10-20  
恩,我看看
2 楼 wangzhongjie 2007-10-19  
恩,刚好现在在做一个网站,里面又房屋发布模块,需要这样得东西,拿来看看!^_^
1 楼 loky 2007-10-14  
图片上传和等比缩放的技术我刚刚在一个项目中用过,
和你用的差不多,不过很是费了一番周折。希望能对以后的朋友有些帮助。

相关推荐

Global site tag (gtag.js) - Google Analytics