在采用Java进行网站开发时,为了网站上面添加版权方面的信息,避免被他人非法使用。经常需要在图片上面添加水印,下面我们使用java来给图片添加水印。
实现原理:
这里的水印是一张小图片,我们将水印小图片绘制到目标大图片上面。
代码如下:
package com.bug315; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * 图片处理工具类 * @author huangxin * */ public class ImageTools { /** * 水印的位置 */ public static final int LEFT_TOP = 0x0; public static final int LEFT_CENTER = 0x1; public static final int LEFT_BOTTOM = 0x2; public static final int RIGHT_TOP = 0x3; public static final int RIGHT_CENTER = 0x4; public static final int RIGHT_BOTTOM = 0x5; public static final int TOP_CENTER = 0x6; public static final int BOTTOM_CENTER = 0x7; public static final int CENTER = 0x8; /** * 给图片添加水印 * * @param iconPath 水印图片路径 * @param srcImgPath 源图片路径 * @param targerPath 目标图片路径 * @param position 水印图片路径,见ImageTools的常量 * @param alpha 水印图片的透明度 */ public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, int position, float alpha) { markImageByIcon(iconPath, srcImgPath, targerPath, -1, position, alpha); } /** * 给图片添加水印、可设置水印图片旋转角度 * * @param iconPath 水印图片路径 * @param srcImgPath 源图片路径 * @param targerPath 目标图片路径 * @param degree 水印图片旋转角度 * @param position 水印图片路径,见ImageTools的常量 * @param alpha 水印图片的透明度 */ public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, int degree, int position, float alpha) { OutputStream outputStream = null; try { Image srcImgObj = ImageIO.read(new File(srcImgPath)); BufferedImage bufferedSrcImgObj = new BufferedImage(srcImgObj.getWidth(null), srcImgObj.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到画笔对象 Graphics2D graphic = bufferedSrcImgObj.createGraphics(); // 设置对线段的锯齿状边缘处理 graphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR ); graphic.drawImage( srcImgObj.getScaledInstance(srcImgObj.getWidth(null), srcImgObj.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); /** * 设置水印旋转 */ if ( -1 != degree ) { graphic.rotate(Math.toRadians(degree), (double) bufferedSrcImgObj.getWidth() / 2, (double) bufferedSrcImgObj.getHeight() / 2 ); } // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度 ImageIcon iconImageIconObj = new ImageIcon(iconPath); Image iconImageObj = iconImageIconObj.getImage(); /** * 设置水印图片的透明度 */ if ( alpha >= 0 && alpha <= 1) { graphic.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); } /** * 水印图片的大小 */ int iconHeight = iconImageObj.getHeight(null); int iconWidth = iconImageObj.getWidth(null); while ( bufferedSrcImgObj.getWidth(null) < iconWidth ) { iconWidth = (int)(iconWidth * 0.8); iconHeight = (int)(iconHeight * 0.8); } while ( bufferedSrcImgObj.getHeight(null) < iconHeight ) { iconWidth = (int)(iconWidth * 0.8); iconHeight = (int)(iconHeight * 0.8); } /** * 设置水印图片的位置 */ int xPosition = 0, yPosition = 0; switch ( position ) { case LEFT_TOP: xPosition = 0; yPosition = 0; break; case LEFT_CENTER: xPosition = 0; yPosition = (srcImgObj.getHeight(null) - iconHeight) / 2; break; case LEFT_BOTTOM: xPosition = 0; yPosition = (srcImgObj.getHeight(null) - iconHeight); break; case RIGHT_TOP: xPosition = (srcImgObj.getWidth(null) - iconWidth); yPosition = 0; break; case RIGHT_CENTER: xPosition = (srcImgObj.getWidth(null) - iconWidth); yPosition = (srcImgObj.getHeight(null) - iconHeight) / 2; break; case RIGHT_BOTTOM: xPosition = (srcImgObj.getWidth(null) - iconWidth); yPosition = (srcImgObj.getHeight(null) - iconHeight); break; case TOP_CENTER: xPosition = (srcImgObj.getWidth(null) - iconWidth) / 2; yPosition = 0; break; case BOTTOM_CENTER: xPosition = (srcImgObj.getWidth(null) - iconWidth) / 2; yPosition = (srcImgObj.getHeight(null) - iconHeight); break; case CENTER: xPosition = (srcImgObj.getWidth(null) - iconWidth) / 2; yPosition = (srcImgObj.getHeight(null) - iconHeight)/2; break; } graphic.drawImage(iconImageObj, xPosition, yPosition, iconWidth, iconHeight, null); graphic.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); graphic.dispose(); /** * 生成图片 */ outputStream = new FileOutputStream(targerPath); ImageIO.write(bufferedSrcImgObj, "JPG", outputStream); } catch (Exception e) { e.printStackTrace(); } finally { try { if ( null != outputStream ) { outputStream.close(); outputStream = null; } } catch (Exception e) { e.printStackTrace(); } } } // }
客户端代码:
public class Test02 { public static void main(String[] args) { String iconPath = "img/icon2.png"; String srcImgPath = "img/img_12.png"; String distImgPath = "img/dist_img_12.png"; ImageTools.markImageByIcon(iconPath, srcImgPath, distImgPath, ImageTools.CENTER, 1.0F); } }