Java進行時

          程序人生
          數據加載中……
          Java添加水印(圖片水印,文字水印)
          因為項目中考慮到添加圖片版權的保護,特意看了下水印的處理...以下有兩種方式:

          第一種是添加文字水印:
          import java.awt.*;
          import java.awt.image.*;
          import java.io.*;
          import javax.swing.*;
          import com.sun.image.codec.jpeg.*;

          public class WaterSet {
              
          /**
               * 給圖片添加水印
               * 
               * 
          @param filePath
               *            需要添加水印的圖片的路徑
               * 
          @param markContent
               *            水印的文字
               * 
          @param markContentColor
               *            水印文字的顏色
               * 
          @param qualNum
               *            圖片質量
               * 
          @return
               
          */

              
          public boolean createMark(String filePath, String markContent,
                      Color markContentColor, 
          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);
                  Graphics2D g 
          = bimage.createGraphics();
                  g.setColor(markContentColor);
                  g.setBackground(Color.white);
                  g.drawImage(theImg, 
          00null);
                  g.drawString(markContent, width 
          / 5, height / 5); // 添加水印的文字和設置水印文字出現的內容
                  g.dispose();
                  
          try {
                      FileOutputStream out 
          = new FileOutputStream(filePath);
                      JPEGImageEncoder encoder 
          = JPEGCodec.createJPEGEncoder(out);
                      JPEGEncodeParam param 
          = encoder.getDefaultJPEGEncodeParam(bimage);
                      param.setQuality(qualNum, 
          true);
                      encoder.encode(bimage, param);
                      out.close();
                  }
           catch (Exception e) {
                      
          return false;
                  }

                  
          return true;
              }

          }

          第二種是添加圖片水印和文字水印兩種方法,水印圖片可以是GIF,PNG透明的文件,我一般采用的是PNG的,因為它的質量和GIF相比要高一些:

          import java.awt.Color;
          import java.awt.Font;
          import java.awt.Graphics;
          import java.awt.Image;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.FileOutputStream;
          import javax.imageio.ImageIO;
          import com.sun.image.codec.jpeg.JPEGCodec;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;

          public final class ImageUtils {
              
          public ImageUtils() {

              }


              
          /*
               * public final static String getPressImgPath() { return ApplicationContext
               * .getRealPath("/template/data/util/shuiyin.gif"); }
               
          */


              
          /**
               * 把圖片印刷到圖片上
               * 
               * 
          @param pressImg --
               *            水印文件
               * 
          @param targetImg --
               *            目標文件
               * 
          @param x
               *            --x坐標
               * 
          @param y
               *            --y坐標
               
          */

              
          public final static void pressImage(String pressImg, String targetImg,
                      
          int x, int y) {
                  
          try {
                      
          //目標文件
                      File _file = new File(targetImg);
                      Image src 
          = ImageIO.read(_file);
                      
          int wideth = src.getWidth(null);
                      
          int height = src.getHeight(null);
                      BufferedImage image 
          = new BufferedImage(wideth, height,
                              BufferedImage.TYPE_INT_RGB);
                      Graphics g 
          = image.createGraphics();
                      g.drawImage(src, 
          00, wideth, height, null);

                      
          //水印文件
                      File _filebiao = new File(pressImg);
                      Image src_biao 
          = ImageIO.read(_filebiao);
                      
          int wideth_biao = src_biao.getWidth(null);
                      
          int height_biao = src_biao.getHeight(null);
                      g.drawImage(src_biao, (wideth 
          - wideth_biao) / 2,
                              (height 
          - height_biao) / 2, wideth_biao, height_biao, null);
                      
          //水印文件結束
                      g.dispose();
                      FileOutputStream out 
          = new FileOutputStream(targetImg);
                      JPEGImageEncoder encoder 
          = JPEGCodec.createJPEGEncoder(out);
                      encoder.encode(image);
                      out.close();
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          /**
               * 打印文字水印圖片
               * 
               * 
          @param pressText
               *            --文字
               * 
          @param targetImg --
               *            目標圖片
               * 
          @param fontName --
               *            字體名
               * 
          @param fontStyle --
               *            字體樣式
               * 
          @param color --
               *            字體顏色
               * 
          @param fontSize --
               *            字體大小
               * 
          @param x --
               *            偏移量
               * 
          @param y
               
          */


              
          public static void pressText(String pressText, String targetImg,
                      String fontName, 
          int fontStyle, int color, int fontSize, int x,
                      
          int y) {
                  
          try {
                      File _file 
          = new File(targetImg);
                      Image src 
          = ImageIO.read(_file);
                      
          int wideth = src.getWidth(null);
                      
          int height = src.getHeight(null);
                      BufferedImage image 
          = new BufferedImage(wideth, height,
                              BufferedImage.TYPE_INT_RGB);
                      Graphics g 
          = image.createGraphics();
                      g.drawImage(src, 
          00, wideth, height, null);
                      
          // String s="www.qhd.com.cn";
                      g.setColor(Color.RED);
                      g.setFont(
          new Font(fontName, fontStyle, fontSize));

                      g.drawString(pressText, wideth 
          - fontSize - x, height - fontSize
                              
          / 2 - y);
                      g.dispose();
                      FileOutputStream out 
          = new FileOutputStream(targetImg);
                      JPEGImageEncoder encoder 
          = JPEGCodec.createJPEGEncoder(out);
                      encoder.encode(image);
                      out.close();
                  }
           catch (Exception e) {
                      System.out.println(e);
                  }

              }


              
          public static void main(String[] args) {
                  pressImage(
          "F:/logo.png",          "F:/123.jpg"00);
              }

          }

          posted on 2007-11-29 13:15 davma 閱讀(12980) 評論(6)  編輯  收藏

          評論

          # re: Java添加水印(圖片水印,文字水印) 2007-11-30 14:06 專注java開源

          不錯~~
            回復  更多評論    

          # re: Java添加水印(圖片水印,文字水印) 2007-11-30 14:14 專注java開源

            回復  更多評論    

          # re: Java添加水印(圖片水印,文字水印) 2007-11-30 18:02 專注java開源

          ImageUtils這個類pressImage方法的x、y參數沒有用,建議只需要一個position參數代替x、y。
            回復  更多評論    

          # re: Java添加水印(圖片水印,文字水印) 2009-07-10 12:18 hemingwang0902

          值得學習
            回復  更多評論    

          # re: Java添加水印(圖片水印,文字水印) 2009-08-07 11:32 t

          跟.net差不多!
            回復  更多評論    

          # re: Java添加水印(圖片水印,文字水印)[未登錄] 2014-01-07 11:18 

          thank you
            回復  更多評論    

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 中江县| 平泉县| 商南县| 江都市| 五莲县| 炎陵县| 仪陇县| 仁怀市| 宁武县| 合阳县| 伊宁县| 汉寿县| 泰顺县| 陇川县| 汪清县| 嘉鱼县| 肥东县| 毕节市| 南和县| 萝北县| 赣州市| 岑溪市| 秦安县| 图们市| 邵阳县| 博白县| 彭泽县| 阜新市| 闸北区| 红桥区| 西贡区| 和田市| 天等县| 缙云县| 台中市| 嵊州市| 淮阳县| 乌鲁木齐市| 宽城| 汾阳市| 玛曲县|