數(shù)據(jù)加載中……
          Java添加水?。▓D片水印,文字水印)
          因?yàn)轫?xiàng)目中考慮到添加圖片版權(quán)的保護(hù),特意看了下水印的處理...以下有兩種方式:

          第一種是添加文字水印:
          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
               *            圖片質(zhì)量
               * 
          @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); // 添加水印的文字和設(shè)置水印文字出現(xiàn)的內(nèi)容
                  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的,因?yàn)樗馁|(zhì)量和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 --
               *            目標(biāo)文件
               * 
          @param x
               *            --x坐標(biāo)
               * 
          @param y
               *            --y坐標(biāo)
               
          */

              
          public final static void pressImage(String pressImg, String targetImg,
                      
          int x, int y) {
                  
          try {
                      
          //目標(biāo)文件
                      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);
                      
          //水印文件結(jié)束
                      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 --
               *            目標(biāo)圖片
               * 
          @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) 評(píng)論(6)  編輯  收藏

          評(píng)論

          # re: Java添加水?。▓D片水印,文字水?。?nbsp;2007-11-30 14:06 專注java開源

          不錯(cuò)~~

          # re: Java添加水?。▓D片水印,文字水?。?nbsp;2007-11-30 14:14 專注java開源

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

          ImageUtils這個(gè)類pressImage方法的x、y參數(shù)沒有用,建議只需要一個(gè)position參數(shù)代替x、y。

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

          值得學(xué)習(xí)

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

          跟.net差不多!

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

          thank you

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 色达县| 诏安县| 南涧| 迁西县| 伊吾县| 盐池县| 延津县| 高尔夫| 上栗县| 东兴市| 勐海县| 九龙城区| 杭锦旗| 汶上县| 忻城县| 台江县| 米泉市| 班戈县| 富蕴县| 隆尧县| 榆树市| 乌鲁木齐市| 静乐县| 阿拉尔市| 六安市| 大石桥市| 右玉县| 灵丘县| 珠海市| 白城市| 林周县| 吉林市| 西充县| 银川市| 松江区| 桂阳县| 吉木乃县| 福清市| 池州市| 电白县| 韩城市|