import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import java.net.URLConnection; /** * 图片添加水印工具类 gsn 24-7-29 */ public class ImageUtils { //根据url下载图片 public static File downloadImageAsFile(String imageUrl) throws IOException { URL url = new URL(imageUrl); URLConnection connection = url.openConnection(); // 获取文件名,假设从URL中获取 String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1); // 创建临时文件来保存图片数据 File tempFile = File.createTempFile("temp_image_", fileName); tempFile.deleteOnExit(); // 程序结束时删除临时文件 try (InputStream inputStream = connection.getInputStream(); FileOutputStream outputStream = new FileOutputStream(tempFile)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } return tempFile; } // 添加文字水印的方法 public static byte[] addTextWatermark(MultipartFile file, String watermarkText) throws IOException { // 读取上传的图片文件 BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(file.getBytes())); // 创建一个新的图片,类型为 BufferedImage.TYPE_INT_ARGB,支持透明度 BufferedImage watermarkedImage = new BufferedImage( originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); // 获取画笔 Graphics2D g2d = (Graphics2D) watermarkedImage.getGraphics(); // 将原始图片绘制到新图片中 g2d.drawImage(originalImage, 0, 0, null); // 设置水印文字的样式 g2d.setColor(Color.RED); g2d.setFont(new Font("黑体", Font.BOLD, 30)); // g2d.setFont(new Font("Arial", Font.BOLD, 30)); // 计算水印文字的位置,这里假设在右下角,留出一定的边距 FontMetrics fontMetrics = g2d.getFontMetrics(); int textWidth = fontMetrics.stringWidth(watermarkText); int textHeight = fontMetrics.getHeight(); int x = originalImage.getWidth() - textWidth - 10; // 水印文字距离右边缘的距离 int y = originalImage.getHeight() - textHeight - 10; // 水印文字距离底部的距离 // 绘制水印文字 g2d.drawString(watermarkText, x, y); // 释放资源 g2d.dispose(); // 将处理后的图片转换为字节数组 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(watermarkedImage, "png", outputStream); // 这里可以根据需要调整输出格式,例如 "jpeg" return outputStream.toByteArray(); } public static File addTextWatermark(File inputFile, String watermarkText) throws IOException { // 读取上传的图片文件 BufferedImage originalImage = ImageIO.read(inputFile); // 创建一个新的图片,类型为 BufferedImage.TYPE_INT_ARGB,支持透明度 BufferedImage watermarkedImage = new BufferedImage( originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); // 获取画笔 Graphics2D g2d = (Graphics2D) watermarkedImage.getGraphics(); // 将原始图片绘制到新图片中 g2d.drawImage(originalImage, 0, 0, null); // 设置水印文字的样式 g2d.setColor(Color.RED); g2d.setFont(new Font("黑体", Font.BOLD, 30)); // 计算水印文字的位置,这里假设在右下角,留出一定的边距 FontMetrics fontMetrics = g2d.getFontMetrics(); int textWidth = fontMetrics.stringWidth(watermarkText); int textHeight = fontMetrics.getHeight(); int x = originalImage.getWidth() - textWidth - 10; // 水印文字距离右边缘的距离 int y = originalImage.getHeight() - textHeight - 10; // 水印文字距离底部的距离 // 绘制水印文字 g2d.drawString(watermarkText, x, y); // 释放资源 g2d.dispose(); // 将处理后的图片保存到临时文件 File outputFile = File.createTempFile("watermarked", ".png"); // 可根据需要调整输出格式,例如 ".jpg" ImageIO.write(watermarkedImage, "png", outputFile); return outputFile; } // 判断文件类型是否为图片 public static boolean isImage(MultipartFile file) { try { // 通过 ImageIO 读取文件,如果不是图片会抛出异常 ImageIO.read(new ByteArrayInputStream(file.getBytes())); return true; } catch (IOException e) { return false; } } }