効果:
注意点:
1.画像に中国語が表示されています。中国語を表示する場合、作者はフォントファイルの読み取り方法を使用します。この点に特に注意する必要があります。ここで使用するフォントファイルはsimsun.ttf(宋体)であり、これをcsdnファイルにアップロードする方法。、コンピュータのC:\ Windows \ Fontsにアクセスして、必要なフォントを見つけることができます。
2、文字换行
ステップ:
1.ホワイトボードの背景画像を生成します
2. Graphics2Dを使用して、テキストとQRコードを背景画像に描画します
3.心臓の画像を生成します
テキストラッピングの参照アドレス:https://www.cnblogs.com/hi-gdl/p/10445566.html
コード:
/**
* 描述:创建空白背景图片
* @param path 图片存储路径
*/
public static String createImage(String path){
//创建一个图片缓冲区 BACKGROUND_WIDTH=600 BACKGROUND_HEIGHT=800
BufferedImage image = new BufferedImage(BACKGROUND_WIDTH, BACKGROUND_HEIGHT, BufferedImage.TYPE_INT_BGR);
//获取图片处理对象
Graphics graphics = image.getGraphics();
//填充背景色
graphics.setColor(Color.WHITE);
graphics.fillRect(1, 1, BACKGROUND_WIDTH - 1, BACKGROUND_HEIGHT - 1);
//设定边框颜色
graphics.setColor(Color.WHITE);
graphics.drawRect(0, 0, BACKGROUND_WIDTH - 1, BACKGROUND_HEIGHT - 1);
//输出文件
File file = new File(path,"background.png");
path = file.getPath();
try {
ImageIO.write(image, "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
//释放资源
graphics.dispose();
return path;
}
public static void main(String[] args) {
String backgroundPath = createImage("D:\\code");
System.out.println("背景图片路径:"+backgroundPath);
String qrCodePath = "D:\\code\\google.png";
String base64 = null;
try {
//设置图片大小
BufferedImage background = ImageIO.read(new File(backgroundPath));
BufferedImage qrCode = ImageIO.read(new File(qrCodePath));
Graphics2D g = background.createGraphics();
g.setColor(Color.BLACK);
//字体
ClassPathResource fontResource = new ClassPathResource("/simsun.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontResource.getInputStream());
font = font.deriveFont(Font.BOLD, 20);
g.setFont(font);
String activityNameString = "活动名称:北上广深北上广深北上广深北上广深北上广深北上广深北上广深北上广深北上广深北上广深";
String activityTypeString = "活动类型:经济特区";
String activityDateString = "活动时间:2020-10-14";
//当活动名称过长的时候就需要进行换行
FontRenderContext frc = g.getFontRenderContext();
g.getFontRenderContext();
Rectangle2D stringBounds = font.getStringBounds(activityNameString, frc);
double fontWidth = stringBounds.getWidth();
List<String> line_list = Lists.newArrayList();
//这个地方的480是你自己要开始画的地方距离背景宽度大小,比如背景图片宽度600,在x为120地方开始画,相隔480
if (fontWidth > 480) {
int textWidth = 480;
double bs = fontWidth / textWidth;//文本长度是文本框长度的倍数
int lineCharCount = (int) Math.ceil(activityNameString.length() / bs);//每行大概字数
int beginIndex = 0;
while (beginIndex < activityNameString.length()) {
int endIndex = beginIndex + lineCharCount;
if (endIndex >= activityNameString.length()) {
endIndex = activityNameString.length();
}
String line_str = activityNameString.substring(beginIndex, endIndex);//截取长度
Rectangle2D tempStringBounds = font.getStringBounds(line_str, frc);
int tzcs = 0;//调整次数
int tzzs = 1;//调整字数,临时文本的字符串长度大于要求的文本宽度时,每次减少临时文本的字数,然后重新测量文本宽度
while (tempStringBounds.getWidth() > textWidth) {
line_str = line_str.substring(0, line_str.length() - tzzs);//每次向前 tzzs 个字符重新计算
tempStringBounds = font.getStringBounds(line_str, frc);
tzcs++;
}
line_list.add(line_str);
beginIndex = beginIndex + line_str.length();
for (int i = 0; i < line_list.size(); i++) {
String line_strs = line_list.get(i);
g.drawString(line_strs, 120, (i + 2) * 35);//35为每行的高度
}
}
} else {
g.drawString(activityNameString, 120, 90);
}
g.drawString(activityTypeString, 120, 150);
g.drawString(activityDateString, 120, 210);
//在背景图片上添加二维码图片
g.drawImage(qrCode, 100, 340, qrCode.getWidth(), qrCode.getHeight(), null);
g.dispose();
ImageIO.write(background, "png", new File("D:\\code", "download.png"));
//转成base64
//输出流
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(background, "png", stream);
//转换为base64
Base64.Encoder encoder1 = Base64.getEncoder();
base64 = "data:image/png;base64,"
+ encoder1.encodeToString(stream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
}