J2EE社區(qū)

          茍有恒,何必三更起五更眠;
          最無(wú)益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16
          Java添加水印(圖片水印,文字水印)
          因?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);
              }

          }



          名稱: ?4C.ESL | .↗Evon
          口號(hào): 遇到新問(wèn)題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
          mail: 聯(lián)系我


          主站蜘蛛池模板: 永寿县| 府谷县| 张掖市| 元阳县| 乌兰县| 兴宁市| 安远县| 巴彦淖尔市| 富川| 阜新市| 筠连县| 陇南市| 怀远县| 耒阳市| 石屏县| 翁源县| 宾川县| 习水县| 思南县| 田东县| 航空| 志丹县| 凭祥市| 乌拉特后旗| 朔州市| 太谷县| 汕尾市| 彰武县| 剑阁县| 黑山县| 天柱县| 锡林郭勒盟| 酒泉市| 桦甸市| 许昌市| 河池市| 和硕县| 通渭县| 凉山| 历史| 新晃|