用JAVA實現(xiàn)數(shù)字水?。梢姡?/h1>
數(shù)字水印有可見不可見之分,可見的比如課件上印有學校?;眨⒉┌l(fā)圖片會水印上上傳者的信息及微博logo等。
用java實現(xiàn)可見的數(shù)字水印,草人主要是用到了java.awt包中的AlphaComposite類,當然在實現(xiàn)之前先介紹一下AlphaComposite類:
AlphaComposite類是關(guān)于兩個目標重疊的混合處理類,此類實現(xiàn)的特定規(guī)則是 T. Porter 和 T. Duff 合著的 “Compositing Digital Images”, SIGGRAPH 84, 253-259 中描述的 12 條基本規(guī)則集。該類提供的一個getInstance的方法,其中的兩個參數(shù)為rule和alpha,第二個參數(shù)將由調(diào)用者設(shè)置一個alpha值,即是透明度的設(shè)置,而第一個參數(shù)則是混合方式。此類擴展了 Porter 和 Duff 定義的方程,包含一個額外的因子。AlphaComposite 類的實例可以包含一個 alpha 值,在將該值用于混合方程之前,可以用它來修改不透明度和每個源像素的覆蓋率。
Porter和 Duff 的論文在混合方程的描述中使用了以下因子:

以規(guī)則SRC_OVER為例,使用這些因子,Porter 和 Duff 定義了 12 種選擇混合因子 Fs 和 Fd 的方法,從而產(chǎn)生了 12 種令人滿意的可視效果。在對 12 個指定可視效果的靜態(tài)字段的描述中,給出了具有確定 Fs 和 Fd 值的方程。SRC_OVER在目標色之上合成源色(Porter-Duff Source Over Destination 規(guī)則)。指定Fs = 1 和 Fd = (1-As),因此:
Ar = As + Ad*(1-As)
Cr = Cs + Cd*(1-As)
該類擴展后一共有24中規(guī)則,定義了9個方法,由于草人的程序中用到了方法getInstance()就對之說明一下——
•詳細定義:public static AlphaComposite getInstance(int rule, float alpha)
•功能:創(chuàng)建一個 AlphaComposite 對象,它具有指定的規(guī)則和用來乘源色 alpha 值的常量 alpha 值。在將源色與目標色合成前,要將源色乘以指定的 alpha 值。
•參數(shù):rule——合成規(guī)則,24種;alpha——將乘源色的 alpha 值的常量 alpha 值。alpha 必須是范圍 [0.0, 1.0] 之內(nèi)(包含邊界值)的一個浮點數(shù)字。
•拋出:IllegalArgumentException - 如果 alpha 小于 0.0 或大于 1.0,或者 rule 是以下規(guī)則之一:CLEAR、SRC、DST、SRC_OVER、DST_OVER、SRC_IN、DST_IN、SRC_OUT、DST_OUT、 SRC_ATOP、DST_ATOP 或 XOR。
更詳細推薦AlphaCompositehttp類文檔:http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/awt/AlphaComposite.html#hashCode()、
Compositing Digital Images 論文:https://en.wikipedia.org/wiki/Alpha_compositing
既然是圖像處理,就先創(chuàng)建一個java2d對象
- Graphics2D g2d=image.createGraphics();
- //用源圖像填充背景
- g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
然后為 Graphics2D 上下文設(shè)置 Composite后就可以將想要寫入的文字或者圖片寫入源圖片上
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
//為 Graphics2D 上下文設(shè)置 Composite。 Composite 用于所有繪制方法中,如 drawImage、
//drawString、draw 和 fill。 它指定新的像素如何在呈現(xiàn)過程中與圖形設(shè)備上的現(xiàn)有像素組合。
g2d.setComposite(ac);
完整代碼(代碼中注釋足夠了,就不多啰嗦)
- package cumt.zry.two;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import javax.imageio.*;
- public class Watermark2{
- public Watermark2(){super();};
- /**
- * 在源圖片上設(shè)置水印文字
- */
- public void WordsToImage(String srcImagePath,float alpha,
- String font,int fontStyle,int fontSize,Color color,
- String inputWords,int x,int y,String imageFormat,String toPath) throws IOException{
- FileOutputStream fos=null;
- try {
- //讀取圖片
- BufferedImage image = ImageIO.read(new File(srcImagePath));
- //創(chuàng)建java2D對象
- Graphics2D g2d=image.createGraphics();
- //用源圖像填充背景
- g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
- //!!!!
- AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
- //為 Graphics2D 上下文設(shè)置 Composite。 Composite 用于所有繪制方法中,如 drawImage、
- //drawString、draw 和 fill。 它指定新的像素如何在呈現(xiàn)過程中與圖形設(shè)備上的現(xiàn)有像素組合。
- g2d.setComposite(ac);
- //設(shè)置文字字體名稱、樣式、大小
- g2d.setFont(new Font(font, fontStyle, fontSize));
- g2d.setColor(color);//設(shè)置字體顏色
- g2d.drawString(inputWords, x, y); //輸入水印文字及其起始x、y坐標
- g2d.dispose();
- //將水印后的圖片寫入toPath路徑中
- fos=new FileOutputStream(toPath);
- ImageIO.write(image, imageFormat, fos);
- }
- //文件操作錯誤拋出
- catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(fos!=null){
- fos.close();
- }
- }
- }
- /**
- * 在源圖像上設(shè)置圖片水印
- */
- public void ImageToImage(String srcImagePath,String appendImagePath,
- float alpha,int x,int y,int width,int height,
- String imageFormat,String toPath) throws IOException{
- FileOutputStream fos = null;
- try {
- //讀圖
- BufferedImage image = ImageIO.read(new File(srcImagePath));
- //創(chuàng)建java2D對象
- Graphics2D g2d=image.createGraphics();
- //用源圖像填充背景
- g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
- //關(guān)鍵地方
- AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
- g2d.setComposite(ac);
- BufferedImage appendImage = ImageIO.read(new File(appendImagePath));
- g2d.drawImage(appendImage, x, y, width, height, null, null);
- g2d.dispose();
- fos=new FileOutputStream(toPath);
- ImageIO.write(image, imageFormat, fos);
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(fos!=null){
- fos.close();
- }
- }
- }
- public static void main(String[] args) throws Exception
- {
- Watermark2 imageObj = new Watermark2();
- //源圖片路徑
- String srcImagePath = "F:/27.jpg";
- //水印圖片路徑
- String appendImagePath = "F:/logo.jpg";
- // ---- 宋體 普通字體 77號字 紅色 透明度0.4"
- float alpha = 0.4F;
- String font = "宋體";
- int fontStyle = Font.PLAIN;
- int fontSize = 77;
- Color color = Color.RED;
- String inputWords = "圖片上設(shè)置水印文字";
- int x = 1700;
- int y = 77;
- String imageFormat = "jpg";
- //水印文字后的存儲路徑
- String wToPath = "F:/31.png";
- //水印圖片后的存儲路徑
- String IToPath = "F:/7.png" ;
- imageObj.WordsToImage(srcImagePath, alpha, font, fontStyle,
- fontSize, color, inputWords, x, y, imageFormat, wToPath);
- imageObj.ImageToImage(srcImagePath, appendImagePath, alpha,
- x, y, 300, 200, imageFormat, IToPath);
- }
- }
實現(xiàn)預(yù)覽:

