src/org/nutz/img/Images.java
public static BufferedImage addWatermark(Object srcIm,
                                             Object markIm,
                                             float opacity,
                                             int pos,
                                             int margin) {
        BufferedImage im1 = read(srcIm);
        BufferedImage im2 = read(markIm);

        int cw = im1.getWidth();
        int ch = im1.getHeight();
        int mw = im2.getWidth();
        int mh = im2.getHeight();

        if (opacity > 1 || opacity <= 0) {
            opacity = 0.5f;
        }
        if (pos > 9 || pos <= 0) {
            pos = 5;
        }

        // 计算水印位置
        int px = 0;
        int py = 0;
        switch (pos) {
        case WATERMARK_TOP_LEFT:
            px = margin;
            py = margin;
            break;
        case WATERMARK_TOP_CENTER:
            px = (cw - mw) / 2;
            py = margin;
            break;
        case WATERMARK_TOP_RIGHT:
            px = cw - mw - margin;
            py = margin;
            break;
        case WATERMARK_CENTER_LEFT:
            px = margin;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_CENTER:
            px = (cw - mw) / 2;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_CENTER_RIGHT:
            px = cw - mw - margin;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_BOTTOM_LEFT:
            px = margin;
            py = ch - mh - margin;
            break;
        case WATERMARK_BOTTOM_CENTER:
            px = (cw - mw) / 2;
            py = ch - mh - margin;
            break;
        case WATERMARK_BOTTOM_RIGHT:
            px = cw - mw - margin;
            py = ch - mh - margin;
            break;
        }

        // 添加水印
        Graphics2D gs = im1.createGraphics();
        gs.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, opacity));
        gs.drawImage(im2, px, py, null);
        gs.dispose();

        return im1;
    }
public static BufferedImage addWatermark(Object srcIm,
                                             Object markIm,
                                             float opacity,
                                             int pos,
                                             int margin) {
        BufferedImage im1 = read(srcIm);
        BufferedImage im2 = read(markIm);

        int cw = im1.getWidth();
        int ch = im1.getHeight();
        int mw = im2.getWidth();
        int mh = im2.getHeight();

        if (opacity > 1 || opacity <= 0) {
            opacity = 0.5f;
        }
        if (pos > 9 || pos <= 0) {
            pos = 5;
        }

        // 计算水印位置
        int px = 0;
        int py = 0;
        switch (pos) {
        case WATERMARK_TOP_LEFT:
            px = margin;
            py = margin;
            break;
        case WATERMARK_TOP_CENTER:
            px = (cw - mw) / 2;
            py = margin;
            break;
        case WATERMARK_TOP_RIGHT:
            px = cw - mw - margin;
            py = margin;
            break;
        case WATERMARK_CENTER_LEFT:
            px = margin;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_CENTER:
            px = (cw - mw) / 2;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_CENTER_RIGHT:
            px = cw - mw - margin;
            py = (ch - mh) / 2;
            break;
        case WATERMARK_BOTTOM_LEFT:
            px = margin;
            py = ch - mh - margin;
            break;
        case WATERMARK_BOTTOM_CENTER:
            px = (cw - mw) / 2;
            py = ch - mh - margin;
            break;
        case WATERMARK_BOTTOM_RIGHT:
            px = cw - mw - margin;
            py = ch - mh - margin;
            break;
        }

        // 添加水印
        Graphics2D gs = im1.createGraphics();
        gs.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, opacity));
        gs.drawImage(im2, px, py, null);
        gs.dispose();

        return im1;
    }
public static BufferedImage addWatermark ( Object srcIm , Object markIm , float opacity , int pos , int margin ) { BufferedImage im1 = read ( srcIm ) ; BufferedImage im2 = read ( markIm ) ; int cw = im1 . getWidth ( ) ; int ch = im1 . getHeight ( ) ; int mw = im2 . getWidth ( ) ; int mh = im2 . getHeight ( ) ; if ( opacity > 1 || opacity <= 0 ) { opacity = 0.5f ; } if ( pos > 9 || pos <= 0 ) { pos = 5 ; } // 计算水印位置 int px = 0 ; int py = 0 ; switch ( pos ) { case WATERMARK_TOP_LEFT : px = margin ; py = margin ; break ; case WATERMARK_TOP_CENTER : px = ( cw - mw ) / 2 ; py = margin ; break ; case WATERMARK_TOP_RIGHT : px = cw - mw - margin ; py = margin ; break ; case WATERMARK_CENTER_LEFT : px = margin ; py = ( ch - mh ) / 2 ; break ; case WATERMARK_CENTER : px = ( cw - mw ) / 2 ; py = ( ch - mh ) / 2 ; break ; case WATERMARK_CENTER_RIGHT : px = cw - mw - margin ; py = ( ch - mh ) / 2 ; break ; case WATERMARK_BOTTOM_LEFT : px = margin ; py = ch - mh - margin ; break ; case WATERMARK_BOTTOM_CENTER : px = ( cw - mw ) / 2 ; py = ch - mh - margin ; break ; case WATERMARK_BOTTOM_RIGHT : px = cw - mw - margin ; py = ch - mh - margin ; break ; } // 添加水印 Graphics2D gs = im1 . createGraphics ( ) ; gs . setComposite ( AlphaComposite . getInstance ( AlphaComposite . SRC_ATOP , opacity ) ) ; gs . drawImage ( im2 , px , py , null ) ; gs . dispose ( ) ; return im1 ; }
为图片添加水印，可以设定透明度与水印的位置
<p>
水印位置默认支持9种，分别是：

TOP_LEFT | TOP_CENTER | TOP_RIGHT CENTER_LEFT | CENTER | CENTER_RIGHT
BOTTOM_LEFT | BOTTOM_CENTER | BOTTOM_RIGHT


@param srcIm
源图片
@param markIm
水印图片
@param opacity
透明度, 要求大于0小于1, 默认为0.5f
@param pos
共9个位置，请使用 Images.WATERMARK_{XXX} 进行设置，默认为
Images.WATERMARK_CENTER
@param margin
水印距离四周的边距 默认为0
@return
为图片添加水印，可以设定透明度与水印的位置 <p > 水印位置默认支持9种，分别是：