给本地图片添加水印(图片,文字)

    技术2022-07-11  121

    适用于给本地图片添加水印,需要知道图片和水印图片的绝对路径工具类 import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class ImageUtil { public enum positionTypeEnum { BOTTOM_RIGHT("1", "右下角"); private String key; private String value; private positionTypeEnum(String key, String value) { this.key = key; this.value = value; } } /** * 给图片增加文字水印 * */ public static void addText(String imgPath, String outImgPath, String text, Color color, int x, int y) { try { // 读取原图片信息 File imgFile = null; Image img = null; if (imgPath != null) { imgFile = new File(imgPath); } if (imgFile != null && imgFile.exists() && imgFile.isFile() && imgFile.canRead()) { img = ImageIO.read(imgFile); } int imgWidth = img.getWidth(null); int imgHeight = img.getHeight(null); // 加水印 BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); int size = imgWidth / text.length(); Font font = new Font("斜体", Font.HANGING_BASELINE, size); mark(bufImg, img, text, font, color, 0, imgHeight - 50); // 输出图片 FileOutputStream outImgStream = new FileOutputStream(outImgPath); ImageIO.write(bufImg, "jpg", outImgStream); outImgStream.flush(); outImgStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 给图片增加图片水印 * */ public static void addPicture(String inputImg, String markImg, String outputImg, int width, int height, int x, int y) { // 读取原图片信息 File inputImgFile = null; File markImgFile = null; Image img = null; Image mark = null; try { if (inputImg != null && markImg != null) { inputImgFile = new File(inputImg); markImgFile = new File(markImg); } if (inputImgFile != null && inputImgFile.exists() && inputImgFile.isFile() && inputImgFile.canRead()) { img = ImageIO.read(inputImgFile); } if (markImgFile != null && markImgFile.exists() && markImgFile.isFile() && markImgFile.canRead()) { mark = ImageIO.read(markImgFile); } int imgWidth = img.getWidth(null); int imgHeight = img.getHeight(null); BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); mark(bufImg, img, mark, width, height, x, y); // mark(bufImg, img, mark, width, height, x, imgHeight - 50); 保持在图片底部 FileOutputStream outImgStream = new FileOutputStream(outputImg); ImageIO.write(bufImg, "jpg", outImgStream); outImgStream.flush(); outImgStream.close(); } catch (IOException e) { e.printStackTrace(); } } //加文字水印 public static void mark(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) { Graphics2D g = bufImg.createGraphics(); g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null); g.setColor(color); g.setFont(font); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f)); g.drawString(text, x, y); g.dispose(); } //加图片水印 public static void mark(BufferedImage bufImg, Image img, Image markImg, int width, int height, int x, int y) { Graphics2D g = bufImg.createGraphics(); g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null); g.drawImage(markImg, x, y, width, height, null); g.dispose(); } } 测试类 @Test public void test3() throws IOException { String inputImg = "D:\\1.jpg"; String markImg = "D:\\logo.jpg"; String outputImg = "D:\\outputImg.jpg"; ImageUtil.addPicture(inputImg,markImg,outputImg,75,20,60,180); }
    Processed: 0.015, SQL: 9