Commit fc8d5933 by songyinghui

feat: 图片合并居中

parent 5a026396
package com.gic.haoban.manage.service.util; package com.gic.haoban.manage.service.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
...@@ -17,40 +20,49 @@ import java.io.InputStream; ...@@ -17,40 +20,49 @@ import java.io.InputStream;
**/ **/
public class DrawImageUtils { public class DrawImageUtils {
public static void drawImage() throws IOException { private static final Logger log = LoggerFactory.getLogger(DrawImageUtils.class);
// ImageIcon imageIcon = new ImageIcon("");
//
// URL url = new URL("http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png");
long startTime = System.currentTimeMillis();
InputStream inputStream = DrawImageUtils.class.getClassLoader().getResourceAsStream("template/base_pic.png");
if (inputStream == null){
return;
}
BufferedImage image = ImageIO.read(inputStream);;
System.out.println("图片宽度:" + image.getWidth() + " px");
System.out.println("图片高度:" + image.getHeight() + " px");
Graphics2D pen = image.createGraphics();
pen.setColor(Color.BLACK);
//pen.setColor(new Color(179, 250, 233, 100));
//Font.PLAIN(正常),Font.BOLD(粗体),Font.ITALIC(斜体)
pen.setFont(new Font("微软雅黑", Font.ITALIC, 20));
pen.drawString("我是图片水印", image.getWidth()/2, image.getHeight()/ 2); /**
pen.dispose(); * 绘画素材月报海报图
* @param fileName
* @param materialNum
* @param amountText
* @throws IOException
*/
public static void drawImageMonth(String fileName, String materialNum, String amountText) {
try {
long startTime = System.currentTimeMillis();
InputStream inputStream = DrawImageUtils.class.getClassLoader().getResourceAsStream("template/month_bkg.png");
if (inputStream == null) {
return;
}
File file = new File("test2.png"); BufferedImage image = ImageIO.read(inputStream);
FileOutputStream fos = new FileOutputStream(file); Graphics2D pen = image.createGraphics();
ImageIO.write(image, "png", fos); pen.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
long endTime = System.currentTimeMillis(); pen.setColor(Color.WHITE);
System.out.println(endTime - startTime); Font font = new Font("微软雅黑", Font.BOLD, 56);
pen.setFont(font);
FontMetrics metrics = pen.getFontMetrics(font);
int startX = 40 + (260 - metrics.stringWidth(materialNum)) / 2;
pen.drawString(materialNum, startX, 250);
int amountX = 320 + (353 - metrics.stringWidth(amountText)) / 2;
pen.drawString(amountText, amountX, 250);
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
ImageIO.write(image, "png", fos);
pen.dispose();
long endTime = System.currentTimeMillis();
log.info("合成图片耗时 {} ms", (endTime - startTime));
}catch (Exception ex){
log.info("合成月报图片异常", ex);
}
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
DrawImageUtils.drawImage();
DrawImageUtils.drawImageMonth("test2.png", "104", "13,433.33");
DrawImageUtils.drawImageMonth("test3.png", "1", "1.43万");
} }
} }
...@@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
...@@ -109,11 +110,12 @@ public class NotityTest { ...@@ -109,11 +110,12 @@ public class NotityTest {
messageDTO.setUserIds(Collections.singletonList("wo59NLDQAA2fEL1xL51lJIy9y2N2aulA")); messageDTO.setUserIds(Collections.singletonList("wo59NLDQAA2fEL1xL51lJIy9y2N2aulA"));
QywxNewsArticleMessageDTO articleInfo = new QywxNewsArticleMessageDTO(); QywxNewsArticleMessageDTO articleInfo = new QywxNewsArticleMessageDTO();
articleInfo.setAppid(config.getAppid()); articleInfo.setAppid(config.getAppid());
articleInfo.setTitle("测试图文标题"); articleInfo.setTitle("素材使用月报");
articleInfo.setDescription("这是一个图文描述"); articleInfo.setDescription("3月1日~3月31日的素材使用月报已生成,请查收");
articleInfo.setPagepath("/monthList"); articleInfo.setPagepath("/monthList");
articleInfo.setPicurl("https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/jhdm/enterprise_common-9292fc686bbf4da2a168d4428363e630.jpeg"); articleInfo.setPicurl("https://platform-1251519181.cos.ap-shanghai.myqcloud.com/image/newdmwltest/enterprise_common-e1e8b3d46fdb4486ad545aae6cba67a1.png");
messageDTO.setArticleMessages(Collections.singletonList(articleInfo));
messageDTO.setArticleMessages(Arrays.asList(articleInfo));
boolean b = qywxSuiteApiService.sendMessage(corpid, config.getWxSuiteid(), messageDTO); boolean b = qywxSuiteApiService.sendMessage(corpid, config.getWxSuiteid(), messageDTO);
System.out.println(b); System.out.println(b);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment