实现步骤:
读取图像
Image src = ImageIO.read
创建目标图像
BufferedImage distImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB);
//目标图像画笔画图,把原图像画出来
Graphics2D graphics2D = (Graphics2D) distImage.getGraphics();
graphics2D.drawImage(src,0, 0, dstWidth, dstHeight, null);
//添加文字水印
graphics2D.setColor(Color.red);
graphics2D.drawString("V哥教育", 30, 30);
//保存到文件
ImageIO.write(distImage,"png", new File("C:\Users\ZHANGMINGDA\Desktop\日报\订单详情-smal.png"));
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /** * @ClassName ImageIOTest * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/5/8. */ public class ImageIOTest { public static void main(String[] args) throws IOException { //读取图像 Image src = ImageIO.read(new File("C:\Users\ZHANGMINGDA\Desktop\日报\订单详情.png")); //设置目的图像宽高 int dstWidth = 500; int dstHeight = 200; //创建目的图像 BufferedImage distImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB); //目标图像画笔画图,把原图像画出来 Graphics2D graphics2D = (Graphics2D) distImage.getGraphics(); graphics2D.drawImage(src,0, 0, dstWidth, dstHeight, null); //添加文字水印 graphics2D.setColor(Color.red); graphics2D.drawString("V哥教育", 30, 30); //保存到文件 ImageIO.write(distImage,"png", new File("C:\Users\ZHANGMINGDA\Desktop\日报\订单详情-smal.png")); } }