处理图片缩放和旋转。

 

    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");  
    } 

 

 

posted @ 2013-04-12 15:34  k2013  阅读(179)  评论(0编辑  收藏  举报