Java实现画图 给图片底部添加文字标题
作者:趙小傑 时间:2023-03-28 23:23:06
Java画图 给图片底部添加文字标题
需求给图片底部添加文字编号
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* JAVA 画图(生成文字水印)
*
* @author 杰宝宝
*
*/
public class ImageUtil {
/**
* @param str
* 生产的图片文字
* @param oldPath
* 原图片保存路径
* @param newPath
* 新图片保存路径
* @param width
* 定义生成图片宽度
* @param height
* 定义生成图片高度
* @return
* @throws IOException
*/
public void create(String str, String oldPath, String newPath, int width, int height){
try {
File oldFile = new File(oldPath);
Image image = ImageIO.read(oldFile);
File file = new File(newPath);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.drawImage(image, 0, 0, width - 25, height - 25, null); //这里减去25是为了防止字和图重合
/** 设置生成图片的文字样式 * */
Font font = new Font("黑体", Font.BOLD, 25);
g2.setFont(font);
g2.setPaint(Color.BLACK);
/** 设置字体在图片中的位置 在这里是居中* */
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(str, context);
double x = (width - bounds.getWidth()) / 2;
//double y = (height - bounds.getHeight()) / 2; //Y轴居中
double y = (height - bounds.getHeight());
double ascent = -bounds.getY();
double baseY = y + ascent;
/** 防止生成的文字带有锯齿 * */
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
/** 在图片上生成文字 * */
g2.drawString(str, (int) x, (int) baseY);
ImageIO.write(bi, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
ImageUtil img = new ImageUtil();
img.create("编号:0011", "E:\\111.png", "E:\\222.png", 455, 455);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
原图:
生成后:
Java 给图片添加文字水印
水印操作有很多,例如:给图片添加文字、图片水印,给pdf文件添加水印,给文件加盖公章,这类需求还是时常会遇到的,今天就简单记录一下给图片添加文字水印的demo,仅供大家参考,后续会写别的情况的添加水印的demo,有用到的可以关注一下。
package com.gupaoedu.vip.test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
public class watermark {
/**
* 给图片添加水印
*/
public void addWaterMark() {
Color color = new Color(255, 200, 0, 118); // 水印颜色
Font font = new Font("微软雅黑", Font.ITALIC, 45); //水印字体
String waterMarkContent="我爱你 I LOVE YOU"; //水印内容
String tarImgPath = "C:\\Users\\yun\\Desktop\\新建文件夹\\timg2.jpg"; //存储目标路径
try {
File file = new File("C:\\Users\\yun\\Desktop\\新建文件夹\\timg.jpg"); //原图片
BufferedImage buImage = ImageIO.read(file);
int width = buImage.getWidth(); //图片宽
int height = buImage.getHeight(); //图片高
//添加水印
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(buImage, 0, 0, width, height, null);
g.setColor(color); //水印颜色
g.setFont(font); //水印字体
int x = width -2*getWatermarkLength(waterMarkContent, g); //这是一个计算水印位置的函数,可以根据需求添加
int y = height - 1*getWatermarkLength(waterMarkContent, g);
g.drawString(waterMarkContent, 400, 300); //水印位置
g.dispose(); //释放资源
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufferedImage, "jpg", outImgStream);
System.out.println("添加水印完成");
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
public static void main(String[] args) {
new watermark().addWaterMark();
}
}
来源:https://blog.csdn.net/qq_22906343/article/details/82117831
标签:Java,图片,文字,标题
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android使用个推实现三方应用的推送功能
2022-04-16 08:52:46
![](https://img.aspxhome.com/file/2023/4/138374_0s.png)
Winform窗体圆角设计代码
2022-04-22 18:15:30
![](https://img.aspxhome.com/file/2023/1/126071_0s.jpg)
Java硬币翻转倍数递增试算实例
2021-09-29 08:39:22
Android中创建对话框(确定取消对话框、单选对话框、多选对话框)实例代码
2023-07-04 01:48:25
![](https://img.aspxhome.com/file/2023/4/137494_0s.gif)
详解Java的Spring框架下bean的自动装载方式
2022-10-23 08:13:59
![](https://img.aspxhome.com/file/2023/2/64432_0s.png)
C# 格式化JSON的两种实现方式
2023-03-14 05:31:16
![](https://img.aspxhome.com/file/2023/9/71079_0s.gif)
Java BeanPostProcessor与BeanFactoryPostProcessor基础使用讲解
2022-10-25 21:52:35
详解Java中使用泛型实现快速排序算法的方法
2022-04-28 09:47:00
Java中的static关键字你了解多少
2022-05-21 11:25:51
![](https://img.aspxhome.com/file/2023/9/65609_0s.jpg)
详解Java实现多线程的三种方式
2021-10-30 03:19:16
![](https://img.aspxhome.com/file/2023/9/110799_0s.png)
Eclipse中改变默认的workspace的方法及说明详解
2022-07-31 12:07:21
IDEA中Mybatis的MGB使用逆向工程配置的详细教程
2022-01-04 17:19:50
![](https://img.aspxhome.com/file/2023/3/88583_0s.png)
给c#添加SetTimeout和SetInterval函数
2021-07-02 00:58:08
![](https://img.aspxhome.com/file/2023/7/77237_0s.png)
SpringMVC RESTFul实战案例访问首页
2022-03-12 00:21:01
![](https://img.aspxhome.com/file/2023/8/80918_0s.png)
Android RecyclerView实现滑动删除
2022-11-20 19:50:42
![](https://img.aspxhome.com/file/2023/5/139335_0s.jpg)
Android列表实现(2)_游标列表案例讲解
2022-11-15 16:49:55
Java多线程之synchronized关键字的使用
2023-12-12 21:46:16
基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览实例
2022-06-24 08:07:48
SpringBoot上传图片到指定位置并返回URL的实现
2023-08-13 14:02:52
![](https://img.aspxhome.com/file/2023/4/88064_0s.jpg)
Spring Cloud Gateway去掉url前缀
2023-06-05 00:54:38