gif图转jpg有两种方式:
1、使用jimi包,jar包下载地址 http://pan.baidu.com/s/1geTT8t1
2、使用gif4j包,jar包下载地址 http://pan.baidu.com/s/1eRDGC8a
见代码
package utils;
import com.gif4j.GifDecoder;
import com.gif4j.GifFrame;
import com.gif4j.GifImage;
import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* Created by cuisea on 2017/3/3.
*/
public class ImageUtil {
public static void main(String[] args) {
final String source="/Users/cuisea/Downloads/girl.gif";
ExecutorService pool= Executors.newFixedThreadPool(50);
//测试多线程并发
for (int i = 0; i < 1000; i++) {
final int k=i;
final String dest="/Users/cuisea/Downloads/test/girl"+(i+1)+".jpg";
pool.submit(new Runnable() {
@Override
public void run() {
// gif4jToJpg(source,dest);
gifToJpg(source,dest,100);
System.out.println(Thread.currentThread().getName()+" is create jpp "+k);
}
});
}
pool.shutdown();
}
/**
* gif图转jpg图片(使用jimi)
*
* @param source
* gif图片路径
* @param dest
* 目标图片路径
* @param quality
* 图片质量
*/
public static boolean gifToJpg(String source, String dest, int quality) {
if (dest == null || dest.trim().equals(""))
dest = source;
if (!dest.toLowerCase().trim().endsWith("jpg")) {
dest += ".jpg";
}
if (quality < 0 || quality > 100 || (quality + "") == null || (quality + "").equals("")) {
quality = 75;
}
try {
JPGOptions options = new JPGOptions();
options.setQuality(quality);
ImageProducer image = Jimi.getImageProducer(source);
JimiWriter writer = Jimi.createJimiWriter(dest);
writer.setSource(image);
writer.setOptions(options);
writer.putImage(dest);
} catch (JimiException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* gif图转jpg图片(使用gif4j)
*
* @param source
* gif图片路径
* @param dest
* 目标图片路径
*/
public static boolean gif4jToJpg(String source, String dest) {
GifImage image= null;
try {
image = GifDecoder.decode(new File(source));
if (image.getNumberOfFrames()>0){
GifFrame frame=image.getFrame(0);
BufferedImage bufferedImage=frame.getAsBufferedImage();
OutputStream out = new FileOutputStream(dest);
ImageIO.write(bufferedImage, "jpeg", out);//将frame 按jpeg格式 写入out中
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* gif4j获取图片信息 参考http://www.gif4j.com/java-gif4j-pro-gif-image-load-decode.htm#loadgifimageextractmetainfo
* * @param file
* @return
* @throws IOException
*/
public static GifImage loadGifImageAndExtractMetaInfo(File file) throws IOException {
// load and decode gif image from the file
GifImage gifImage = GifDecoder.decode(file);
// print general GIF image info
System.out.println("gif image format version: " + gifImage.getVersion());
System.out.println("gif image logic screen width: " + gifImage.getScreenWidth());
System.out.println("gif image logic screen height: " + gifImage.getScreenHeight());
// check if one or more comments present
if (gifImage.getNumberOfComments() > 0) {
// get iterator over gif image textual comments
Iterator commentsIterator = gifImage.comments();
while (commentsIterator.hasNext())
System.out.println(commentsIterator.next()); // print comments
}
System.out.println("number of frames: " + gifImage.getNumberOfFrames());
// below we iterate frames in loop
// but it can also be done using Iterator instance: gifImage.frames()
for (int i = 0; i < gifImage.getNumberOfFrames(); i++) {
System.out.println("------frame(" + (i + 1) + ")---------");
GifFrame frame = gifImage.getFrame(i);
System.out.println("width: " + frame.getWidth());
System.out.println("height: " + frame.getHeight());
System.out.println("position: " + frame.getX() + "," + frame.getY());
System.out.println("disposal method: " + frame.getDisposalMethod());
System.out.println("delay time: " + frame.getDelay());
System.out.println("is interlaced: " + frame.isInterlaced());
// get frame's color model
IndexColorModel frameColorModel = frame.getColorModel();
System.out.println("number of colors: " + frameColorModel.getMapSize());
System.out.println("is transparent: " + frameColorModel.hasAlpha());
System.out.println("transparent index: " + frameColorModel.getTransparentPixel());
//get frame's representation as an Image
Image image = frame.getAsImage();
//get frame's representation as a BufferedImage
BufferedImage bufferedImage = frame.getAsBufferedImage();
}
return gifImage;
}
}
项目开始采用jimi的方式处理gif图转jpg图片,但是偶尔出现线程阻塞的情况
"TOPST_Quartz_Worker-6" prio=10 tid=0x00007f953d0be800 nid=0x67d8 in Object.wait() [0x00007f9554a0b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at com.sun.jimi.core.JimiImageHandle.waitImageSet(JimiImageHandle.java:67)
- locked <0x000000078791bb98> (a com.sun.jimi.core.JimiImageHandle)
at com.sun.jimi.core.JimiImageHandle.getWrappedJimiImage(JimiImageHandle.java:89)
at com.sun.jimi.core.util.JimiUtil.asJimiRasterImage(JimiUtil.java:676)
at com.sun.jimi.core.JimiReader.getNextJimiImage(JimiReader.java:633)
at com.sun.jimi.core.JimiReader.getNextImageProducer(JimiReader.java:672)
at com.sun.jimi.core.JimiReader.getImageProducer(JimiReader.java:451)
at com.sun.jimi.core.Jimi.getImageProducer(Jimi.java:176)
at com.sun.jimi.core.Jimi.getImageProducer(Jimi.java:162)
at com.topstcn.modules.utils.ImageUtil.gifToJpg(ImageUtil.java:710)
at com.topstcn.modules.utils.ImageUtil.loadImageCommon(ImageUtil.java:596)
at com.topstcn.modules.utils.ImageUtil.loadImages(ImageUtil.java:532)
at com.kyhtech.news.services.SpiderManager.loadSingleImage(SpiderManager.java:1944)
at com.kyhtech.news.services.SpiderManager.loadSingleImage(SpiderManager.java:1933)
at com.kyhtech.news.services.SpiderManager.loadImageCommon(SpiderManager.java:1918)
at com.kyhtech.news.services.spider.UcttSpider.getByUcttContent(UcttSpider.java:95)
at com.kyhtech.news.services.spider.UcttSpider.getByUctt(UcttSpider.java:60)
at com.kyhtech.news.job.UcttSpiderJob.fetchSnews(UcttSpiderJob.java:32)
at com.kyhtech.news.job.BaseSpiderJob.execute(BaseSpiderJob.java:56)
at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
查看源码也没有头绪,多线程测试也没问题。暂时换成gif4j的方式运行一段时间看看。