1.一行代码教你用java代码写出图片水印
public static void main(String[] args) throws IOException {
//读取网络图片
// URL url = new URL("https://zyq2022.oss-cn-chengdu.aliyuncs.com/test.png");
// BufferedImage image = ImageIO.read(url.openStream());
// 读取图片文件(读取本地图片)
File file = new File("D:\opt\test.png");
System.out.println("图片名称:" + file.getName());
System.out.println("图片大小:" + file.length() / 1024 + " kb");
// 将文件对象转化为图片对象
BufferedImage image = ImageIO.read(file);
System.out.println("图片宽度:" + image.getWidth() + " px");
System.out.println("图片高度:" + image.getHeight() + " px");
// 创建画笔
Graphics2D pen = image.createGraphics();
// 设置画笔颜色为白色
pen.setColor(Color.RED);
// pen.setColor(new Color(179, 250, 233, 200));
// 设置画笔字体样式为微软雅黑,斜体,文字大小为20px
pen.setFont(new Font("微软雅黑", Font.ITALIC, 50));
// 写上水印文字和坐标
pen.drawString("格兰婚纱出版社出版水印", 8, 950);
// 创建新图片文件
File files = new File("D:\opt\test2.png");
// 将处理好的图片数据写入到新图片文件中
FileOutputStream fos = new FileOutputStream(files);
ImageIO.write(image, "png", fos);
}
2.效果展示