(一)FontUtils 工具类(功能:读取艺术字体文件,转换成 Font 对象)

import org.springframework.util.StringUtils;import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;/*** @author 咸鱼* @date 2018/12/17 21:21*/
public final class FontUtils {private static Map<String, File> fontMap = new HashMap<>();/** 扫描所有的艺术字体包*/static {InputStream resourceAsStream = FontUtils.class.getClassLoader().getResourceAsStream("application.properties");Properties properties = new Properties();try {properties.load(resourceAsStream);String fontFilePath = properties.getProperty("font.path");if (StringUtils.isEmpty(fontFilePath)) {System.out.println("未配置艺术字体文件路径");} else {File file = new File(fontFilePath);File[] files = file.listFiles();if (files != null && files.length > 0){for (File tempFile : files){String fileName = tempFile.getName();String fontName = fileName.substring(0, fileName.indexOf("."));fontMap.put(fontName, tempFile);}}}} catch (IOException e) {e.printStackTrace();} finally {try {resourceAsStream.close();} catch (IOException e) {e.printStackTrace();}}}/***  引入自定义的字体* @param fontName 字体样式* @param fontSize  字体大小* @return 字体对象*/public static Font getFont(String fontName, float fontSize) {Font font = null;File file = fontMap.get(fontName);if (file != null){try(FileInputStream fileInputStream = new FileInputStream(file)) {Font tempFont = Font.createFont(Font.TRUETYPE_FONT, fileInputStream);//当参数为 float 类型,才是设置文字大小font = tempFont.deriveFont(fontSize);} catch (IOException | FontFormatException e) {e.printStackTrace();}}return font;}/*** 将字符串按照自定义的间隔写入* @param str   目标字符串* @param x     写入的位置的x轴* @param y     写入的位置的y轴* @param rate  写入间隔* @param g     画布* @param fontSize  字体的大小*/public static void drawString(String str,int x,int y,int rate, Graphics2D g,int fontSize){String tempStr="";int tempx=x;while (str.length()>0){tempStr=str.substring(0, 1);str=str.substring(1, str.length());//使图形去除锯齿状,可以得到多么细腻的图形g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);g.drawString(tempStr, tempx, y);tempx = tempx + fontSize - rate;}}
}

(二)ImageUtils工具类

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;/*** @author 咸鱼* @date 2018/12/17 21:50*/
public final class ImageUtils {/*** 获取文字字体(艺术字)* @param fontName 字体名* @param fontSize 字体大小* @return 字体*/public static Font getFont(String fontName, int fontSize){return getFont(fontName, null, fontSize);}/*** 获取文字字体* @param fontName 字体名* @param fontStyle 字体样式* @param fontSize 字体大小* @return 字体*/public static Font getFont(String fontName, Integer fontStyle, int fontSize){Font defaultFont = new Font("宋体", Font.BOLD, fontSize);Font font;if (fontStyle != null){//普通字体font = new Font(fontName, fontStyle, fontSize);} else {//艺术字font = FontUtils.getFont(fontName, fontSize);if (font == null){//默认字体font = defaultFont;}}return font;}/*** 在图片上添加文字* @param pressText 水印文字* @param srcImageFile 原图路径* @param desImageFile 目标图路径* @param fontColor 字体颜色* @param font 字体对象* @param x 横坐标* @param y 纵坐标* @param alpha 透明度*/public static void pressText(String pressText, String srcImageFile, String desImageFile,Color fontColor, Font font,int x, int y, float alpha) {try {File img = new File(srcImageFile);Image src = ImageIO.read(img);int width = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = image.createGraphics();g.drawImage(src, 0, 0, width, height, null);g.setColor(fontColor);g.setFont(font);//设置透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));// 在指定坐标(图片居中)绘制水印文字g.drawString(pressText, (width - (getLength(pressText) * font.getSize())) / 2 + x,(height - font.getSize()) / 2 + y);g.dispose();// 输出到文件流ImageIO.write(image, "JPEG", new File(desImageFile));} catch (Exception e) {e.printStackTrace();}}/*** 绘制水印图片* @param pressImgFile 水印图片* @param srcImageFile 原图* @param desImageFile 目标图* @param x 横坐标* @param y 纵坐标* @param alpha 透明度*/public static void pressImage(String pressImgFile, String srcImageFile,String desImageFile,int x, int y, float alpha) {try {File img = new File(srcImageFile);Image src = ImageIO.read(img);int width = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = image.createGraphics();g.drawImage(src, 0, 0, width, height, null);// 水印图片Image imgIcon = ImageIO.read(new File(pressImgFile));int imgIconWidth = imgIcon.getWidth(null);int imgIconHeight = imgIcon.getHeight(null);//设置透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));//居中绘制水印图片g.drawImage(imgIcon, (width - imgIconWidth) / 2 + x,(height - imgIconHeight) / 2 + y, imgIconWidth, imgIconHeight, null);//释放画板g.dispose();ImageIO.write(image,"JPEG", new File(desImageFile));} catch (Exception e) {e.printStackTrace();}}/*** 得到文本长度的一半* @param text 文本* @return 文本总长度的一半*/public static int getLength(String text) {int length = 0;for (int i = 0; i < text.length(); i++) {if ((text.charAt(i) + "").getBytes().length > 1) {//中文2个字节length += 2;} else {//非中文1个字节length += 1;}}return length / 2;}/*** 图像类型转换* @param srcImageFile 原图路径* @param formatName  目标图格式* @param desImageFile 目标图路径*/public static void convert(String srcImageFile, String formatName, String desImageFile) {try {File imageFile = new File(srcImageFile);if (imageFile.canRead() && imageFile.canWrite()) {BufferedImage src = ImageIO.read(imageFile);ImageIO.write(src, formatName, new File(desImageFile));}} catch (Exception e) {e.printStackTrace();}}
}

(三)准备好字体包

(四)配置 艺术字体 文件路径
application.properties

font.path=E:\demo\font

(五)测试

public static void main(String[] args) throws ClassNotFoundException {//普通字体Font systemFont = getFont("宋体", Font.BOLD, 30);pressText("我有一只小毛驴","e:/demo/test.jpg", "e:/demo/student_pressText1.jpg",Color.RED, systemFont,0, 0, 0.5f);//艺术字体Font customFont = getFont("新蒂剪纸体", 30);pressText("我有一只小毛驴","e:/demo/test.jpg", "e:/demo/student_pressText2.jpg",Color.RED, customFont,0, 0, 0.5f);//绘制水印图片pressImage("e:/demo/student.jpg", "e:/demo/test.jpg","e:/demo/abc_pressImage.jpg",0, 0, 0.5f);}

六、添加艺术字到图片相关推荐

  1. html图片滚动红点_HTML中更换或添加网站背景图片的代码怎么写?(示例)

    本篇文章主要介绍了HTML代码中如何更换或添加网站背景图片?对于小白来说,最简单的方法就是,如果是更换背景图片的话. 我们可以在网页上点击鼠标右键查看网站源代码,然后找到css里面的背景图这一段代码, ...

  2. php图片写入带问号_php实现图片上传时添加文字和图片水印技巧

    本文实现的功能特别适用于一些商城和图片站中,分享了图片在上传时添加文字和图片水印的技巧,供大家参考,具体内容如下 1. water.class.php header('Content-Type:tex ...

  3. php给图片加图片水印,php给图片添加文字或图片水印实现代码

    原标题:php给图片添加文字或图片水印实现代码 一.文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件.效果图如下: $dst_path = 'dst ...

  4. java 多行 n_Java实现向Word添加多行图片水印

    码农公社  210.net.cn  210是何含义?10月24日是程序员节,1024 =210.210既 210 之意. Word中设置水印效果时,不论是文本水印或者是图片水印都只能添加单个文字或者图 ...

  5. 自动化办公之excel教程(4):使用艺术字,图片,图形美化工作表

    一.使用艺术字,图片,图形美化工作表 1.插入和设置艺术字 插入文本框 第一步:插入选项卡,文本框 第二步:框选区域 输入文字 第三步:在绘图工具选项卡中,有轮廓填充形状等可以对文本框进行美化 插入艺 ...

  6. opencv学习笔记(六)直方图比较图片相似度

    opencv学习笔记(六)直方图比较图片相似度 opencv提供了API来比较图片的相似程度,使我们很简单的就能对2个图片进行比较,这就是直方图的比较,直方图英文是histogram, 原理就是就是将 ...

  7. java 界面艺术字,Java 在Word文档中添加艺术字

    与普通文字相比,艺术字更加美观有趣也更具有辨识度,常见于一些设计精美的杂志或宣传海报中.我们在日常工作中编辑Word文档时,也可以通过添加艺术字体来凸显文章的重点,美化页面排版.这篇文章将介绍如何使用 ...

  8. c++如何显示图片_Vue+laravel后端添加商品后图片如何显示?

    知识点:利用laravel的php artisan storage:link实现软连接 php artisan storage:link 命令执行完毕后,就会在项目里多出一个 public/stora ...

  9. vb添加GIF动态图片

    众说周知,GIF格式动画文件具有小巧.制作方便等特点,因此在网上得到广泛应用,在vb的picturebox和image控件添加图片后变成静止的了,这给我们设计VB应用程序带来了不便.原来以为实现起来特 ...

最新文章

  1. 被辞后恶意报复,程序员清除125台设备数据,被判21个月监禁
  2. 谭浩强C程序设计第四版答案
  3. word饼图如何画引导线_网络授课如何手写、标注?
  4. 左转待转区----当同向直行信号灯绿灯亮时,左转弯的车辆进入左转待转区等候放行信号(即使此时左转弯灯是红色的) 注意:当直行红灯时候禁止进入...
  5. 【javascript】数据结构-链表
  6. 您会后悔对Lambdas应用重载!
  7. 「PKUWC2018」Slay the Spire
  8. 动态添加表格点击事件
  9. 20世纪50年代开始,数字技术出现,数字计算机开始代替模拟计算机,我们从电气时代逐渐走到了信息时代,电脑重塑了社会的架构与价值。...
  10. spark 算子使用类变量_SparkCore的常用算子
  11. 全减器及其相关概念的理解
  12. C语言中数组和字符串长度以及输入输出详解
  13. 项目是通过文件流的方法查看文件 无法直接在线查看mht后缀文件的解决方案
  14. J - 山峰和山谷 Ridges and Valleys
  15. Android G711编解码
  16. 长芯微LCM1118 16 位模数转换器 P2P替代ADS1118
  17. 数据仓库(DW)、数据湖、数据中台的关系
  18. Opencv中的ROI介绍
  19. hook failed (add --no-verify to bypass)
  20. Python学习记录——Python容器:列表、元组、字典与集合(1)

热门文章

  1. Titanic:数据挖掘入门的第一步
  2. python 爬虫 链家网二手房信息采集代码
  3. 两个开关电源可以并联使用吗开关电源有均流功能,只有开关电源有均流功能的才可以并联使用。没有的切记不可并联使用。电工之家百度快照课复制(可以把网址复制到百度搜索栏,不是http网址搜索栏)
  4. 元数据管理工具Atlas学习笔记之使用
  5. CSS 解决火狐浏览器打印时,背景颜色丢失的问题
  6. 问答推广技巧和注意事项问答平台推广的执行方案
  7. 博客管理系统php教程,Wblog博客程序管理系统
  8. 串联单元集成自清洗过滤器
  9. All in 区块链的百度昨日发布了白皮书,说了些什么?
  10. python实现和‘对象’qq自动续火