嘿~好久不见。

今天给大家分享的是java代码生成海报

先看看效果图:

file

是不是还挺漂亮的,背景来自我们公司ui。

看一下代码工程:

file


controller:

package cn.cnbuild.draw.controller;

import cn.cnbuild.draw.utils.draw.PosterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

   @Autowired
   private PosterUtil posterUtil;


   @RequestMapping(value = "/")
   public String index() {

       String qrCodeUrl = "https://img.cnbuilder.cn/blog/6a63929cb1b4bd502e423bfbf25132e5.png"; //二维码
       String name = "kingyifan";
       String company = "猿码优创";

       posterUtil.drawImg(qrCodeUrl, name, company);
       return "欢迎来带猿码优创-生成海报系统";
   }
}

//draw工具类:
package cn.cnbuild.draw.utils.draw;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;

import cn.cnbuild.draw.utils.FontUtil;
import org.springframework.stereotype.Component;

@Component
public class PosterUtil {

   /**
    * 生成海报
    *
    * @param qrCodeUrl
    * @param name
    * @param company
    * @return
    */
   public String drawImg(String qrCodeUrl, String name, String company) {
       try {
           Poster poster_ = new Poster();
           //海报的宽度
           poster_.setWidth(750);
           //海报的高度
           poster_.setHeight(1334);
           poster_.setQrCodeUrl(qrCodeUrl); //二维码
           poster_.setName(name);//名字
           poster_.setCompany(company);//公司
           Poster poster = PosterUtil.initPoster(poster_);
           String url = drawPoster(poster);
           return url;

       } catch (Exception e) {
           e.printStackTrace();
       }
       return "";
   }

   //背景的图片地址
   private static String bg_img = "D:\\hcr\\dev\\source\\other\\learn\\draw\\src\\main\\resources\\draw\\bg\\bg1.png";

   /**
    * 初始化海报信息
    *
    * @param poster_
    * @return
    */
   static Poster initPoster(Poster poster_) {

       Poster poster = new Poster();
       //画布宽高
       poster.setWidth(poster_.getWidth());
       poster.setHeight(poster_.getHeight());

       //名字
       poster.setName(poster_.getName());
       //字体可以更换成本地连接,这样不会造成字体丢失
       poster.setNameFont(FontUtil.getFont(FontUtil.SIMSUN1, 60));
       poster.setNameColor(new Color(255, 255, 255));//颜色字体
       poster.setNameX(46);//名字在海报的什么位置
       poster.setNameY(418);


       //公司 --注释同上
       poster.setCompany(poster_.getCompany());
       poster.setCompanyFont(FontUtil.getFont(FontUtil.SIMSUN, 26));
       poster.setCompanyColor(new Color(255, 255, 255));
       poster.setCompanyX(48);
       poster.setCompanyY(490);


       //二维码
       poster.setQrCodeUrl(poster_.getQrCodeUrl());
       poster.setQrCodeWidth(252);
       poster.setQrCodeHeight(252);
       poster.setQrCodeY(566);
       poster.setQrCodeX(260);


       return poster;
   }

   String drawPoster(Poster poster) throws Exception {
       System.out.println("开始生成二维码");
       //开始时间
       long startTime = System.currentTimeMillis();

       String qrCodeUrl = poster.getQrCodeUrl();
       BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));

       int width = poster.getWidth();
       int height = poster.getHeight();
       //画布
       BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       Graphics2D g = (Graphics2D) canvas.getGraphics();
       BufferedImage bg = null;
       bg = ImageIO.read(new File(bg_img));
       g.drawImage(bg.getScaledInstance(width, height, Image.SCALE_DEFAULT), 0, 0, null);

       // 设置文字抗锯齿
       g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

       //画二维码
       g.drawImage(qrCodeImage.getScaledInstance(poster.getQrCodeWidth(), poster.getQrCodeHeight(), Image.SCALE_SMOOTH),
               poster.getQrCodeX(), poster.getQrCodeY(), null);

       //名字
       g.setColor(poster.getNameColor());
       g.setFont(poster.getNameFont());
       g.drawString(poster.getName(), poster.getNameX(), poster.getNameY());

       //公司
       g.setColor(poster.getCompanyColor());
       g.setFont(poster.getCompanyFont());
       g.drawString(poster.getCompany(), poster.getCompanyX(), poster.getCompanyY());

       g.dispose();

       //生成图片地址
       File resultImg = new File("D:\\hcr\\dev\\source\\other\\learn\\draw\\src\\main\\resources\\draw\\result\\" + poster.getName() + ".png");
       ImageIO.write(canvas, "png", resultImg);

       System.out.println("生成成功!");
       System.out.println("耗时: " + (System.currentTimeMillis() - startTime) / 1000.0 + "s");
       System.out.println("生成文件路径: " + resultImg.getAbsolutePath());

       //返回图片地址
       return resultImg.getAbsolutePath();
   }

}

海报实体类:
package cn.cnbuild.draw.utils.draw;

import java.awt.*;
import java.io.Serializable;

public class Poster implements Serializable {
   private int width;//海报的宽(像素为单位)
   private int height;//海报的高

   //名字
   private String name;
   private Font nameFont;
   private Color nameColor;
   private int nameX;
   private int nameY;

   //公司
   private String company;
   private Font companyFont;
   private Color companyColor;
   private int companyX;
   private int companyY;


   //二维码
   private String qrCodeUrl;
   private int qrCodeX;
   private int qrCodeY;
   private int qrCodeWidth;
   private int qrCodeHeight;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public Font getNameFont() {
       return nameFont;
   }

   public void setNameFont(Font nameFont) {
       this.nameFont = nameFont;
   }

   public Color getNameColor() {
       return nameColor;
   }

   public void setNameColor(Color nameColor) {
       this.nameColor = nameColor;
   }

   public int getNameX() {
       return nameX;
   }

   public void setNameX(int nameX) {
       this.nameX = nameX;
   }

   public int getNameY() {
       return nameY;
   }

   public void setNameY(int nameY) {
       this.nameY = nameY;
   }

   public String getCompany() {
       return company;
   }

   public void setCompany(String company) {
       this.company = company;
   }

   public Font getCompanyFont() {
       return companyFont;
   }

   public void setCompanyFont(Font companyFont) {
       this.companyFont = companyFont;
   }

   public Color getCompanyColor() {
       return companyColor;
   }

   public void setCompanyColor(Color companyColor) {
       this.companyColor = companyColor;
   }

   public int getCompanyX() {
       return companyX;
   }

   public void setCompanyX(int companyX) {
       this.companyX = companyX;
   }

   public int getCompanyY() {
       return companyY;
   }

   public void setCompanyY(int companyY) {
       this.companyY = companyY;
   }

   public String getQrCodeUrl() {
       return qrCodeUrl;
   }

   public void setQrCodeUrl(String qrCodeUrl) {
       this.qrCodeUrl = qrCodeUrl;
   }

   public int getQrCodeX() {
       return qrCodeX;
   }

   public void setQrCodeX(int qrCodeX) {
       this.qrCodeX = qrCodeX;
   }

   public int getQrCodeY() {
       return qrCodeY;
   }

   public void setQrCodeY(int qrCodeY) {
       this.qrCodeY = qrCodeY;
   }

   public int getQrCodeWidth() {
       return qrCodeWidth;
   }

   public void setQrCodeWidth(int qrCodeWidth) {
       this.qrCodeWidth = qrCodeWidth;
   }

   public int getQrCodeHeight() {
       return qrCodeHeight;
   }

   public void setQrCodeHeight(int qrCodeHeight) {
       this.qrCodeHeight = qrCodeHeight;
   }

   public int getWidth() {
       return width;
   }

   public void setWidth(int width) {
       this.width = width;
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }


}


字体工具类:
package cn.cnbuild.draw.utils;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class FontUtil {


   public static final int SIMSUN = 5;
   public static final int SIMSUN1 = 6;


   /**
    * 根据字体类型获取字体
    *
    * @param type
    * @param size
    * @return
    */
   public static Font getFont(int type, float size) {
       // 字体路径 TODO
       String path = "";
       switch (type) {
           case SIMSUN:
               path = "D:\\hcr\\dev\\source\\other\\learn\\draw\\src\\main\\resources\\draw\\font\\aa.TTF";
               break;
           case SIMSUN1:
               path = "D:\\hcr\\dev\\source\\other\\learn\\draw\\src\\main\\resources\\draw\\font\\cc.OTF";
               break;
       }

       File file = new File(path);
       InputStream inputStream = null;
       try {
           inputStream = new FileInputStream(file);


           Font sPfBoldFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);

           sPfBoldFont = sPfBoldFont.deriveFont(size);
           sPfBoldFont.deriveFont(Font.BOLD);
           return sPfBoldFont;
       } catch (FontFormatException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (inputStream != null) {
                   inputStream.close();
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       return null;
   }
}

启动文件:
package cn.cnbuild.draw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DrawApplication {

   public static void main(String[] args) {
       SpringApplication.run(DrawApplication.class, args);
   }

}

application.yml文件

spring:
 profiles:
   active: dev #选择要用那个配置文件
	
application-dev.yml文件	

#端口号访问路径
server:
 port: 12007
 servlet:
   context-path: /

pom文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.3.1.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>cn.cnbuild</groupId>
   <artifactId>draw</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>draw</name>
   <description>Demo project for Spring Boot</description>

   <properties>
       <weixin-java-pay.version>3.5.0</weixin-java-pay.version>
       <java.version>1.8</java.version>
   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
       </dependency>

       <dependency>
           <groupId>cn.hutool</groupId>
           <artifactId>hutool-all</artifactId>
           <version>4.1.21</version>
       </dependency>


       <!--添加fastjson依赖-->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.28</version>
       </dependency>


       <!-- jackson序列化 -->
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>


   </dependencies>
   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>

</project> 

启动文件:

file

终、、本文就是java生成海报,有什么问题可以联系我。


代码下载链接: https://pan.baidu.com/s/1k7naT2biSsZEm7tj2XTPcw 提取码: 8y42

鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。你们的赞助决定我更新的速度哦!

微信:

支付宝:


感谢一路支持我的人。。。。。

Love me and hold me
QQ:69673804(16年老号)
EMAIL:itw@tom.com
友链交换
如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。

网站名称:猿码优创
网站地址:http://blog.cnbuilder.cn
网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
网站Logo/头像: [头像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)

欢迎关注猿码优创(联系小优优进内部群哦,新鲜技术优先更新):

file