图片加水印

ImageMarkUtil.java

package cn.xxxx.cms.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageMarkUtil {
    public static void main(String args[]) throws IOException {
        drawImageMark("D:\\images\\test.jpg","D:\\images\\scene2.jpg","D:\\images\\testMarker2.jpg");

        //drawNew("作者:shine","D:\\images\\test.jpg","D:\\images\\testMarker.jpg",Position.LOWER_RIGHT,Color.green);
    }
    public static void drawMark(String text,String filename,String savepath) throws IOException{
        drawNew(text,filename,savepath,null,null);
    }
    public static void drawNew(String text,String filename,String savepath,Position position) throws IOException{
        drawNew(text,filename,savepath,position,null);
    }


    public static BufferedImage getDrawNew(String text,BufferedImage bufferImg,Position position,Color color) throws IOException {
        Color fontColor=color;
        if(fontColor==null){
            fontColor=Color.black;
        }
        if(position==null){

        }
        Position positionTmp=position;
        if(positionTmp==null){
            positionTmp=Position.LOWER_RIGHT;
        }

        BufferedImage buf = bufferImg;
        int w = buf.getWidth(),h = buf.getHeight();

        BufferedImage newimage = new BufferedImage(w , h,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = newimage.createGraphics();
        g.drawImage(buf, 0, 0, w, h, null);
        // 图片中标识 start
        g.setColor(fontColor);
        //图片位置定位计算并且绘制
        imageWHCountProcess(g,text,w,h,positionTmp);
        // draw end
        g.dispose();
        return newimage;
    }

    /***
     * 在给定图片上面绘制文本水印
     * @param text
     * @param filename
     * @param savepath
     * @param position 水印位置
     * @param color
     * @throws IOException
     */
    public static void drawNew(String text,String filename,String savepath,Position position,Color color) throws IOException {
        Color fontColor=color;
        if(fontColor==null){
            fontColor=Color.black;
        }
        if(position==null){

        }
        Position positionTmp=position;
        if(positionTmp==null){
            positionTmp=Position.LOWER_RIGHT;
        }

        BufferedImage buf = ImageIO.read(new File(filename));
        int w = buf.getWidth(),h = buf.getHeight();

        BufferedImage newimage = new BufferedImage(w , h,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = newimage.createGraphics();
        g.drawImage(buf, 0, 0, w, h, null);
        // 图片中标识 start
        g.setColor(fontColor);
        //图片位置定位计算并且绘制
        imageWHCountProcess(g,text,w,h,positionTmp);
        // draw end
        g.dispose();
        ImageIO.write(newimage, "JPEG", new File(savepath));

    }

    /***
     * 图片位置定位计算
     * @param g
     * @param text
     * @param w
     * @param h
     * @param positionTmp
     */
    private static void imageWHCountProcess(Graphics2D g,String text,int w,int h,Position positionTmp){
        //LOWER_RIGHT
        switch (positionTmp){
            case TOP:
                int wT1=w-200/2+200;
                g.drawString(text, wT1+200,30);
                break;
            case CENTER:
                int wT2=(w-200)/2;
                int hT2=(h-30)/2;
                g.drawString(text, wT2+200,hT2+30);
                break;
            case BOTTOM:
                int wT3=(w-200)/2;
                g.drawString(text, wT3+200, h-30);
                break;
            case LOWER_RIGHT:
                g.drawString(text, w-200, h-30);
                break;
            default:break;
        }
    }

    /***
     * 把带有水印的图标绘制在图片下方
     * @param filename
     * @param filename2
     * @param savepath
     * @throws IOException
     */
    public static void drawImageMark(String filename,String filename2,String savepath) throws IOException{
        BufferedImage buf = ImageIO.read(new File(filename));
        BufferedImage buf2 = ImageIO.read(new File(filename2));
        //
        int w = buf.getWidth(),h = buf.getHeight();
        BufferedImage newimage = new BufferedImage(w , h,
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = newimage.createGraphics();
        // draw start
        g.drawImage(buf, 0, 0, w, h, null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1f));
        g.drawImage(buf2, w-buf2.getWidth(), h-buf2.getHeight(), buf2.getWidth(), buf2.getHeight(), null);
        g.dispose();
        ImageIO.write(newimage, "JPEG", new File(savepath));
    }


   /** 1. 定义枚举类型*/
   public enum Position {
       // 利用构造函数传参
       TOP(0),CENTER (1), BOTTOM (2), LOWER_RIGHT(3);

       /**定义私有变量*/
       private int nCode ;

        /** 构造函数,枚举类型只能为私有*/
       private Position( int _nCode) {
           this . nCode = _nCode;
       }

       @Override
       public String toString() {
           return String.valueOf (this.nCode );
       }
   }
}

使用

                                    BufferedImage _img = ImageIO.read(file);
                                    if(null!=_img) {

                                        BufferedImage bufferImg= new BufferedImage(_img.getWidth(),_img.getHeight(),BufferedImage.TYPE_INT_RGB);
                                        bufferImg.getGraphics().drawImage(_img, 0, 0, null);

                                        bufferImg = ImageMarkUtil.getDrawNew("水印内容",bufferImg,Position.LOWER_RIGHT,Color.orange);

                                }
评论