博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java,图片压缩,略缩图
阅读量:6473 次
发布时间:2019-06-23

本文共 5790 字,大约阅读时间需要 19 分钟。

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

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 fromFileStr18      *            原始图片完整路径19      * @param saveToFileStr20      *            缩略图片保存路径21      * @param sysimgfilenNow22      *            处理后的图片文件名前缀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 或png35         // String fileExtNmae="";36         double Ratio = 0.0;37         File F = new File(fromFileStr);38         if (!F.isFile())39             throw new Exception(F40                     + " 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             else48                 Ratio = (double)height / Bi.getWidth();49         }50         AffineTransformOp op = new AffineTransformOp(AffineTransform51                 .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
你可能感兴趣的文章
kickstart文件详解
查看>>
lua——string之string.gsub
查看>>
JS中的跨域问题
查看>>
MySQL 8 新特性之持久化全局变量的修改
查看>>
Docker Tag
查看>>
ZOJ 2459 Pyramids
查看>>
activemq自己配置安装过程
查看>>
Java Practices -> Home
查看>>
(收藏)Andriod中文翻译组
查看>>
列出Database所有Key列或者获取表主键名称
查看>>
类依赖项的不透明性和透明性
查看>>
更改 input type 的值
查看>>
双重OAuth 2.0架构
查看>>
Codeforces 452A Eevee
查看>>
推荐的优秀博客--多关注,多看看
查看>>
深入理解盒模型
查看>>
java中已经排序的列表中插入新值
查看>>
整理一些开源项目
查看>>
hibernate -- HQL语句总结
查看>>
Android深入浅出之 AudioTrack分析
查看>>