• imagecombiner 方便的图片合成包


    image-combiner 国人开源的一个工具包还是很方便的,可以方便的搞一些图片以及文字处理

    参考使用

    • 代码
    package com.dalong;
     
    import com.freewayso.image.combiner.ImageCombiner;
    import com.freewayso.image.combiner.enums.OutputFormat;
     
    public class App {
        public static void main(String[] args) throws Exception {
            //合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准)
            ImageCombiner combiner = new ImageCombiner("http://127.0.0.1:8080/bkg.png", OutputFormat.JPG);
     
            //加图片元素
            combiner.addImageElement("http://127.0.0.1:8080/prod.png", 250, 300);
     
            //加文本元素
            combiner.addTextElement("周末大放送", 60, 250, 400);
     
            //执行图片合并
            combiner.combine();
     
            //可以获取流(并上传oss等)
           // InputStream is = combiner.getCombinedImageStream();
     
            //也可以保存到本地
            combiner.save("image.jpg");
        }
    }
    • 效果

    说明

    image-combiner 对于图片的路径使用了URL,所以如果是本地文件,可以使用files:/// 协议,或者通过File 类进行生成

           //合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准)
            String bkgURL = new File("src/main/resources/images/bkg.png").toURI().toURL().toString();
            String prodURL = new File("src/main/resources/images/prod.png").toURI().toURL().toString();
            ImageCombiner combiner = new ImageCombiner(bkgURL, OutputFormat.JPG);
            // file:////<path>/src/main/resources/images/prod.png
            //加图片元素
            combiner.addImageElement(prodURL, 250, 300);
     
            //加文本元素
            combiner.addTextElement("周末大放送", 60, 250, 400);
     
            //执行图片合并
            combiner.combine();
     
            //可以获取流(并上传oss等)
           // InputStream is = combiner.getCombinedImageStream();
     
            //也可以保存到本地
            combiner.save("image.jpg");

    对于项目打包情况的需要使用classloader 加载,参考

           //合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准)
            String bkgURL = App.class.getClassLoader().getResource("images/bkg.png").toString();
            String prodURL = App.class.getClassLoader().getResource("images/prod.png").toString();
            ImageCombiner combiner = new ImageCombiner(bkgURL, OutputFormat.JPG);
            // file:////<path>/src/main/resources/images/prod.png
            //加图片元素
            combiner.addImageElement(prodURL, 250, 300);
     
            //加文本元素
            combiner.addTextElement("周末大放送", 60, 250, 400);
     
            //执行图片合并
            combiner.combine();
     
            //可以获取流(并上传oss等)
           // InputStream is = combiner.getCombinedImageStream();
     
            //也可以保存到本地
            combiner.save("image.jpg");

    我已经写过简单的基于thumbnailator处理的,实际上如果需要复杂的处理image-combiner 是一个很不错的选择

    参考资料

    http://dromara.gitee.io/image-combiner/#/
    https://github.com/coobird/thumbnailator
    https://www.cnblogs.com/rongfengliang/p/13823824.html

  • 相关阅读:
    新浪微博爬虫项目
    time
    黑客增长
    python2 3 区别
    爬虫高性能相关
    登录_爬取并筛选拉钩网职位信息_自动提交简历
    破解极验验证码
    tesseract-ocr 传统验证码识别
    刻意练习
    计算学员的考试总成绩以及平均成绩
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15810985.html
Copyright © 2020-2023  润新知