網上找了很多資料都沒有產生gif圖片的例子。但是產生浮水印的檔案到不少,如果我們把gif圖片合成浮水印後,圖片就不動了,所以我寫了個小例子供大家參考。(如果你想在某個圖片上列印個動感表徵圖,這個類就能很好地實現,前提是你必須先將動感表徵圖分解成幾張單獨的gif圖片。當然你可以用程式來實現。)
要用到 AnimatedGifEncoder 類自行下載
=========================================
package com.test;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class MakeOver {
public static void main(String[] args) {
MakeOver mo = new MakeOver();
mo.CreateGIF("e:/pic/test.gif","e:/map.gif","e:/pic/",82,395);
//參數列表:輸出圖片地址,加浮水印的圖片地址,動態表徵圖地址,縱座標,橫座標
//注意:此 e:/pic/ 目錄下的檔案如下 1.gif 2.gif 3.gif 。。。。
}
public void CreateGIF(String outputFileName,String path1,String path2,int height,int weidth){
try {
// 指定Frame的檔案
AnimatedGifEncoder e = new AnimatedGifEncoder();
OutputStream os = new FileOutputStream(outputFileName); //輸出圖片
e.start(os);// 開始處理
e.setQuality(15); //設定圖片品質
e.setRepeat(0); //設定迴圈
e.setDelay(500); // 設定延遲時間
MakeOver abc = new MakeOver(); //執行個體化圖片合成類
String path3 = ""; //動態圖片地址
for (int i = 1; i < 3; i++) { //此處只添加 2 張gif圖片
path3 = path2 + i +".gif";
BufferedImage im = abc.pressImage(path3,path1, weidth, height);
e.addFrame(im);// 迴圈加入Frame
}
e.finish();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
public BufferedImage pressImage(String pressImg,
String targetImg, 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, 0, 0, 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, x,
y, wideth_biao, height_biao, null);
// 浮水印檔案結束
g.dispose();
return image;
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return null;
}
}