preface
以下是我在实际工作的项目中,想要实现从后端生成海报的功能,本来项目使用golang开发的,但是对于golang的画图不是很熟悉,所以想到使用Java,但是对于Java的画图其实也不熟悉,但是通过在网上找资料,也算是实现了,本文介绍一下如何实现的步骤
说明
用来处理图像压缩的好用工具
thumbnailator
处理生成二维码的工具
zxing
pom依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
相当于compile,但是打包阶段做了exclude操作-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
以下是主要代码
code
工具方法
public static BufferedImage generate(int type, String msgTitle, String link) throws Exception {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(Objects.requireNonNull(UltimateImgGenerator.class.getClassLoader().getResourceAsStream("img.png")));
System.out.println(bimg.getHeight());
System.out.println(bimg.getWidth());
} catch (IOException e1) {
e1.printStackTrace<

本文介绍了如何在Java中利用thumbnailator库进行图像压缩,ZXing库生成二维码,并结合SpringBoot实现后端生成海报的功能。详细讲解了代码实现过程,包括文字居中、二维码插入及图片处理等关键步骤。
最低0.47元/天 解锁文章

290

被折叠的 条评论
为什么被折叠?



