写了两天,总算做出来了。
主要是调格式,完全是苦力活。
刚开始一行行实现,后来需要做成视频效果,又进行了部分修改。
public static void main(String[] args) throws Exception {
// ImageUtil.cut(FileUtil.Desktop+"1.jpg", FileUtil.Desktop+"tail.jpg",
// 148, 328,110,110);
//生成头
// String chatTo="自由|帮你找课|招代理";
// String curtime="晚上11:36";
// int percent=75;
// BufferedImage bi = makeHead(chatTo, curtime, percent);
BufferedImage me = ImageUtil.getRoundedImage(ImageIO.read(new File(FileUtil.Desktop+"我.png")),10);//110
BufferedImage he = ImageUtil.getRoundedImage(ImageIO.read(new File(FileUtil.Desktop+"她.png")),10);
String background=FileUtil.Desktop+"bg.jpg";//1851
String[] rows={"me`什么价格","he`腾讯文档医学会员","he`单买88 会员199","以上是打招呼的内容","15:22","me`我好像有这个了,2020年买的,标题都没变"};
// BufferedImage bi = makeChat(me, he, background, rows);
// ImageIO.write(bi, "png", new File(FileUtil.Desktop + "target.png"));
BufferedImage read = ImageIO.read(new File(FileUtil.Desktop+"bg.png"));
Graphics2D g = (Graphics2D) read.getGraphics();
g=cal(g,rows,me,he);
ImageIO.write(read, "png", new File(FileUtil.Desktop + "target.png"));
}public static BufferedImage makeChat(BufferedImage me, BufferedImage he, String background, String[] rows)
throws IOException {
//开始距离25 距离30 日期间隔45 日期50
//最长对话740
BufferedImage bi = ImageIO.read(new File(background));
Graphics2D g2 = (Graphics2D) bi.getGraphics();
// 消除文字锯齿
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 消除画图锯齿
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int cury=25;
for (int i = 0; i < rows.length; i++) {
String row=rows[i];
if(row.startsWith("me`")){
cury=isay(g2,me,row.substring(row.indexOf("`")+1),cury);
}else if(row.startsWith("he`")){
cury=hesay(g2,he,row.substring(row.indexOf("`")+1),cury);
}else if(row.matches("(周.\\s)?\\d+:\\d+")){
if(i>0)cury+=15;
cury=drawDate(g2, row, cury);
}else if("以上是打招呼的内容".equals(row)){
cury=drawDesc(g2, row, cury);
}
}
return bi;
}
public static int drawDesc(Graphics2D g2,String desc,int cury) throws IOException{
Font font=new Font("黑体", Font.PLAIN, 38);
g2.setFont(font);
int len = ImageUtil.getDrawStringLength(g2, desc);
int beginx=(1080-(len+2*font.getSize()/3))/2;
//绘文本框
g2.setColor(Color.white);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));//设置透明度
g2.fillRoundRect(beginx, cury, len+2*font.getSize()/3, 70, 10,10);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));// 透明度设置结束
//绘日期
g2.setColor(Color.black);
ImageUtil.drawString(g2, desc, 0, font, 540-len/2, (int)(cury+((70-font.getSize())/2)+font.getSize()*0.9));
return cury+70+30;
}
public static int drawDate(Graphics2D g2,String time,int cury) throws IOException{
Font font=new Font("黑体", Font.PLAIN, 30);
g2.setFont(font);
int len = ImageUtil.getDrawStringLength(g2, time);
int beginx=(1080-(len+2*font.getSize()/2))/2;
//绘文本框
g2.setColor(Color.white);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));//设置透明度
g2.fillRoundRect(beginx, cury, len+2*font.getSize()/2, 50, 10,10);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));// 透明度设置结束
//绘日期
g2.setColor(Color.black);
ImageUtil.drawString(g2, time, 0, font, 540-len/2, (int)(cury+((50-font.getSize())/2)+font.getSize()*0.9));
return cury+50+45;
}
现在是先把所有的位置全部计算出来
List<Integer> beginys=new ArrayList<Integer>();//本回复最开始
List<Integer> endys=new ArrayList<Integer>();//本回复的结束
List<Integer> kuang_beginxs=new ArrayList<Integer>();//框开始
List<Integer> kuang_heights=new ArrayList<Integer>();//框高度
List<Integer> zi_beginxs=new ArrayList<Integer>();//文字开始
List<Integer> zi_widths=new ArrayList<Integer>();//字最大宽度
List<Integer> zi_fontsizes=new ArrayList<Integer>();//本回复的文字大小
List<List<String>> contents=new ArrayList<List<String>>();//本回复内容