生而为人 谁不付出 谁不努力

微信小程序端有关于一篇文章生成一张海报图片,用于用户保存之后分享,实际开发的过程中遇到的一些问题如下:

1:开发编辑器里面生成存在的情况 

wx.getImageInfo()方法处理之后才可以

                

wx.getImageInfo(Object object)

获取图片信息。网络图片需先配置download域名才能生效。

参数

Object object

属性

类型

默认值

必填

说明

src

string

 


图片的路径,支持网络路径、本地路径、代码包路径

success

function

 


接口调用成功的回调函数

fail

function

 


接口调用失败的回调函数

complete

function

 


接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数

Object res

属性

类型

说明

最低版本

width

number

图片原始宽度,单位px。不考虑旋转。

 

height

number

图片原始高度,单位px。不考虑旋转。

 

path

string

图片的本地路径

 

orientation

string

拍照时设备方向

1.9.90

type

string

图片格式

1.9.90

2:生成海报图片时候 因为微信小程序是同步请求数据 

        有的数据信息还没有拿到就去实现生成了海报分享图片,就会存在有时候没有图片,有时候没有文章,所以最后想到直接在服务器端生成一张图片

3:Java后台生成海报

       话不多数 ,这个我就直接上代码,直接运行就可以看到实际的效果,但是每个人生成的海报样式什么的 需要自己慢慢的的调试,我记得我调试文本的时候是真的难的很 一个一个像素的调啊 好了 下面贴出代码

A:main方法执行就可以看到生成的海报图片啦

           

package com.myFirstSpring.test; 

import java.awt.Font;

import sun.font.FontDesignMetrics;

/** 
 * @author tqf
 * @version 创建时间:2020-3-13 下午2:36:41 
 * 类说明:
 */
public class PosterMain {
	public static void main(String[] args) throws Exception {
        String qrCodeUrl = "http://m.xinjuenet.com/images/banner/i-wx.jpg"; //二维码
        String goodsUrl = "http://m.xinjuenet.com/images/banner/b-xldh.jpg"; //顶部图片
        String avatarUrl = "http://images.xinjuenet.com//20190529140101.jpg"; //头像
        String name = "和自己对话";
        String desc = "沉默王二1,《Web 全栈开发进阶之路》作者;一个不止写代码的程序员,还写有趣有益的文字,给不喜欢严肃的你。沉默王二,《Web 全栈开发进阶之路》作者;一个不止写代码的程序员,还写有趣有益的文字,给不喜欢严肃的你。";
        String price = "重度抑郁1";
        
        Poster poster_ = new Poster();
        poster_.setWidth(375);
        poster_.setHeight(670);
        poster_.setQrCodeUrl(qrCodeUrl); //二维码
        poster_.setGoodsUrl(goodsUrl); //顶部banner
        poster_.setDesc(desc); //测评结果说明
        poster_.setPrice(price); //测评结果标题

        Poster poster = PosterUtil.initPoster(poster_);
        PosterUtil.drawPoster(poster);
        
        
    }
	
	
	

}

   B:关于生成海报图片的实体类 Poster

   

package com.myFirstSpring.test; 

import java.awt.Color;
import java.awt.Font;
import java.io.Serializable;

/** 
 * @author tqf
 * @version 创建时间:2020-3-13 下午2:32:59 
 * 类说明:Poster实体类
 */
public class Poster implements Serializable {
    private int width;//海报的宽(像素为单位)
    private int height;//海报的高
    //头像
    private String avatarUrl;//头像url
    private int avatarX;//头像左上角横坐标
    private int avatarY;//头像左上角纵坐标
    private int avatarWidth;//头像宽
    private int avatarHeight;//头像高
    private boolean isCircle;//是否圆形头像
    //名字
    private String name;
    private Font nameFont;
    private Color nameColor;
    private int nameX;
    private int nameY;
    //商品
    private String goodsUrl;
    private int goodsX;
    private int goodsY;
    private int goodsWidth;
    private int goodsHeight;
    //商品描述
    private String desc;
    private Font descFont;
    private Color descColor;
    private int descX;
    private int descY;
    //商品价格
    private String price;
    private Font priceFont;
    private Color priceColor;
    private int priceX;
    private int priceY;
    
    //测评结果标题
    private String result;
    private Font resultFont;
    private Color resultColor;
    private int resultX;
    private int resultY;
    
    //测评结果描述
    private String result_content;
    private Font result_contentFont;
    private Color result_contentColor;
    private int result_contentX;
    private int result_contentY;
    
    /**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * @return the resultFont
	 */
	public Font getResultFont() {
		return resultFont;
	}

	/**
	 * @param resultFont the resultFont to set
	 */
	public void setResultFont(Font resultFont) {
		this.resultFont = resultFont;
	}

	/**
	 * @return the resultColor
	 */
	public Color getResultColor() {
		return resultColor;
	}

	/**
	 * @param resultColor the resultColor to set
	 */
	public void setResultColor(Color resultColor) {
		this.resultColor = resultColor;
	}

	/**
	 * @return the resultX
	 */
	public int getResultX() {
		return resultX;
	}

	/**
	 * @param resultX the resultX to set
	 */
	public void setResultX(int resultX) {
		this.resultX = resultX;
	}

	/**
	 * @return the resultY
	 */
	public int getResultY() {
		return resultY;
	}

	/**
	 * @param resultY the resultY to set
	 */
	public void setResultY(int resultY) {
		this.resultY = resultY;
	}

	/**
	 * @return the result_content
	 */
	public String getResult_content() {
		return result_content;
	}

	/**
	 * @param result_content the result_content to set
	 */
	public void setResult_content(String result_content) {
		this.result_content = result_content;
	}

	/**
	 * @return the result_contentFont
	 */
	public Font getResult_contentFont() {
		return result_contentFont;
	}

	/**
	 * @param result_contentFont the result_contentFont to set
	 */
	public void setResult_contentFont(Font result_contentFont) {
		this.result_contentFont = result_contentFont;
	}

	/**
	 * @return the result_contentColor
	 */
	public Color getResult_contentColor() {
		return result_contentColor;
	}

	/**
	 * @param result_contentColor the result_contentColor to set
	 */
	public void setResult_contentColor(Color result_contentColor) {
		this.result_contentColor = result_contentColor;
	}

	/**
	 * @return the result_contentX
	 */
	public int getResult_contentX() {
		return result_contentX;
	}

	/**
	 * @param result_contentX the result_contentX to set
	 */
	public void setResult_contentX(int result_contentX) {
		this.result_contentX = result_contentX;
	}

	/**
	 * @return the result_contentY
	 */
	public int getResult_contentY() {
		return result_contentY;
	}

	/**
	 * @param result_contentY the result_contentY to set
	 */
	public void setResult_contentY(int result_contentY) {
		this.result_contentY = result_contentY;
	}

	//小程序码
    private String qrCodeUrl;
    private int qrCodeX;
    private int qrCodeY;
    private int qrCodeWidth;
    private int qrCodeHeight;
    //提示1
    private String tip1;
    private Font tip1Font;
    private Color tip1Color;
    private int tip1X;
    private int tip1Y;
    //提示2
    private String tip2;
    private Font tip2Font;
    private Color tip2Color;
    private int tip2X;
    private int tip2Y;
    //底部栏
    private Color footerColor;
    private int footerWidth;
    private int footerHeight;
    private int footerX;
    private int footerY;
    //底部栏提示字
    private String footerTip;
    private Font footerTipFont;
    private Color footerTipColor;
    private int footerTipX;
    private int footerTipY;

    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;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public int getAvatarX() {
        return avatarX;
    }

    public void setAvatarX(int avatarX) {
        this.avatarX = avatarX;
    }

    public int getAvatarY() {
        return avatarY;
    }

    public void setAvatarY(int avatarY) {
        this.avatarY = avatarY;
    }

    public int getAvatarWidth() {
        return avatarWidth;
    }

    public void setAvatarWidth(int avatarWidth) {
        this.avatarWidth = avatarWidth;
    }

    public int getAvatarHeight() {
        return avatarHeight;
    }

    public void setAvatarHeight(int avatarHeight) {
        this.avatarHeight = avatarHeight;
    }

    public boolean isCircle() {
        return isCircle;
    }

    public void setCircle(boolean circle) {
        isCircle = circle;
    }

    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 getGoodsUrl() {
        return goodsUrl;
    }

    public void setGoodsUrl(String goodsUrl) {
        this.goodsUrl = goodsUrl;
    }

    public int getGoodsX() {
        return goodsX;
    }

    public void setGoodsX(int goodsX) {
        this.goodsX = goodsX;
    }

    public int getGoodsY() {
        return goodsY;
    }

    public void setGoodsY(int goodsY) {
        this.goodsY = goodsY;
    }

    public int getGoodsWidth() {
        return goodsWidth;
    }

    public void setGoodsWidth(int goodsWidth) {
        this.goodsWidth = goodsWidth;
    }

    public int getGoodsHeight() {
        return goodsHeight;
    }

    public void setGoodsHeight(int goodsHeight) {
        this.goodsHeight = goodsHeight;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Font getDescFont() {
        return descFont;
    }

    public void setDescFont(Font descFont) {
        this.descFont = descFont;
    }

    public Color getDescColor() {
        return descColor;
    }

    public void setDescColor(Color descColor) {
        this.descColor = descColor;
    }

    public int getDescX() {
        return descX;
    }

    public void setDescX(int descX) {
        this.descX = descX;
    }

    public int getDescY() {
        return descY;
    }

    public void setDescY(int descY) {
        this.descY = descY;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Font getPriceFont() {
        return priceFont;
    }

    public void setPriceFont(Font priceFont) {
        this.priceFont = priceFont;
    }

    public Color getPriceColor() {
        return priceColor;
    }

    public void setPriceColor(Color priceColor) {
        this.priceColor = priceColor;
    }

    public int getPriceX() {
        return priceX;
    }

    public void setPriceX(int priceX) {
        this.priceX = priceX;
    }

    public int getPriceY() {
        return priceY;
    }

    public void setPriceY(int priceY) {
        this.priceY = priceY;
    }

    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 String getTip1() {
        return tip1;
    }

    public void setTip1(String tip1) {
        this.tip1 = tip1;
    }

    public Font getTip1Font() {
        return tip1Font;
    }

    public void setTip1Font(Font tip1Font) {
        this.tip1Font = tip1Font;
    }

    public Color getTip1Color() {
        return tip1Color;
    }

    public void setTip1Color(Color tip1Color) {
        this.tip1Color = tip1Color;
    }

    public int getTip1X() {
        return tip1X;
    }

    public void setTip1X(int tip1X) {
        this.tip1X = tip1X;
    }

    public int getTip1Y() {
        return tip1Y;
    }

    public void setTip1Y(int tip1Y) {
        this.tip1Y = tip1Y;
    }

    public String getTip2() {
        return tip2;
    }

    public void setTip2(String tip2) {
        this.tip2 = tip2;
    }

    public Font getTip2Font() {
        return tip2Font;
    }

    public void setTip2Font(Font tip2Font) {
        this.tip2Font = tip2Font;
    }

    public Color getTip2Color() {
        return tip2Color;
    }

    public void setTip2Color(Color tip2Color) {
        this.tip2Color = tip2Color;
    }

    public int getTip2X() {
        return tip2X;
    }

    public void setTip2X(int tip2X) {
        this.tip2X = tip2X;
    }

    public int getTip2Y() {
        return tip2Y;
    }

    public void setTip2Y(int tip2Y) {
        this.tip2Y = tip2Y;
    }

    public Color getFooterColor() {
        return footerColor;
    }

    public void setFooterColor(Color footerColor) {
        this.footerColor = footerColor;
    }

    public int getFooterWidth() {
        return footerWidth;
    }

    public void setFooterWidth(int footerWidth) {
        this.footerWidth = footerWidth;
    }

    public int getFooterHeight() {
        return footerHeight;
    }

    public void setFooterHeight(int footerHeight) {
        this.footerHeight = footerHeight;
    }

    public int getFooterX() {
        return footerX;
    }

    public void setFooterX(int footerX) {
        this.footerX = footerX;
    }

    public int getFooterY() {
        return footerY;
    }

    public void setFooterY(int footerY) {
        this.footerY = footerY;
    }

    public String getFooterTip() {
        return footerTip;
    }

    public void setFooterTip(String footerTip) {
        this.footerTip = footerTip;
    }

    public Font getFooterTipFont() {
        return footerTipFont;
    }

    public void setFooterTipFont(Font footerTipFont) {
        this.footerTipFont = footerTipFont;
    }

    public Color getFooterTipColor() {
        return footerTipColor;
    }

    public void setFooterTipColor(Color footerTipColor) {
        this.footerTipColor = footerTipColor;
    }

    public int getFooterTipX() {
        return footerTipX;
    }

    public void setFooterTipX(int footerTipX) {
        this.footerTipX = footerTipX;
    }

    public int getFooterTipY() {
        return footerTipY;
    }

    public void setFooterTipY(int footerTipY) {
        this.footerTipY = footerTipY;
    }
}

  C:生成海报的工具方法 

package com.myFirstSpring.test; 

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

import sun.font.FontDesignMetrics;

/** 
 * @author tqf
 * @version 创建时间:2020-3-13 下午2:34:33 
 * 类说明:海报工具类
 */
public class PosterUtil {
	 static Poster initPoster(Poster poster_) {
       Poster poster = new Poster();
       //画布
       poster.setWidth(poster_.getWidth());
       poster.setHeight(poster_.getHeight());
       //头像
      /* poster.setAvatarUrl(avatarUrl);
       poster.setCircle(true);
       poster.setAvatarX((int)(width/11.5));
       poster.setAvatarY(height/28);
       poster.setAvatarWidth(width/6);
       poster.setAvatarHeight(width/6);
       //名字
       poster.setName(name);
       poster.setNameFont(new Font("宋体",Font.PLAIN,width/24));
       poster.setNameColor(new Color(33,33,33));
       poster.setNameX(poster.getAvatarX()+poster.getAvatarWidth()+20);
       poster.setNameY(poster.getAvatarY()+poster.getAvatarHeight()/2+15);*/
       //商品
       poster.setGoodsUrl(poster_.getGoodsUrl());
       poster.setGoodsWidth(375); //banner图宽度填充满
       poster.setGoodsHeight(150);
       poster.setGoodsX((poster_.getWidth()-poster.getGoodsWidth())/2);
       poster.setGoodsY(poster.getAvatarY()+poster.getAvatarHeight()); //+100是往下移动
       //测评结果
       poster.setDesc("测评结果");
       poster.setDescColor(Color.GRAY);
       int size = poster_.getWidth()/17;
       poster.setDescFont(new Font("宋体",Font.BOLD,18));
       poster.setDescX((poster_.getWidth()-"测评结果".length()*size)/6-15);
       poster.setDescY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(poster_.getHeight()/21.3)+15);
       //测评结果标题
       poster.setPrice(poster_.getPrice());
       poster.setPriceColor(Color.BLACK);
       poster.setPriceFont(new Font("宋体",Font.BOLD,size));
       poster.setPriceX((poster_.getWidth()-poster_.getPrice().length()*size)/5);
       poster.setPriceY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(poster_.getHeight()/10.6)+18);
       
       //测评说明
       poster.setResult("测评说明");
       poster.setResultColor(Color.GRAY);
       poster.setResultFont(new Font("宋体",Font.BOLD,18));
       poster.setResultX((poster_.getWidth()-"测评说明".length()*size)/6-15);
       poster.setResultY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(poster_.getHeight()/21.3)+90);
       
       //测评说明描述
       poster.setResult_content(poster_.getDesc());
       poster.setResult_contentColor(Color.BLACK);
       poster.setResult_contentFont(new Font("宋体",Font.BOLD,17));
       poster.setResult_contentX((3*size)/6+23);
       poster.setResult_contentY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(poster_.getHeight()/21.3)+120);
 
       
       //小程序码
       poster.setQrCodeUrl(poster_.getQrCodeUrl());
       poster.setQrCodeWidth((int)(poster_.getWidth()/2.85));
       poster.setQrCodeHeight((int)(poster_.getWidth()/2.85));
       poster.setQrCodeX((int)(poster_.getWidth()/16.5));
       poster.setQrCodeY(poster_.getHeight()-poster.getQrCodeHeight()-(int)(poster_.getHeight()/7.68));
       //tips1
       poster.setTip1("长按识别小程序码");
       poster.setTip1Color(Color.BLACK);
       poster.setTip1Font(new Font("宋体",Font.BOLD,poster_.getWidth()/21));
       poster.setTip1X(poster.getQrCodeX()+poster.getQrCodeWidth()+20);
       poster.setTip1Y(poster.getQrCodeY()+poster.getQrCodeHeight()/2+10);
       //tips2
       poster.setTip2("咨询我们是专业的");//好物与好友一起分享  
       poster.setTip2Color(Color.GRAY);
       poster.setTip2Font(new Font("宋体",Font.PLAIN,poster_.getWidth()/25)); 
       poster.setTip2X(poster.getQrCodeX()+poster.getQrCodeWidth()+20);
       poster.setTip2Y(poster.getQrCodeY()+poster.getQrCodeHeight()/2+32);//90是这句问题与上面的间距
       //footer
       poster.setFooterColor(new Color(49,196,141));
       poster.setFooterWidth(poster_.getWidth());
       poster.setFooterHeight(poster_.getHeight()/13);
       poster.setFooterX(0);
       poster.setFooterY(poster_.getHeight()-poster.getFooterHeight());
       //footer tips
       poster.setFooterTip("「心觉咨询」提供内容及技术支持");
       poster.setFooterTipColor(Color.WHITE);
       poster.setFooterTipFont(new Font("宋体",Font.BOLD,poster_.getWidth()/21));
       poster.setFooterTipX((poster_.getWidth()-(poster.getFooterTip().length()*poster_.getWidth()/21))/2);
       poster.setFooterTipY(poster_.getHeight()-poster.getFooterHeight()/3);
       return poster;
   }

   static void drawPoster(Poster poster) throws Exception {
       long startTime = System.currentTimeMillis();
       String qrCodeUrl = poster.getQrCodeUrl();
       String goodsUrl = poster.getGoodsUrl();
       String avatarUrl = poster.getAvatarUrl();
       BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
       BufferedImage goodsImage = ImageIO.read(new URL(goodsUrl));
       int width = poster.getWidth();
       int height = poster.getHeight();
       //画布
       BufferedImage canvas = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
       Graphics2D g = (Graphics2D) canvas.getGraphics();
       g.setBackground(Color.WHITE);//设置背景色
       g.clearRect(0, 0, width, height);

       // 设置文字抗锯齿
       g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       /*//圆形头像
       BufferedImage newAvatar = circle(avatarUrl,poster.getAvatarWidth());
       //画头像
       g.drawImage(newAvatar.getScaledInstance(newAvatar.getWidth(), newAvatar.getHeight(), Image.SCALE_SMOOTH), poster.getAvatarX(), poster.getAvatarY(), null);
       // 4. 写字(昵称)
       g.setColor(poster.getNameColor());
       g.setFont(poster.getNameFont());
       g.drawString(poster.getName(), poster.getNameX(), poster.getNameY());*/
       //画商品
//       g.drawImage(Thumbnails.of(goodsImage).size(poster.getGoodsWidth(), poster.getGoodsHeight()).asBufferedImage(), poster.getGoodsX(), poster.getGoodsY(), null);
       g.drawImage(goodsImage.getScaledInstance(poster.getGoodsWidth(), poster.getGoodsHeight(), Image.SCALE_SMOOTH), poster.getGoodsX(), poster.getGoodsY(), null);
       //测评结果
       g.setColor(poster.getDescColor());
       g.setFont(poster.getDescFont());
       g.drawString("测评结果", poster.getDescX(), poster.getDescY());
       //测评结果标题
       g.setColor(poster.getPriceColor());
       g.setFont(poster.getPriceFont());
       g.drawString(poster.getPrice(), poster.getPriceX(), poster.getPriceY());
       
       //画测评说明
       g.setColor(poster.getResultColor());
       g.setFont(poster.getResultFont());
       g.drawString(poster.getResult(), poster.getResultX(), poster.getResultY());
       
       
       //画测评说明描述
     /*  g.setColor(poster.getResult_contentColor());
       g.setFont(poster.getResult_contentFont());
       g.drawString("结婚苏丹发挥撒旦反对萨苏打法苏丹法苏丹法", poster.getResult_contentX()+25, poster.getResult_contentY());*/
       
       
       
       FontDesignMetrics metrics = FontDesignMetrics.getMetrics(poster.getResult_contentFont());
       String zh = poster.getResult_content();
       String[] rows = makeLineFeed(zh, metrics, 300).split("\n");
       int  y = poster.getResult_contentY();
       for (int i = 0; i < rows.length; i++) {
    	   g.setColor(poster.getResult_contentColor());
           g.setFont(poster.getResult_contentFont());
           if(i > 0){
        	   y+=28;
           }
           if(i>4){
        	   break;
           }else if(i==4){
        	   g.drawString(rows[i].substring(0, rows[i].length()-3)+" ...", poster.getResult_contentX()+25, y); 
        	   break;
           }
           g.drawString(rows[i], poster.getResult_contentX()+25, y);
           
       }
       
       
       //画小程序码
       g.drawImage(qrCodeImage.getScaledInstance(poster.getQrCodeWidth(), poster.getQrCodeHeight(), Image.SCALE_SMOOTH),
               poster.getQrCodeX(), poster.getQrCodeY(), null);
       //画tips1
       g.setColor(poster.getTip1Color());
       g.setFont(poster.getTip1Font());
       g.drawString(poster.getTip1(), poster.getTip1X(), poster.getTip1Y());
       //画tips2
       g.setColor(poster.getTip2Color());
       g.setFont(poster.getTip2Font());
       g.drawString(poster.getTip2(), poster.getTip2X(), poster.getTip2Y());
       //画底部栏
       g.setColor(poster.getFooterColor());
       g.fillRect(poster.getFooterX(),poster.getFooterY(),poster.getFooterWidth(),poster.getFooterHeight());
       //画底部栏提示
       g.setColor(poster.getFooterTipColor());
       g.setFont(poster.getFooterTipFont());
       g.drawString(poster.getFooterTip(), poster.getFooterTipX(), poster.getFooterTipY());
       g.dispose();
       File resultImg = new File("D:\\demo.png");
       ImageIO.write(canvas, "png", resultImg);
       //上传服务器代码
		//ByteArrayOutputStream bs = new ByteArrayOutputStream();
       //ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
       //ImageIO.write(canvas, "png", imgOut);
       //InputStream inSteam = new ByteArrayInputStream(bs.toByteArray());
       //String url = OSSFactory.build().upload(inSteam, UUID.randomUUID().toString()+".png");
       System.out.println("生成成功!");
       System.out.println("耗时: " + (System.currentTimeMillis()-startTime)/1000.0 + "s");
       System.out.println("生成文件路径: " + resultImg.getAbsolutePath());
   }


   private static BufferedImage circle(String avatar_img,int width) throws Exception {
       BufferedImage avatar = ImageIO.read(new URL(avatar_img));
       BufferedImage newAvatar = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
       Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, width, width);
       Graphics2D g2 = newAvatar.createGraphics();
       newAvatar = g2.getDeviceConfiguration().createCompatibleImage(width, width, Transparency.TRANSLUCENT);
       g2 = newAvatar.createGraphics();
       g2.setComposite(AlphaComposite.Clear);
       g2.fill(new Rectangle(newAvatar.getWidth(), newAvatar.getHeight()));
       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
       g2.setClip(shape);
       // 使用 setRenderingHint 设置抗锯齿
       g2 = newAvatar.createGraphics();
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       g2.fillRoundRect(0, 0,width, width, width, width);
       g2.setComposite(AlphaComposite.SrcIn);
       g2.drawImage(avatar, 0, 0, width, width, null);
       g2.dispose();
       return newAvatar;
   }
   
 //文本换行处理
 	public static String makeLineFeed(String zh, FontDesignMetrics metrics, int max_width) {
 		StringBuilder sb = new StringBuilder();
 		int line_width = 0;
 		for (int i = 0; i < zh.length(); i++) {
 			char c = zh.charAt(i);
 			sb.append(c);
 			// 如果主动换行则跳过
 			if (sb.toString().endsWith("\n")) {
 				line_width = 0;
 				continue;
 			}
 			// FontDesignMetrics 的 charWidth() 方法可以计算字符的宽度
 			int char_width = metrics.charWidth(c);
 			line_width += char_width;
 			// 如果当前字符的宽度加上之前字符串的已有宽度超出了海报的最大宽度,则换行
 			if (line_width >= max_width - char_width) {
 				line_width = 0;
 				sb.append("\n");
 			}
 		}
 		return sb.toString();
 	}
}

下面来看看实际生成的效果图吧 ,还有一点生成图片存放地址我是写File resultImg = new File("D:\\demo.png"); 

cropp java 海报 java生成海报图片_回调函数

最后说一下,如果搞好你看到这篇文章,刚好有可以用到,那就点赞收藏加关注 ,谢谢