Spring Boot集成easyposter快速入门Demo

1.什么是easyposter?

easyposter是一个简单的,便于扩展的绘制海报工具包

使用场景

在日常工作过程中,通常一些C端平台会伴随着海报生成与分享业务。因为随着移动互联网的迅猛发展,社交分享已成为我们日常生活的重要组成部分。海报分享作为一种直观、生动的分享方式,被广泛应用于各类应用场景中,如产品推广、活动宣传、内容分享等。本文主要介绍通过Java进行海报生成的原理解析与代码示例。

2.代码工程

实验目的

使用easyposter生成一张文章海报图

pom.xml

 

xml

代码解读

复制代码

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>poster</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.github.quaintclever</groupId> <artifactId>easyposter</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>

controller

整理了这份面试笔记包括了:Java面试、Spring、JVM、MyBatis、Redis、MySQL、并发编程、微服务、Linux、Springboot、SpringCloud、MQ、Kafka 面试专题

需要全套面试笔记【点击此处】即可免费获取

kotlin

代码解读

复制代码

package com.et.poster.controller; import com.et.poster.service.PosterUtil; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; @RestController public class HelloWorldController { @RequestMapping("/hello") public Map<String, Object> showHelloWorld(){ Map<String, Object> map = new HashMap<>(); map.put("msg", "HelloWorld"); return map; } @RequestMapping(path = "/genPoster",produces = MediaType.IMAGE_PNG_VALUE) @ResponseBody public byte[] genposterpng( HttpServletRequest request, HttpServletResponse response) { try { byte[] bytes = PosterUtil.test(); OutputStream out = null; try { out = response.getOutputStream(); out.write(bytes); out.flush(); } catch (IOException ex) { ex.printStackTrace(); } return null; } catch (Exception e) { e.printStackTrace(); return null; } } }

生成海报工具类

 

java

代码解读

复制代码

package com.et.poster.service; import com.quaint.poster.core.impl.PosterDefaultImpl; import lombok.extern.slf4j.Slf4j; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import java.util.Random; /** * @author quaint * @date 21 February 2020 * @since 1.0 */ @Slf4j public class PosterUtil { public static String ossDomain="http://qdliuhaihua.oss-cn-qingdao-internal.aliyuncs.com/"; public static void main(String[] args) throws Exception{ //String in1pathString = PosterUtil.class.getResource("/static/css/fonts/simkai.ttf").getFile(); //System.out.printf(in1pathString); test(); } public static byte[] test() throws IOException, IllegalAccessException, FontFormatException { BufferedImage background = ImageIO.read(new URL("http://image.liuhaihua.cn/bg.jpg")); File file2= new File("/Users/liuhaihua/Downloads/2.jpg"); BufferedImage mainImage = ImageIO.read(file2); BufferedImage siteSlogon = ImageIO.read(new URL("http://image.liuhaihua.cn/site.jpg")); BufferedImage xx = ImageIO.read(new URL("http://image.liuhaihua.cn/xx.jpg")); File file5= new File("/Users/liuhaihua/IdeaProjects/springboot-demo/poster/src/main/resources/image/wx_300px.png"); BufferedImage qrcode = ImageIO.read(file5); SamplePoster poster = SamplePoster.builder() .backgroundImage(background) .postQrcode(qrcode) .xuxian(xx) .siteSlogon(siteSlogon) .postTitle("Java generate poster in 5 miniutes") .postDate("2022年11月14日 pm1:23 Author:Harries") .posttitleDesc("Demand Background\u200B We often encounter such a demand in multi-terminal application development: when users browse products, they feel good and want to share them with friends. At this time, the terminal (Android, Apple, H5, etc.) generates a beautiful product poster and shares it with others through WeChat or other channels. You may also encounter the demand: make a personal business card and print it out or share it with others. The effect is probably like this: It may also be like this (the above pictures are all mine...") .mainImage(mainImage) .build(); PosterDefaultImpl<SamplePoster> impl = new PosterDefaultImpl<>(); BufferedImage test = impl.annotationDrawPoster(poster).draw(null); //ImageIO.write(test,"png",new FileOutputStream("/Users/liuhaihua/annTest.png")); ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(test, "png", stream); return stream.toByteArray(); } }

以上只是一些关键代码

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值