zoukankan      html  css  js  c++  java
  • 处理图片缩放和旋转。

        Thumbnailator是一个用来生成图像缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。

        有了这玩意,就不用在费心思使用Image I/O API,Java 2D API等等来生成缩略图了。

        Thumbnailator的下载地址:

        http://code.google.com/p/thumbnailator/downloads/list

    好了,直接上代码:

    /**  
         *   
         * @throws IOException   
         * @brief 生成缩略图简单实例   
         *  
         */ 
        public static void simple() throws IOException{  
                //需要转换的文件为桌面上的1.png  
                Thumbnails.of("C:/Documents and Settings/Administrator/桌面/1.png")  
                /*  
                 * forceSize,size和scale必须且只能调用一个  
                 */ 
    //          .forceSize(400, 400)  //生成的图片一定为400*400  
                /*  
                 * 若图片横比200小,高比300小,不变  
                 * 若图片横比200小,高比300大,高缩小到300,图片比例不变  
                 * 若图片横比200大,高比300小,横缩小到200,图片比例不变  
                 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300  
                 */ 
                .size(200, 300)     
                .outputFormat("png") //生成图片的格式为png  
                .outputQuality(0.8f) //生成质量为80%  
    //          .scale(0.5f)  //缩小50%  
                //输出到桌面5文件  
                .toFile("C:/Documents and Settings/Administrator/桌面/2");  
        }  
          
        /**  
         *   
         * @throws IOException   
         * @brief 生成旋转的缩略图  
         *  
         */ 
        public static void rotate() throws IOException{  
            Thumbnails.of("C:/Documents and Settings/Administrator/桌面/1.png")  
            //顺时针旋转90度  
            .rotate(90)  
            .scale(0.8f)  
            .toFile("C:/Documents and Settings/Administrator/桌面/3");  
        }  
          
        /**  
         *   
         * @brief 生成带水印的图片  
         *  
         * @throws IOException  
         */ 
        public static void watermark() throws IOException {  
            Thumbnails.of("C:/Documents and Settings/Administrator/桌面/1.png")  
            //水印在右下角,50%透明度,水印图片为桌面上的logo.gif  
            .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File("C:/Documents and Settings/Administrator/桌面/logo.gif")),0.5f)  
            .scale(0.8f)  
            .toFile("C:/Documents and Settings/Administrator/桌面/4");  
        } 
  • 相关阅读:
    MySQL权限整理及授权命令
    netstat
    5.7.20 多实例——MGR部署实战
    ELK(V7)部署与架构分析
    服务器安全之iptables
    记一次MySQL中Waiting for table metadata lock的解决方法
    MySQL在线DDL gh-ost 使用说明
    如何只查看配置文件中未被注释的有效配置行
    nginx的http负载均衡
    nginx实现正向代理和反向代理
  • 原文地址:https://www.cnblogs.com/kong0it/p/3016770.html
Copyright © 2011-2022 走看看