处理图片缩放和旋转。

 

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

 

 

转载于:https://www.cnblogs.com/kong0it/archive/2013/04/12/3016770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值