当前位置: 首页>編程日記>正文

JAVA实现GIF添加水印

JAVA实现GIF添加水印

 

gif4j不开源,收费

代码:

package com.xxxx.util;import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;public class GifUtils {private static Logger logger = LoggerFactory.getLogger(GifUtils.class);public static void main(String[] args) throws Exception {long timestart = System.currentTimeMillis();InputStream inputStream1 = new FileInputStream("E:\\Workspace\\image\\gif_flag_test.gif");String dpath="E:\\Workspace\\image\\gif_flag_test_r.gif";//InputStream inputStream1 = new FileInputStream("E:\\Workspace\\image\\gif_flag_test_2.gif");//String dpath="E:\\Workspace\\image\\gif_flag_test_r_2.gif";String iconpath="E:\\Workspace\\tomcat\\webapps\\win_case_hover.png";GifUtils.getGifSticker(dpath,"测试",inputStream1,iconpath,0,155,156);long timeend = System.currentTimeMillis();logger.info("用时"+(timeend-timestart));}public static void getGifSticker(String desPath, InputStream inputStream, String iconPath)throws FontFormatException, IOException{getGifSticker(desPath,"测试",inputStream,iconPath,0,155,156);}public static void getGifSticker(String url,String markContent, InputStream inputStream, String iconPath,Integer degree, int srcWidth, int srcHeight) throws FontFormatException, IOException  {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();try {if(srcWidth < 131 || srcHeight < 18 ) {logger.info("图片尺寸小于水印尺寸,跳过!");return;}GifDecoder decoder = new GifDecoder();//读入gif数据流decoder.read(inputStream);//获取GIF的宽高Dimension dimension = decoder.getFrameSize();int height = (int)dimension.getHeight();//获取字体文件数据流,用于规范生成文字的字体格式//InputStream intput = GifUtils.class.getResourceAsStream(fontName);//生成字体Font font = new Font("宋体",Font.TRUETYPE_FONT,18);//Font.createFont(Font.TRUETYPE_FONT, intput);//要是想使用deriveFont设置字体大小必须重新指定Font,而且不支持整数型,只能使用浮点类型//Font font = font1.deriveFont(25.0f);//读取帧数int frameCount = decoder.getFrameCount();AnimatedGifEncoder encoder = new AnimatedGifEncoder();//String url = "C:\\Users\\TMP\\o\\" + + System.currentTimeMillis()+".gif";encoder.start(url);encoder.setRepeat(0);Graphics2D g = null;ImageIcon imgIcon = new ImageIcon(iconPath);// 得到Image对象。Image img = imgIcon.getImage();float alpha = 1f; // 透明度/*** 对GIF进行拆分* 每一帧进行文字处理* 组装*/for (int i = 0; i < frameCount; i++) {BufferedImage buffImg=decoder.getFrame(i);//g=buffImg.createGraphics();g=(Graphics2D) buffImg.getGraphics();//初始化图像//g =  (Graphics2D) decoder.getFrame(i).getGraphics();/*** RenderingHint是对图片像素,锯齿等等做的优化,可保证生成的图片放大锯齿点阵也不会很明显*///g.setRenderingHint(SunHints.KEY_ANTIALIASING, SunHints.VALUE_ANTIALIAS_ON);//g.setRenderingHint(SunHints.KEY_TEXT_ANTIALIASING, SunHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
//           g.setRenderingHint(SunHints.KEY_STROKE_CONTROL, SunHints.VALUE_STROKE_PURE);
//           g.setRenderingHint(SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST, 100);
//           g.setRenderingHint(SunHints.KEY_FRACTIONALMETRICS, SunHints.VALUE_FRACTIONALMETRICS_OFF);
//           g.setRenderingHint(SunHints.KEY_RENDERING, SunHints.VALUE_RENDER_DEFAULT);//g.setColor(Color.black);//g.setFont(font);// 设置对线段的锯齿状边缘处理g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);g.setComposite(AlphaComposite.Src);g.drawImage(buffImg.getScaledInstance(buffImg.getWidth(null), buffImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);if (null != degree) {// 设置水印旋转g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);}g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
//表示水印图片的位置,叠加图层g.drawImage(img, 12, 10, null);;g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));//设置打印文字和坐标,此处应该和图片二选一if(StringUtils.isNotBlank(markContent)){//水印文字g.setColor(Color.black);g.setFont(font);g.drawString(markContent, 12, 22);}g.dispose();//组装每一帧encoder.addFrame(buffImg);//设置每帧的切换时间if (i != frameCount - 1) {encoder.setDelay(decoder.getDelay(i));}}encoder.finish();byte b[] = outputStream.toByteArray();}finally {try {if (null != outputStream)outputStream.close();} catch (IOException e) {}}}
}

POM引用:

<!--在/lib下 mvn install:install-file -Dfile=animated-gif-lib-1.5-SNAPSHOT.jar -DgroupId=com.madgag  -DartifactId=animated-gif-lib  -Dversion=1.5-SNAPSHOT -Dpackaging=jar -->
<!-- java gif 添加水印功能 library -->
<dependency><groupId>com.madgag</groupId><artifactId>animated-gif-lib</artifactId><version>1.5-SNAPSHOT</version>
</dependency>

引用库:https://github.com/rtyley/animated-gif-lib-for-java

git clone git@github.com:rtyley/animated-gif-lib-for-java.git

mvn clean compile package install

原始文章转自:https://blog.csdn.net/weixin_39396822/article/details/85232921


https://www.fengoutiyan.com/post/15864.html

相关文章:

  • 手机gif动图上加水印
  • js图片加水印
  • gif水印怎么加
  • 添加文字水印
  • 水印怎么做
  • 如何加水印
  • 水印
  • 动态水印
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機