• Spring Boot集成easyposter快速入门Demo


    1.什么是easyposter?

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

    使用场景

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

    2.代码工程

    实验目的

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

    pom.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>springboot-demo</artifactId>
    7. <groupId>com.et</groupId>
    8. <version>1.0-SNAPSHOT</version>
    9. </parent>
    10. <modelVersion>4.0.0</modelVersion>
    11. <artifactId>poster</artifactId>
    12. <properties>
    13. <maven.compiler.source>11</maven.compiler.source>
    14. <maven.compiler.target>11</maven.compiler.target>
    15. </properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-starter-web</artifactId>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.springframework.boot</groupId>
    23. <artifactId>spring-boot-autoconfigure</artifactId>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.boot</groupId>
    27. <artifactId>spring-boot-starter-test</artifactId>
    28. <scope>test</scope>
    29. </dependency>
    30. <dependency>
    31. <groupId>com.github.quaintclever</groupId>
    32. <artifactId>easyposter</artifactId>
    33. <version>1.2</version>
    34. </dependency>
    35. <dependency>
    36. <groupId>org.projectlombok</groupId>
    37. <artifactId>lombok</artifactId>
    38. </dependency>
    39. </dependencies>
    40. </project>

    controller

    1. package com.et.poster.controller;
    2. import com.et.poster.service.PosterUtil;
    3. import org.springframework.http.MediaType;
    4. import org.springframework.web.bind.annotation.PathVariable;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.io.IOException;
    11. import java.io.OutputStream;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. @RestController
    15. public class HelloWorldController {
    16. @RequestMapping("/hello")
    17. public Map showHelloWorld(){
    18. Map map = new HashMap<>();
    19. map.put("msg", "HelloWorld");
    20. return map;
    21. }
    22. @RequestMapping(path = "/genPoster",produces = MediaType.IMAGE_PNG_VALUE)
    23. @ResponseBody
    24. public byte[] genposterpng( HttpServletRequest request,
    25. HttpServletResponse response) {
    26. try {
    27. byte[] bytes = PosterUtil.test();
    28. OutputStream out = null;
    29. try {
    30. out = response.getOutputStream();
    31. out.write(bytes);
    32. out.flush();
    33. } catch (IOException ex) {
    34. ex.printStackTrace();
    35. }
    36. return null;
    37. } catch (Exception e) {
    38. e.printStackTrace();
    39. return null;
    40. }
    41. }
    42. }

    生成海报工具类

    1. package com.et.poster.service;
    2. import com.quaint.poster.core.impl.PosterDefaultImpl;
    3. import lombok.extern.slf4j.Slf4j;
    4. import javax.imageio.ImageIO;
    5. import java.awt.*;
    6. import java.awt.image.BufferedImage;
    7. import java.io.*;
    8. import java.net.URL;
    9. import java.util.Random;
    10. /**
    11. * @author quaint
    12. * @date 21 February 2020
    13. * @since 1.0
    14. */
    15. @Slf4j
    16. public class PosterUtil {
    17. public static String ossDomain="http://qdliuhaihua.oss-cn-qingdao-internal.aliyuncs.com/";
    18. public static void main(String[] args) throws Exception{
    19. //String in1pathString = PosterUtil.class.getResource("/static/css/fonts/simkai.ttf").getFile();
    20. //System.out.printf(in1pathString);
    21. test();
    22. }
    23. public static byte[] test() throws IOException, IllegalAccessException, FontFormatException {
    24. BufferedImage background = ImageIO.read(new URL("http://image.liuhaihua.cn/bg.jpg"));
    25. File file2= new File("/Users/liuhaihua/Downloads/2.jpg");
    26. BufferedImage mainImage = ImageIO.read(file2);
    27. BufferedImage siteSlogon = ImageIO.read(new URL("http://image.liuhaihua.cn/site.jpg"));
    28. BufferedImage xx = ImageIO.read(new URL("http://image.liuhaihua.cn/xx.jpg"));
    29. File file5= new File("/Users/liuhaihua/IdeaProjects/springboot-demo/poster/src/main/resources/image/wx_300px.png");
    30. BufferedImage qrcode = ImageIO.read(file5);
    31. SamplePoster poster = SamplePoster.builder()
    32. .backgroundImage(background)
    33. .postQrcode(qrcode)
    34. .xuxian(xx)
    35. .siteSlogon(siteSlogon)
    36. .postTitle("Java generate poster in 5 miniutes")
    37. .postDate("2022年11月14日 pm1:23 Author:Harries")
    38. .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...")
    39. .mainImage(mainImage)
    40. .build();
    41. PosterDefaultImpl<SamplePoster> impl = new PosterDefaultImpl<>();
    42. BufferedImage test = impl.annotationDrawPoster(poster).draw(null);
    43. //ImageIO.write(test,"png",new FileOutputStream("/Users/liuhaihua/annTest.png"));
    44. ByteArrayOutputStream stream = new ByteArrayOutputStream();
    45. ImageIO.write(test, "png", stream);
    46. return stream.toByteArray();
    47. }
    48. }

    以上只是一些关键代码,所有代码请参见下面代码仓库

    代码仓库

    3.测试

    启动Spring Boot应用程序

    测试生成海报

    访问http://127.0.0.1:8088/genPoster,返回图片如下图

    genPoster

    4.引用

  • 相关阅读:
    LeetCode39. Combination Sum
    端到端的基于深度学习的网络入侵检测方法
    在VScode中如何将界面语言设置为中文
    【SQL刷题】DAY16----SQL高级联结专项练习
    计算机组成原理---第七章输入输出系统---I/O系统基本概念,外部系统---选择题
    数据库的备份与恢复
    Linux环境下conda虚拟环境的迁移
    langchain主要模块(五):Agent以及Wandb
    学习Source Generators之HelloWorld
    我们为什么要做一名系统管理员?
  • 原文地址:https://blog.csdn.net/dot_life/article/details/140408198