jsp的系统要加一个上传图片的功能...
如何才能上传的同时加一个水印?

解决方案 »

  1.   

    在你点击上传提交的时候,调用加水印的方法即可:package commons;/**
     *
     * <p>Title: 给图片添加水印</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2007</p>
     * <p>Company: 福瑞网络有限公司</p>
     * <p>[email protected]</p>
     * @author 杨轶
     * @version 1.0
     */
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.swing.*;
    import com.sun.image.codec.jpeg.*;
    import java.text.AttributedCharacterIterator;
    import java.awt.font.TextAttribute;
    import java.text.AttributedString;
    import java.util.regex.Pattern;public class PicShuiYin {
        /**
         * 给图片添加水印
         * @param filePath 需要添加水印的图片的路径
         * @param Content 水印的文字
         * @param ContentColor 水印文字的颜色
         * @param qualNum 图片质量
         * @return
         */
        public boolean createMark(String filePath, String sy_pic_path,
                                  String Content, Color ContentColor,
                                  String fontStyle, int fontSize, float qualNum) {        //图片
            ImageIcon imgIcon = new ImageIcon(filePath);
            Image theImg = imgIcon.getImage();
            int width = theImg.getWidth(null);
            int height = theImg.getHeight(null);
            BufferedImage bimage = new BufferedImage(width, height,
                                                     BufferedImage.TYPE_INT_RGB);//      水印图片
            ImageIcon imgIcon1 = new ImageIcon(sy_pic_path);
            Image theImg1 = imgIcon1.getImage();
            Font f = new Font(fontStyle, Font.BOLD, fontSize);
            AttributedString ats = new AttributedString(Content);
            ats.addAttribute(TextAttribute.FONT, f, 0, Content.length());
            AttributedCharacterIterator iter = ats.getIterator();        Graphics2D g = bimage.createGraphics();
            g.setColor(ContentColor);
            g.setBackground(Color.white);
            //g.setFont(new Font(Content, fontStyle, fontSize));
            g.drawImage(theImg, 0, 0, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f)); //添加图片水印
            g.drawImage(theImg1, width - 530, height -80, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            //g.drawString(iter, width - 120, height - 10); //添加水印的文字和设置水印文字出现的内容 和位置
            g.dispose();
            try {
                FileOutputStream out = new FileOutputStream(filePath);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(bimage);
                out.close();
            } catch (Exception e) {
                return false;
            }
            return true;
        }    public static void main(String[] args) {
            PicShuiYin wm = new PicShuiYin();
            if (wm.createMark("f://11.jpg", "f://logo.png", "要加的文字", Color.white, "华文彩云",
                              20,100f)) {
                System.out.println("添加水印成功!");
            } else {
                System.out.println("添加水印失败!");
            }
        }
      

  2.   

    //g.setFont(new Font(Content, fontStyle, fontSize));
    这句什么意思啊???
      

  3.   

    楼主很强,但这个 AttributedCharacterIterator iter = ats.getIterator();接口是做什么用的呀?好像没用到呀/