java实现多张图片合成gif动态图片
由于工作需要 需要把多张图片合成一个gif动态图片 然后各种面向百度编程 借鉴了一下该作者的文章 原作者[Java基于animated-gif-lib的gif常用处理]
1.使用工具类
public class GifOperator {
public static void main(String[] args) throws IOException {
String dirPath = "D:/demo/";//文件地址
List<BufferedImage> images = new ArrayList<>();
for (int i = 1 ; i < 6;i++) {
File outFile = new File(dirPath + i + ".jpg");
BufferedImage image = ImageIO.read(outFile);
images.add(image);
}
//images代表多张图片 后者代表成功的gif图片
imagesToGif(images,"D:/demo/res_200.gif");
}
/**
* 多图片转gif
* @param imageList
* @param outputPath
* @throws IOException
*/
public static void imagesToGif(List<BufferedImage> imageList, String outputPath) throws IOException {
// 拆分一帧一帧的压缩之后合成
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(outputPath);
encoder.setRepeat(0);
for (BufferedImage bufferedImage :
imageList) {
encoder.setDelay(200);
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
BufferedImage zoomImage = new BufferedImage(width, height, 3);
Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImage.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage(image, 0, 0, null);
encoder.addFrame(zoomImage);
}
encoder.finish();
File outFile = new File(outputPath);
BufferedImage image = ImageIO.read(outFile);
ImageIO.write(image, outFile.getName(), outFile);
}
/**
* Gif转图片集
* @param imagePath
* @param outputDirPath
* @throws IOException
*/
public static void gifToImages(String imagePath,String outputDirPath) throws IOException {
GifDecoder decoder = new GifDecoder();
int status = decoder.read(imagePath);
if (status != GifDecoder.STATUS_OK) {
throw new IOException("read image " + imagePath + " error!");
}
for (int i = 0; i < decoder.getFrameCount();i++) {
BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
File outFile = new File(outputDirPath + i + ".png");
ImageIO.write(bufferedImage, "png", outFile);
}
}
/**
* 视频倒放
* @param imagePath
* @param outputPath
* @throws IOException
*/
public static void reverseGif(String imagePath,String outputPath) throws IOException {
GifDecoder decoder = new GifDecoder();
int status = decoder.read(imagePath);
if (status != GifDecoder.STATUS_OK) {
throw new IOException("read image " + imagePath + " error!");
}
// 拆分一帧一帧的压缩之后合成
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(outputPath);
encoder.setRepeat(decoder.getLoopCount());
for (int i = decoder.getFrameCount() -1; i >= 0; i--) {
encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间
BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());
Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImage.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage(image, 0, 0, null);
encoder.addFrame(zoomImage);
}
encoder.finish();
File outFile = new File(outputPath);
BufferedImage image = ImageIO.read(outFile);
ImageIO.write(image, outFile.getName(), outFile);
}
}
2.导入代码后 缺失pom依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.8</version>
</dependency>
另外 本地idea跑着好好的 部署到服务器出现了一个问题
java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor.postProcessBeanFactory(BusWiringBeanFactoryPostProcessor.java:106)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:181)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at cn.xzcysoft.cy.Application.main(Application.java:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:86)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 20 common frames omitted
然后百度一下 好像是jdk版本兼容问题
解决方案
<!--maven中引入-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>