java,图片压缩,略缩图

在网上找了两个图片的缩放类,在这里分享一下:

 1 package manager.util;
 2 import java.util.Calendar;
 3 import java.io.File;
 4 import java.awt.image.BufferedImage;
 5 import java.awt.Image;
 6 import java.awt.image.AffineTransformOp;
 7 import javax.imageio.ImageIO;
 8 import java.awt.geom.AffineTransform;
 9 public class UploadImg {
10     String fromFileStr;
11     String saveToFileStr;
12     String sysimgfile;
13     int width;
14     int height;
15     String suffix;
16     /**
17      * @param fromFileStr
18      *            原始图片完整路径
19      * @param saveToFileStr
20      *            缩略图片保存路径
21      * @param sysimgfilenNow
22      *            处理后的图片文件名前缀
23      * 
24      */
25     public UploadImg(String fromFileStr, String saveToFileStr, String sysimgfile,String suffix,int width,int height) {
26         this.fromFileStr = fromFileStr;
27         this.saveToFileStr = saveToFileStr;
28         this.sysimgfile = sysimgfile;
29         this.width=width;
30         this.height=height;
31         this.suffix=suffix;
32     }
33     public boolean createThumbnail() throws Exception {
34         // fileExtNmae是图片的格式 gif JPG 或png
35         // String fileExtNmae="";
36         double Ratio = 0.0;
37         File F = new File(fromFileStr);
38         if (!F.isFile())
39             throw new Exception(F
40                     + " is not image file error in CreateThumbnail!");
41         File ThF = new File(saveToFileStr, sysimgfile +"."+suffix);
42         BufferedImage Bi = ImageIO.read(F);
43         Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
44         if ((Bi.getHeight() > width) || (Bi.getWidth() > height)) {
45             if (Bi.getHeight() > Bi.getWidth())
46                 Ratio = (double)width / Bi.getHeight();
47             else
48                 Ratio = (double)height / Bi.getWidth();
49         }
50         AffineTransformOp op = new AffineTransformOp(AffineTransform
51                 .getScaleInstance(Ratio, Ratio), null);
52         Itemp = op.filter(Bi, null);
53         try {
54             ImageIO.write((BufferedImage) Itemp, suffix, ThF);
55         } catch (Exception ex) {
56             throw new Exception(" ImageIo.write error in CreatThum.: "
57                     + ex.getMessage());
58         }
59         return (true);
60     }
61     public static void main(String[] args) {
62         UploadImg UI;
63         boolean ss = false;
64         try {
65             UI = new UploadImg("d://FairyTaleVector040.png", "d://temp", "ps_low1","png",650,650);
66             ss = UI.createThumbnail();
67             if (ss) {
68                 System.out.println("Success");
69             } else {
70                 System.out.println("Error");
71             }
72         } catch (Exception e) {
73             System.out.print(e.toString());
74         }
75     }
76 }

 

这个类,可以对jpg、png、gif三种图片缩放,缩放按等比例缩放

 

 1 package manager.util;
 2 import java.awt.Graphics2D;
 3 import java.awt.RenderingHints;
 4 import java.awt.geom.AffineTransform;
 5 import java.awt.image.BufferedImage;
 6 import java.awt.image.ColorModel;
 7 import java.awt.image.WritableRaster;
 8 import java.io.File;
 9 import javax.imageio.ImageIO;
10 public class Thumbnail {
11     /**
12      * 生成缩略图 
13      * fromFileStr:原图片路径
14      *  saveToFileStr:缩略图路径 
15      *  width:缩略图的宽 
16      *  height:缩略图的高
17      */
18     public static void saveImageAsJpg(String fromFileStr, String saveToFileStr,
19             int width, int height,boolean equalProportion) throws Exception {
20         BufferedImage srcImage;
21         String imgType="JPEG";
22         if(fromFileStr.toLowerCase().endsWith(".png")){
23             imgType="PNG";
24         }
25         File fromFile=new File(fromFileStr);
26         File saveFile=new File(saveToFileStr);
27         srcImage=ImageIO.read(fromFile);
28         if(width>0||height>0){
29             srcImage=resize(srcImage,width,height,equalProportion);
30         }
31         ImageIO.write(srcImage,imgType,saveFile);
32     }
33     
34     /**
35      * 将原图片的BufferedImage对象生成缩略图
36      * source:原图片的BufferedImage对象
37      * targetW:缩略图的宽
38      * targetH:缩略图的高
39      */
40     public static BufferedImage resize(BufferedImage source,int targetW,int targetH,boolean equalProportion){
41         int type=source.getType();
42         BufferedImage target=null;
43         double sx=(double)targetW/source.getWidth();
44         double sy=(double)targetH/source.getHeight();
45         //这里想实现在targetW,targetH范围内实现等比例的缩放
46           //如果不需要等比例的缩放则下面的if else语句注释调即可
47         if(equalProportion){
48             if(sx>sy){
49                 sx=sy;
50                 targetW=(int)(sx*source.getWidth());
51             }else{
52                 sy=sx;
53                 targetH=(int)(sx*source.getHeight());
54             }
55         }
56         if(type==BufferedImage.TYPE_CUSTOM){
57             ColorModel cm=source.getColorModel();
58             WritableRaster raster=cm.createCompatibleWritableRaster(targetW,targetH);
59             boolean alphaPremultiplied=cm.isAlphaPremultiplied();
60             target=new BufferedImage(cm,raster,alphaPremultiplied,null);
61         }else{
62             target=new BufferedImage(targetW,targetH,type);
63             Graphics2D g=target.createGraphics();
64             g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
65             g.drawRenderedImage(source,AffineTransform.getScaleInstance(sx,sy));
66             g.dispose();
67         }
68         return target;
69     }
70     
71     public static void main(String[] args){
72         try{
73             Thumbnail.saveImageAsJpg("D://FairyTaleVector040.png", "D://temp//test.png", 480, 480,true);
74         }catch(Exception e){
75             e.printStackTrace();
76         }
77     }
78 }

这个类只能对jpg图片缩放,但是提供了两种缩放方式:等比例和不等比例

 以上内容来自: http://blog.csdn.net/tianhandigeng1498/article/details/6053835

你可能感兴趣的:(java,图片压缩,略缩图)