饶荣庆 -- 您今天UCWEB了吗?--http://www.ucweb.com

3G 手机开发网

   :: 首页 :: 联系 :: 聚合  :: 管理
  99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
关键字: 企业应用       
     今天客户说,他想把他上传的图片加上个水印的功能,以防止别人盗用他的图片。他认为他的图片资料很重要。所以。。。
好,客户有需求,我们就满足他,以前我也比较少写操作图片的api,所以对图片加水印的功能也一直没接触,不过对于现在网络来说。这些根本就不算什么,上网一搜,就找了几个程序出来,现在我重构了下,使它满足我的要求,现在发布出来,希望可以给有需要的朋友一点帮助。 
  1 package com.teesoo.util;
  2 
  3 /**
  4  * <b>类名:sdf.java</b> </br> 编写日期: 2007-5-16 <br/> 
  5  * 程序功能描述: 对图片进行打水印工作,
  6  * 包括图片水印,文字水印等。
  7  * <br/> Demo: <br/>
  8  * Bug: <br/>
  9  * 
 10  * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>
 11  * 
 12  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
 13  */
 14  
 15 
 16 import java.awt.*;
 17 import java.awt.event.*;
 18 import java.io.*;
 19 import java.awt.image.*;
 20 
 21 import org.w3c.dom.*;
 22 import com.sun.image.codec.jpeg.*;
 23 
 24 import javax.imageio.*;
 25 
 26 public final class ImageUtils {
 27     public ImageUtils() {
 28 
 29     }
 30     
 31     public final static String getPressImgPath(){
 32         return ApplicationContext.getRealPath("/template/data/util/shuiyin.png");
 33     }
 34 
 35  /**
 36   * 把图片印刷到图片上
 37   * @param pressImg -- 水印文件
 38   * @param targetImg  -- 目标文件
 39   * @param x
 40   * @param y
 41   */
 42     public final static void pressImage(String pressImg, String targetImg, int x, int y) {
 43         System.setProperty("java.awt.headless""true");
 44         try {
 45             File _file = new File(targetImg);
 46             Image src = ImageIO.read(_file);
 47             int wideth = src.getWidth(null);
 48             int height = src.getHeight(null);
 49             BufferedImage image = new BufferedImage(wideth, height,
 50                     BufferedImage.TYPE_INT_RGB);
 51             Graphics g = image.createGraphics();
 52             g.drawImage(src, 00, wideth, height, null);
 53 
 54             // 水印文件
 55             File _filebiao = new File(pressImg);
 56             Image src_biao = ImageIO.read(_filebiao);
 57             int wideth_biao = src_biao.getWidth(null);
 58             int height_biao = src_biao.getHeight(null);
 59             g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
 60                     height_biao, null);
 61             // /
 62             g.dispose();
 63             FileOutputStream out = new FileOutputStream(targetImg);
 64             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 65             encoder.encode(image);
 66             out.close();
 67         } catch (Exception e) {
 68             e.printStackTrace();
 69         }
 70     }
 71 
 72     /**
 73      * 打印文字水印图片
 74      * @param pressText --文字
 75      * @param targetImg -- 目标图片
 76      * @param fontName -- 字体名
 77      * @param fontStyle -- 字体样式
 78      * @param color -- 字体颜色
 79      * @param fontSize -- 字体大小
 80      * @param x -- 偏移量
 81      * @param y
 82      */
 83      
 84     public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) {
 85         System.setProperty("java.awt.headless""true");
 86         try {
 87             File _file = new File(targetImg);
 88             Image src = ImageIO.read(_file);
 89             int wideth = src.getWidth(null);
 90             int height = src.getHeight(null);
 91             BufferedImage image = new BufferedImage(wideth, height,
 92                     BufferedImage.TYPE_INT_RGB);
 93             Graphics g = image.createGraphics();
 94             g.drawImage(src, 00, wideth, height, null);
 95             // String s="www.qhd.com.cn";
 96             g.setColor(Color.RED);
 97             g.setFont(new Font(fontName, fontStyle, fontSize));
 98          
 99 
100             g.drawString(pressText, wideth - fontSize - x, height - fontSize/2 - y);
101             g.dispose();
102             FileOutputStream out = new FileOutputStream(targetImg);
103             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
104             encoder.encode(image);
105             out.close();
106         } catch (Exception e) {
107             System.out.println(e);
108         }
109     }
110 
111     public static void main(String[] args) {
112         pressImage("C:/shuiyin/shuiyin.png""c:/shuiyin/DSC02342.JPG"300 ,300);
113     }
114 }




爬虫工作室 -- 专业的手机软件开发工作室
3G视线 -- 专注手机软件开发
posted on 2007-05-16 14:47 3G工作室 阅读(2662) 评论(2)  编辑  收藏 所属分类: j2ee

Feedback

# re: 给上传图片加个图片水印,文字水印的功能 2007-05-17 00:26 vv0885
你好!我也正在找这个程序,谢谢你的共享。

我想问一下,我的水印图片是一张透明的gif图,为什么用这个程序后变成黑白的了呢?

  回复  更多评论
  

# re: 给上传图片加个图片水印,文字水印的功能 2007-05-17 11:27 爬虫工作室
不会啊。我的也是用透明的,
不过你用png看下。
可能跟gif有关吧  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: