java 如何產生動態gif圖片

來源:互聯網
上載者:User

網上找了很多資料都沒有產生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;
 }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.