java图片处理框架thumbnailator的简单使用

thumbnailator图片处理

最近在项目中遇到一个需要处理图片的缩放,旋转,剪切,添加水印,转换图片格式的需求。在网上找了一下资料发现一个很好用的开源框架thumbnailator,记录一下使用过程。

maven仓库地址: thumbnailator
maven坐标

        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.13</version>
        </dependency>
直接上代码
  1. 图片的缩放两种方式
 //原图像素 2668*2000
    String source = "E:/images/01.jpg";/*源图片路径*/
    String targetName = "缩放";/*操作名字*/
    String target = "E:/images/" + targetName + ".jpg";/*目标图片保存路径*/
    @Test
    public void t1() throws IOException {
        //缩放按照原图比例缩放 默认
        Thumbnails.of("src/main/resources/001.jpg")/*操作的原图片*/
                .size(100, 100)/*图片输出的长宽 此项不能缺失*/
                .keepAspectRatio(false)/*保存长宽比例*/
                .outputQuality(0.9)/*输出图片质量 0-1*/
                .useOriginalFormat()/*使用图片原格式输出*/
                .toFile(target);/*缩放后的目标图片*/
        //按照比例缩放到原图的0.1倍或者1.2倍
        Thumbnails.of(source).scale(0.1).toFile(target);
    }
  1. 图片的旋转

    @Test
    public void t2() throws IOException {
        //旋转顺时针正数 逆时针负数  保持长宽比例(默认true)可以不用添加此项  -90与270得到的效果一样
        Thumbnails.of(source).size(100, 100).keepAspectRatio(true).rotate(-90).toFile(target);
        Thumbnails.of(source).size(100, 100).keepAspectRatio(true).rotate(270).toFile(target);
    }
  1. 图片的剪切
@Test
    public void t3() throws IOException {
        //从中心点开始剪切一个800X200的图 并缩放到800X800输出 标准做法裁剪800X800输出也应该800X800
        Thumbnails.of(source).sourceRegion(Positions.CENTER, 800, 200).size(800, 800).keepAspectRatio(false).toFile(target);
        //从坐标远点(0,0)裁剪1300到2000并1300X2000输出
        Thumbnails.of(source).sourceRegion(0, 0, 1300, 2000).size(1300, 2000).keepAspectRatio(false).toFile(target);
    }
  1. 图片格式转换
 @Test
    public void t4() throws IOException {
        Thumbnails.of(source).size(200, 200).outputFormat("png").toFile("E:/images/jpg2png.png");
        //转换百度图片为png格式
        Thumbnails.of(this.getClass().getClassLoader().getResourceAsStream("baidu.jpeg")).size(200,200).toFile(new File("src/main/resources/cp/bd.png"));
        //转换图片为gif动图
        Thumbnails.of(source).size(200,200).outputFormat("gif").toFile("E:/GIF图.gif");
    }
  1. 添加图片水印
 public void t6() throws IOException {
        //将图片加载到内存中
        BufferedImage bi = ImageIO.read(new File("src/main/resources/fm.png"));
        Thumbnails.of(source).size(2668, 2000)
                .watermark(Positions.CENTER, bi, 0.9f)
                .watermark(Positions.TOP_LEFT, bi, 0.9f)
                .toFile(target);
    }
  1. 同时添加图片和文字水印
 //同时添加文字和图片水印  300x300 背景透明ARGB  RGB非透明
    @Test
    public void t7() throws IOException {
        //创建文字bi 背景透明ARGB  RGB背景非透明
        BufferedImage font = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
        //创建图片bi
        BufferedImage img = ImageIO.read(new File("src/main/resources/fm.png"));
        //创建文字绘制工具
        Graphics2D g = font.createGraphics();
//        g.drawImage(bi, 0, 0, null); 此处无需绘图在watermark()中会进行绘图
        //字体和字体颜色
        g.setFont(new Font("苹方", Font.PLAIN, 80));
        g.setColor(Color.RED);
        //绘制具体文字
        g.drawString("hello", 100, 100);
        g.drawString("world", 0, 200);
        //执行文字绘制
        g.dispose();
        //将文字和图片水印添加到指定源(source)图片中
        Thumbnails.of(source).size(2668, 2000)
                .watermark(Positions.CENTER, font, 0.8f)
                .watermark(Positions.BOTTOM_RIGHT, img, 0.8f)
                .toFile(target);
    }
  1. 图片旋转并原图输出(正常的操作方式)
 @Test
    public void t9() throws IOException {
    //加载图片到内存中
        BufferedImage bi = ImageIO.read(new File(source));
        Thumbnails.of(bi).size(bi.getWidth(), bi.getHeight()).rotate(45).outputQuality(0.9).toFile(target);
    }
  1. 项目中的使用方式(项目结构) 在这里插入图片描述
    9.个人学习整理资料托管到码云,后期同步更新。搬砖老王.
问题待解决
a)图片旋转后底色为黑色,正常的需要底层为白色暂时没有解决
b)如果需要做更深入的图片处理,需要了解以下java类如:
Image,BufferedImage,Graphics2D,Graphics,ImageIO等
c)希望大家给出更多的图片操作技巧,欢迎点评。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Pollexor 是 Thumbor 图片服务的 JAVA 客户端,兼容 Android 平台。Maven:<dependency>   <groupId>com.squareup</groupId>   <artifactId>pollexor<artifactId>   <version>2.0.2</version> </dependency>Gradle:compile 'com.squareup:pollexor:2.0.2'示例// Without encryption:Thumbor thumbor = Thumbor.create("http://example.com/"); // With encryption:Thumbor thumbor = Thumbor.create("http://example.com/", "key");thumbor.buildImage("http://example.com/image.png")     .resize(48, 48)     .toUrl()// Produces: /unsafe/48x48/example.com/image.pngthumbor.buildImage("http://example.com/image.png")     .crop(10, 10, 90, 90)     .resize(40, 40)     .smart()     .toUrl()// Produces: /unsafe/10x10:90x90/smart/40x40/example.com/image.pngthumbor.buildImage("http://example.com/image.png")     .crop(5, 5, 195, 195)     .resize(95, 95)     .align(BOTTOM, RIGHT)     .toUrl()// Produces: /unsafe/5x5:195x195/right/bottom/95x95/example.com/image.pngthumbor.buildImage("http://example.com/background.png")     .resize(200, 100)     .filter(         roundCorner(10),         watermark(thumbor.buildImage("http://example.com/overlay1.png").resize(200, 100)),         watermark(thumbor.buildImage("http://example.com/overlay2.png").resize(50, 50), 75, 25),         quality(85)     )     .toUrl()// Produces: /unsafe/200x100/filters:round_corner(10,255,255,255):watermark(/unsafe/200x100/example.c 标签:Pollexor

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT暖男成长记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值