Python 官方文档:入门教程 => 点击学习
目录Java画图 给图片底部添加文字标题Java 给图片添加文字水印Java画图 给图片底部添加文字标题 需求给图片底部添加文字编号 import java.awt.Color;
需求给图片底部添加文字编号
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;
public class ImageUtil {
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();
}
}
}
原图:
生成后:
水印操作有很多,例如:给图片添加文字、图片水印,给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();
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Java实现画图 给图片底部添加文字标题
本文链接: https://lsjlt.com/news/157331.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0