ImageCombiner能够做什么?
ImageCombiner是一个专门用于Java服务端图片合成的工具,没有很复杂的功能,简单实用,从实际业务场景出发,提供简单的接口,几行代码即可实现图片拼合(当然用于合成水印也可以),素材上支持图片、文本、矩形三种,支持定位、缩放、旋转、圆角、透明度、颜色、字体、字号、删除线、居中绘制、文本自动换行等特性,足够覆盖图片合成的日常需求。
UML
ImageCombiner怎么使用
ImageCombiner使用起来相当简单,主要的类只用一个,new一个ImageCombiner对象,指定背景图片和输出格式,然后加入各种素材元素,设置元素的位置、大小和效果(如圆角、颜色、透明度等),调用combine()方法即可。combine()方法直接返回BufferedImage对象,也可以调用getCombinedImageStream()获得流,方便上传oss等后续操作,或者调用save()方法保存到本地,调试的时候比较方便。
版本要求
项目不依赖任何框架,完全基于JDK本身编写,没有各种花里胡哨的东西,性能还是相当不错的。
使用
只需要在项目内容引入包
<dependency>
<groupId>com.freewayso</groupId>
<artifactId>image-combiner</artifactId>
<version>2.3.2</version>
</dependency>
几个例子
package com.example.study.image.combiner;
import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.enums.OutputFormat;
import java.io.InputStream;
/**
* <h3>study</h3>
* <p>最简单的例子</p>
*
* @author : ZhangYuJie
* @date : 2022-02-20 19:51
**/
public class SimpleDemo {
public static void main(String[] args) throws Exception {
//合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准)
ImageCombiner combiner = new ImageCombiner("https://sjb-oss.oss-cn-hangzhou.aliyuncs.com/atd/material/7/image/7f0afcc14e60695f0f28f2bb9801fcf5", OutputFormat.JPG);
//加图片元素
combiner.addImageElement("https://sjb-oss.oss-cn-hangzhou.aliyuncs.com/atd/material/7/image/7f0afcc14e60695f0f28f2bb9801fcf5", 0, 300);
//加文本元素
combiner.addTextElement("周末大放送", 60, 100, 960);
//执行图片合并
combiner.combine();
//可以获取流(并上传oss等)
InputStream is = combiner.getCombinedImageStream();
//也可以保存到本地
combiner.save("d://image.jpg");
}
}
package com.example.study.image.combiner;
import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.element.TextElement;
import com.freewayso.image.combiner.enums.OutputFormat;
import java.awt.*;
/**
* <h3>study</h3>
* <p></p>
*
* @author : ZhangYuJie
* @date : 2022-02-20 19:54
**/
public class DynamicWidthDemoTest {
public static void main(String[] args) throws Exception {
String bg = "https://sjb-oss.oss-cn-hangzhou.aliyuncs.com/atd/material/7/image/7f0afcc14e60695f0f28f2bb9801fcf5";
ImageCombiner combiner = new ImageCombiner(bg, OutputFormat.JPG);
String str1 = "您出征";
String str2 = "某城市"; //外部传参,内容不定,宽度也不定
String str3 = ",共在前线战斗了";
String str4 = "365"; //外部传参,内容不定,宽度也不定
String str5 = "天!";
int fontSize = 60;
int xxxFontSize = 80;
int offsetX = 20; //通过计算前一个元素的实际宽度,并累加这个偏移量,得到后一个元素正确的x坐标值
int y = 300;
//第一段
TextElement element1 = combiner.addTextElement(str1, fontSize, offsetX, y);
offsetX += element1.getWidth(); //计算宽度,并累加偏移量
//第二段(内容不定,宽度也不定)
TextElement element2 = combiner.addTextElement(str2, xxxFontSize, offsetX, y)
.setColor(Color.red);
offsetX += element2.getWidth();
//第三段
TextElement element3 = combiner.addTextElement(str3, fontSize, offsetX, y);
offsetX += element3.getWidth();
//第四段(内容不定,宽度也不定)
TextElement element4 = combiner.addTextElement(str4, xxxFontSize, offsetX, y)
.setColor(Color.red);
offsetX += element4.getWidth();
//第五段
combiner.addTextElement(str5, fontSize, offsetX, y);
combiner.combine();
combiner.save("d://demo.jpg");
}
}
具体使用文档官网地址:https://gitee.com/dromara/image-combiner