饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機(jī)開發(fā)網(wǎng)

             :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
          關(guān)鍵字: 企業(yè)應(yīng)用       
               今天客戶說,他想把他上傳的圖片加上個水印的功能,以防止別人盜用他的圖片。他認(rèn)為他的圖片資料很重要。所以。。。
          好,客戶有需求,我們就滿足他,以前我也比較少寫操作圖片的api,所以對圖片加水印的功能也一直沒接觸,不過對于現(xiàn)在網(wǎng)絡(luò)來說。這些根本就不算什么,上網(wǎng)一搜,就找了幾個程序出來,現(xiàn)在我重構(gòu)了下,使它滿足我的要求,現(xiàn)在發(fā)布出來,希望可以給有需要的朋友一點(diǎn)幫助。 
            1 package com.teesoo.util;
            2 
            3 /**
            4  * <b>類名:sdf.java</b> </br> 編寫日期: 2007-5-16 <br/> 
            5  * 程序功能描述: 對圖片進(jìn)行打水印工作,
            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  -- 目標(biāo)文件
           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 -- 目標(biāo)圖片
           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 }




          爬蟲工作室 -- 專業(yè)的手機(jī)軟件開發(fā)工作室
          3G視線 -- 專注手機(jī)軟件開發(fā)
          posted on 2007-05-16 14:47 3G工作室 閱讀(2718) 評論(2)  編輯  收藏 所屬分類: j2ee

          Feedback

          # re: 給上傳圖片加個圖片水印,文字水印的功能 2007-05-17 00:26 vv0885
          你好!我也正在找這個程序,謝謝你的共享。

          我想問一下,我的水印圖片是一張透明的gif圖,為什么用這個程序后變成黑白的了呢?

            回復(fù)  更多評論
            

          # re: 給上傳圖片加個圖片水印,文字水印的功能 2007-05-17 11:27 爬蟲工作室
          不會啊。我的也是用透明的,
          不過你用png看下。
          可能跟gif有關(guān)吧  回復(fù)  更多評論
            

          主站蜘蛛池模板: 常德市| 卢龙县| 谷城县| 绩溪县| 余姚市| 盈江县| 关岭| 崇礼县| 平谷区| 临洮县| 宁陵县| 东山县| 镇沅| 日照市| 都昌县| 洛川县| 玉山县| 乃东县| 鲜城| 开原市| 娄底市| 星座| 方城县| 长武县| 杭锦旗| 阿拉善右旗| 牡丹江市| 兴业县| 剑川县| 温宿县| 禄丰县| 洛隆县| 红河县| 达州市| 梁河县| 惠安县| 昔阳县| 剑河县| 广丰县| 前郭尔| 武胜县|