使用java Graphics2D生成海报图片

前言:最近做的一个项目需要给商户提供生成海报的功能。本来想着用前端canvas来实现,用户自己去托拉拽,生成想要的图片,可视化比较好。后来需求是要在后端生成固定格式的海报图片,只是商户信息发生改变。所以我就写了一个海报工具类,初步实现了需求想要的效果。由于图片存储方式还未确定,所以我默认把生成的图片存到了c盘目录下,到时再根据具体的要求对save()方法进行修改即可。

声明:图片素材来源于网络,如有侵权请告知。

最终效果

部分代码

package com.jazj.canvas.util;

import org.thymeleaf.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品海报生成工具类.
 */
public class ImageUtil {
    // 背景参数
    private String backgroundPath;
    private int backgroundWith;
    private int backgroundHeight;

    // 公司参数
    private String logoPath;
    private int logoX;
    private int logoY;
    private int logoWith;
    private int logoHeight;

    // 商户参数
    // 商户logo
    private String shopLogoPath;
    private int shopLogoX;
    private int shopLogoY;
    private int shopLogoWith;
    private int shopLogoHeight;
    // 商户名称
    private String shopName;
    private int shopNameX;
    private int shopNameY;
    private int shopNameWith;
    private int shopNameHeight;
    // 商户二维码
    private String qrCodePath;
    private int qrCodeX;
    private int qrCodeY;
    private int qrCodeWith;
    private int qrCodeHeight;

    // 商品参数
    // 商品图片
    private String goodsPicturePath;
    private int goodsPictureX;
    private int goodsPictureY;
    private int goodsPictureWith;
    private int goodsPictureHeight;
    // 商品描述
    private String goodsDescription;
    private int goodsDescriptionX;
    private int goodsDescriptionY;
    private int goodsDescriptionWith;
    private int goodsDescriptionHeight;

    // 字体参数
    private String font;
    private int fontSize;

    // 图片信息
    private String pictureType;
    private String urlPath;


    public String getBackgroundPath() {
        return backgroundPath;
    }

    public void setBackgroundPath(String backgroundPath) {
        this.backgroundPath = backgroundPath;
    }

    public int getBackgroundWith() {
        return backgroundWith;
    }

    public void setBackgroundWith(int backgroundWith) {
        this.backgroundWith = backgroundWith;
    }

    public int getBackgroundHeight() {
        return backgroundHeight;
    }

    public void setBackgroundHeight(int backgroundHeight) {
        this.backgroundHeight = backgroundHeight;
    }

    public String getLogoPath() {
        return logoPath;
    }

    public void setLogoPath(String logoPath) {
        this.logoPath = logoPath;
    }

    public int getLogoX() {
        return logoX;
    }

    public void setLogoX(int logoX) {
        this.logoX = logoX;
    }

    public int getLogoY() {
        return logoY;
    }

    public void setLogoY(int logoY) {
        this.logoY = logoY;
    }

    public int getLogoWith() {
        return logoWith;
    }

    public void setLogoWith(int logoWith) {
        this.logoWith = logoWith;
    }

    public int getLogoHeight() {
        return logoHeight;
    }

    public void setLogoHeight(int logoHeight) {
        this.logoHeight = logoHeight;
    }

    public String getShopLogoPath() {
        return shopLogoPath;
    }

    public void setShopLogoPath(String shopLogoPath) {
        this.shopLogoPath = shopLogoPath;
    }

    public int getShopLogoX() {
        return shopLogoX;
    }

    public void setShopLogoX(int shopLogoX) {
        this.shopLogoX = shopLogoX;
    }

    public int getShopLogoY() {
        return shopLogoY;
    }

    public void setShopLogoY(int shopLogoY) {
        this.shopLogoY = shopLogoY;
    }

    public int getShopLogoWith() {
        return shopLogoWith;
    }

    public void setShopLogoWith(int shopLogoWith) {
        this.shopLogoWith = shopLogoWith;
    }

    public int getShopLogoHeight() {
        return shopLogoHeight;
    }

    public void setShopLogoHeight(int shopLogoHeight) {
        this.shopLogoHeight = shopLogoHeight;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public int getShopNameX() {
        return shopNameX;
    }

    public void setShopNameX(int shopNameX) {
        this.shopNameX = shopNameX;
    }

    public int getShopNameY() {
        return shopNameY;
    }

    public void setShopNameY(int shopNameY) {
        this.shopNameY = shopNameY;
    }

    public int getShopNameWith() {
        return shopNameWith;
    }

    public void setShopNameWith(int shopNameWith) {
        this.shopNameWith = shopNameWith;
    }

    public int getShopNameHeight() {
        return shopNameHeight;
    }

    public void setShopNameHeight(int shopNameHeight) {
        this.shopNameHeight = shopNameHeight;
    }

    public String getQrCodePath() {
        return qrCodePath;
    }

    public void setQrCodePath(String qrCodePath) {
        this.qrCodePath = qrCodePath;
    }

    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 getQrCodeWith() {
        return qrCodeWith;
    }

    public void setQrCodeWith(int qrCodeWith) {
        this.qrCodeWith = qrCodeWith;
    }

    public int getQrCodeHeight() {
        return qrCodeHeight;
    }

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

    public String getGoodsPicturePath() {
        return goodsPicturePath;
    }

    public void setGoodsPicturePath(String goodsPicturePath) {
        this.goodsPicturePath = goodsPicturePath;
    }

    public int getGoodsPictureX() {
        return goodsPictureX;
    }

    public void setGoodsPictureX(int goodsPictureX) {
        this.goodsPictureX = goodsPictureX;
    }

    public int getGoodsPictureY() {
        return goodsPictureY;
    }

    public void setGoodsPictureY(int goodsPictureY) {
        this.goodsPictureY = goodsPictureY;
    }

    public int getGoodsPictureWith() {
        return goodsPictureWith;
    }

    public void setGoodsPictureWith(int goodsPictureWith) {
        this.goodsPictureWith = goodsPictureWith;
    }

    public int getGoodsPictureHeight() {
        return goodsPictureHeight;
    }

    public void setGoodsPictureHeight(int goodsPictureHeight) {
        this.goodsPictureHeight = goodsPictureHeight;
    }

    public String getGoodsDescription() {
        return goodsDescription;
    }

    public void setGoodsDescription(String goodsDescription) {
        this.goodsDescription = goodsDescription;
    }

    public int getGoodsDescriptionX() {
        return goodsDescriptionX;
    }

    public void setGoodsDescriptionX(int goodsDescriptionX) {
        this.goodsDescriptionX = goodsDescriptionX;
    }

    public int getGoodsDescriptionY() {
        return goodsDescriptionY;
    }

    public void setGoodsDescriptionY(int goodsDescriptionY) {
        this.goodsDescriptionY = goodsDescriptionY;
    }

    public int getGoodsDescriptionWith() {
        return goodsDescriptionWith;
    }

    public void setGoodsDescriptionWith(int goodsDescriptionWith) {
        this.goodsDescriptionWith = goodsDescriptionWith;
    }

    public int getGoodsDescriptionHeight() {
        return goodsDescriptionHeight;
    }

    public void setGoodsDescriptionHeight(int goodsDescriptionHeight) {
        this.goodsDescriptionHeight = goodsDescriptionHeight;
    }

    public String getFont() {
        return font;
    }

    public void setFont(String font) {
        this.font = font;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public String getPictureType() {
        return pictureType;
    }

    public void setPictureType(String pictureType) {
        this.pictureType = pictureType;
    }

    public String getUrlPath() {
        return urlPath;
    }

    public void setUrlPath(String urlPath) {
        this.urlPath = urlPath;
    }


  

    /**
     * 生成海报.
     *
     * @return
     */
    public static String getPoster(ImageUtil imageUtil) throws Exception {
        // 背景
        BufferedImage canvas = drawInit(imageUtil.getBackgroundPath(), imageUtil.getBackgroundWith(), imageUtil.getBackgroundHeight());
        // 公司信息
        BufferedImage logo = ImageIO.read(new URL(imageUtil.getLogoPath()));
        canvas = drawLogo(canvas, logo, imageUtil.getLogoX(), imageUtil.getLogoY());
        // 商户信息
        BufferedImage shopLogo = drawInit(imageUtil.getShopLogoPath(), imageUtil.getShopLogoWith(), imageUtil.getShopLogoHeight());
        BufferedImage qrCode = drawInit(imageUtil.getQrCodePath(), imageUtil.getQrCodeWith(), imageUtil.getQrCodeHeight());
        canvas = drawBusiness(canvas, shopLogo, imageUtil.getShopLogoX(), imageUtil.getShopLogoY(),
                imageUtil.getShopName(), imageUtil.getShopNameX(), imageUtil.getShopNameY(), imageUtil.getShopNameWith(), imageUtil.getShopNameHeight(),
                imageUtil.getFont(), imageUtil.getFontSize(),
                qrCode, imageUtil.getQrCodeX(), imageUtil.getQrCodeY());
        // 商品信息
        BufferedImage goodsPicture = drawInit(imageUtil.getGoodsPicturePath(), imageUtil.getGoodsPictureWith(), imageUtil.getGoodsPictureHeight());
        canvas = drawGoods(canvas, goodsPicture, imageUtil.getGoodsPictureX(), imageUtil.getGoodsPictureY(),
                imageUtil.getGoodsDescription(), imageUtil.getGoodsDescriptionX(), imageUtil.getGoodsDescriptionY(), imageUtil.getGoodsDescriptionWith(), imageUtil.getGoodsDescriptionHeight(),
                imageUtil.getFont(), imageUtil.getFontSize());
        save(canvas, imageUtil.getPictureType(), imageUtil.getUrlPath());
        return null;
    }

    /**
     * test.
     *
     * @param args args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
       
        // 生成海报
        ImageUtil imageUtil = new ImageUtil();
        imageUtil.setBackgroundPath("https://bbsfiles.vivo.com.cn/vivobbs/attachment/forum/201709/04/130640ltgsgdgfsdsogizz.jpg.thumb.jpg");
        imageUtil.setBackgroundWith(1080);
        imageUtil.setBackgroundHeight(1920);

        imageUtil.setLogoPath("http://d.lanrentuku.com/down/png/1712/10xiaodongwu/10xiaodongwu_10.png");
        imageUtil.setLogoX(800);
        imageUtil.setLogoY(0);
        //imageUtil.setLogoWith();
        //imageUtil.setLogoHeight();

        imageUtil.setShopLogoPath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553763241787&di=81ed9ec823de36398d97f88f2a3efa02&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F12%2F38%2F54%2F27y58PICXCf.jpg");
        imageUtil.setShopLogoX(50);
        imageUtil.setShopLogoY(50);
        imageUtil.setShopLogoWith(150);
        imageUtil.setShopLogoHeight(150);

        imageUtil.setShopName("大鸭梨水果店");
        imageUtil.setShopNameX(250);
        imageUtil.setShopNameY(180);
        imageUtil.setShopNameWith(300);
        imageUtil.setShopNameHeight(100);

        imageUtil.setQrCodePath("http://www.ccoalnews.com/pic/201804/16/72078895-85a8-490b-af38-055e8bdffe14_zsize.jpg");
        imageUtil.setQrCodeX(700);
        imageUtil.setQrCodeY(1550);
        imageUtil.setQrCodeWith(300);
        imageUtil.setQrCodeHeight(300);

        imageUtil.setGoodsPicturePath("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553770919167&di=24da75226fe0c645ca02bba04dbb0bf2&imgtype=0&src=http%3A%2F%2Fimg.yzcdn.cn%2Fupload_files%2F2015%2F05%2F18%2FFkYMD2dI23bRB61X_MPR8S7s7ZdV.jpg%2521580x580.jpg");
        imageUtil.setGoodsPictureX(50);
        imageUtil.setGoodsPictureY(400);
        imageUtil.setGoodsPictureWith(500);
        imageUtil.setGoodsPictureHeight(500);

        imageUtil.setGoodsDescription("泰国金枕头,来自大自然的馈赠!特价88元,欲购从速!");
        imageUtil.setGoodsDescriptionX(50);
        imageUtil.setGoodsDescriptionY(1000);
        imageUtil.setGoodsDescriptionWith(500);
        imageUtil.setGoodsDescriptionHeight(100);

        imageUtil.setFont("微软雅黑");
        imageUtil.setFontSize(50);

        imageUtil.setPictureType("jpg");
        imageUtil.setUrlPath("c:\\poster.jpg");

        getPoster(imageUtil);
    }
}

分步生成图片效果

1、调整图片尺寸。
使用java Graphics2D生成海报图片_第1张图片
2、绘制公司logo。
使用java Graphics2D生成海报图片_第2张图片
3、绘制商户信息。
使用java Graphics2D生成海报图片_第3张图片
4、绘制商品信息。
使用java Graphics2D生成海报图片_第4张图片
5、生成海报。
使用java Graphics2D生成海报图片_第5张图片

代码下载地址:

java海报生成工具类

你可能感兴趣的:(java开发应用)