当前位置: 首页 > news >正文

java合并图片与文字

通过java来绘制海报,加载外部字体并设置样式大小与加粗、设置背景图、合并图片,下面是示例

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;/** java 绘制海报* author xiaochi* date 2024/11/8*/
public class PosterCreatorTest{public static void main(String[] args) throws Exception {// 创建字体// Font font = new Font("微软雅黑", Font.BOLD, 36);// 外部字体String fontPath = "D:\\workspace\\demo3\\src\\main\\resources\\font\\SmileySans2.ttf";Font font = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath));// 合并到图片上的文字String text = "欢迎来到我的世界";int width = 854;// 容器宽度int height = 1280;// 容器高度BufferedImage bgImage = ImageIO.read(new File("D:\\workspace\\demo3\\test1111.png"));BufferedImage combiner = combiner(width, height,null, bgImage, bgImage.getWidth(),bgImage.getHeight());Graphics2D g2d = combiner.createGraphics();// 绘制文本2(不换行)g2d.setColor(getColor("#1bdf1a"));g2d.drawString(text, 100, 100);// 释放图形上下文g2d.dispose();// 绘制文本1(换行)wrapText(combiner,text,font.deriveFont(Font.BOLD,26f),100,50,10,0,null);// 设置文本旋转45°wrapText(combiner,text,font.deriveFont(26f),500,50,combiner.getWidth(),0,null);wrapText(combiner,text,font.deriveFont(26f),500,50,combiner.getWidth(),45,"#ea6f5a");// 合并图片BufferedImage mergeImage = ImageIO.read(new File("D:\\02.png"));merge(combiner,mergeImage,100,100,100,500);try {// 保存图片到文件//ImageIO.write(image, "PNG", new File("D:\\workspace\\demo3\\poster.png"));// 输出文件流ByteArrayOutputStream out = new ByteArrayOutputStream();ImageIO.write(combiner,"png",out);byte[] bytes = out.toByteArray();out.close();FileOutputStream fileOutputStream = new FileOutputStream("D:\\workspace\\demo3\\poster333.png");fileOutputStream.write(bytes);fileOutputStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 创建容器* @param combinerWidth 容器宽度* @param combinerHeight 容器高度* @param bgColor 背景色(默认白色,且背景图会覆盖背景色)* @param bgImage 背景图* @param bgImageWidth 背景图宽度* @param bgImageHeight 背景图高度* @return combiner容器*/public static BufferedImage combiner(int combinerWidth,int combinerHeight,String bgColor,BufferedImage bgImage,int bgImageWidth,int bgImageHeight){BufferedImage combiner = new BufferedImage(combinerWidth,combinerHeight, BufferedImage.TYPE_INT_ARGB);Graphics2D g2d = combiner.createGraphics();// 设置背景颜色g2d.setColor(Color.white);if (null != bgColor && !"".equals(bgColor)){g2d.setColor(getColor(bgColor));}g2d.fillRect(0, 0, combinerWidth, combinerHeight);// 添加背景图片if (null != bgImage){// 添加背景图片g2d.drawImage(bgImage,0,0,bgImageWidth,bgImageHeight,null);}// 释放图形上下文g2d.dispose();return combiner;}/*** 合并图片* @param combiner 容器* @param mergeImage 待合并的图片* @param width  待合并的图片宽度* @param height 待合并的图片高度* @param x 待合并的图片x坐标* @param y 待合并的图片y坐标*/public static void merge(BufferedImage combiner,BufferedImage mergeImage,int width,int height,int x,int y){//BufferedImage image = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);Graphics2D g2d = combiner.createGraphics();g2d.drawImage(mergeImage,x,y,width,height,null);g2d.dispose();}/*** 绘制文字* @param combiner 容器* @param text 文字* @param font 字体(包括大小、样式、颜色)* @param x 文字x坐标* @param y 文字y坐标* @param maxWidth 文字最大宽度(0为竖排显示)* @param rotate 旋转度数* @param color 文字颜色(如:#ffffff)*/public static void wrapText(BufferedImage combiner,String text,Font font,int x,int y,int maxWidth,double rotate,String color){Graphics2D g2d = combiner.createGraphics();//设置字体g2d.setFont(font);// 设置文字颜色if (null != color && !"".equals(color)){g2d.setColor(getColor(color));}// 抗锯齿属性g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //提升观感// 设置旋转AffineTransform at = new AffineTransform();at.rotate(Math.toRadians(rotate),x,y); // 旋转45度,旋转中心为(文字x坐标,100)g2d.setTransform(at);FontMetrics fontMetrics = g2d.getFontMetrics(font);String[] lines = splitText(text, maxWidth, fontMetrics); //实现文字自动换行int lineHeight = g2d.getFontMetrics().getHeight();int ystart = y;for (String line : lines) {g2d.drawString(line, x, ystart);ystart += lineHeight;}g2d.dispose();}/*** 获取颜色* @param color #2395439* @return*/public static Color getColor(String color) {if (color.charAt(0) == '#') {color = color.substring(1);}if (color.length() != 6) {return null;}try {int r = Integer.parseInt(color.substring(0, 2), 16);int g = Integer.parseInt(color.substring(2, 4), 16);int b = Integer.parseInt(color.substring(4), 16);return new Color(r, g, b);} catch (NumberFormatException nfe) {return null;}}/*** 切割文字* @param text* @param maxWidth* @param fontMetrics* @return*/private static String[] splitText(String text, int maxWidth, FontMetrics fontMetrics) {StringBuilder wrappedText = new StringBuilder();String[] words = text.split(""); //以每个字符做拆分,可根据实际需求做更改,下同List<String> lines = new ArrayList<>();for (String word : words) {// 检查添加新单词后是否会超过最大宽度if (wrappedText.length() > 0) {// 检查加上新单词后的总长度if (fontMetrics.stringWidth(wrappedText.toString() + word) > maxWidth) {// 如果超过最大宽度,将当前字符串添加到行列表,并开始新的一行lines.add(wrappedText.toString());wrappedText = new StringBuilder(word);} else {// 如果不超过最大宽度,添加新单词wrappedText.append(word);}} else {wrappedText.append(word);}}// 添加最后一行if (wrappedText.length() > 0) {lines.add(wrappedText.toString());}// 将行列表转换为数组return lines.toArray(new String[0]);}
}

OK了。测试效果图如下:
合成后的效果

http://www.lryc.cn/news/479885.html

相关文章:

  • OpenCV快速入门
  • ArcGIS软件之“计算面积几何”地图制作
  • RHCE 第四次作业
  • 【贪心算法】No.1---贪心算法(1)
  • 分布式光伏管理办法
  • 2024最新软件测试面试热点问题
  • 如何利用探商宝精准营销,抓住行业机遇——以AI技术与大数据推动企业信息精准筛选
  • 嵌入式硬件电子电路设计(三)电源电路之负电源
  • 数据仓库还是数据集市?这俩怎么选?
  • 计算机图形学 实验二 三维模型读取与控制
  • NAT网络工作原理和NAT类型
  • wget命令之Tomcat(三)
  • IP地址修改器 5.0 重制版
  • vscode编译s32ds工程
  • 大数据专业为什么要学习Hadoop课程
  • Xilinx FPGA的Vivado开发流程
  • 音频模型介绍
  • 《编写沪深两市实时交易数据接收程序全攻略》
  • 一文学会easyexcel导入数据,多sheet页、字典转换【附带源码】
  • Spring中的 InitializingBean、BeanPostProcessor、@PostConstruct 等初始化动作的执行时机分析
  • 如何利用指纹浏览器爬虫绕过Cloudflare的防护?
  • idea 基础简单应用(java)
  • windows环境下vscode下载安装
  • Obsidian之与Typora图片格式相互兼容
  • 美半导体巨头正切断中国供应链,给自己“挖坑”?
  • RHCE---搭建lnmp云存储
  • 一些 uniapp相关bug
  • 操作系统-4.2文件系统的层次结构虚拟文件系统
  • 【深度学习】DreamClear:提升图片分辨率的模型
  • 操作系统进程互斥的四种软件实现和三种硬件实现