jpg合成gif

master
luffy9412 7 years ago
parent 117f7bef7c
commit 9a27171938
  1. 60
      core/src/main/java/org/pqh/gif/PixivUtil.java

@ -15,6 +15,7 @@ import javax.net.ssl.HttpsURLConnection;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
@ -33,19 +34,18 @@ public class PixivUtil implements LogManger{
private static final String imgPath="tmp/images/";
private static final String gifPath=imgPath+"gif/";
/**
* 合成动图
* @param illust_id
*/
public static void downLoadImg(int illust_id,String phpsessid){
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
InputStream is;
FileOutputStream fos;
BufferedOutputStream bos;
ZipFile zipFile = null;
String imageUrl=baseUrl+illust_id;
String path;
File tmpDir = null;
try {
log.info("图片链接:"+imageUrl);
Connection.Response response=Jsoup.connect(imageUrl).cookie("PHPSESSID",phpsessid).execute();
@ -75,13 +75,17 @@ public class PixivUtil implements LogManger{
log.info("开始解压");
long a = System.currentTimeMillis();
path = file.getAbsolutePath().replace(".zip", "");
tmpDir = new File(file.getAbsolutePath().replace(".zip", ""));
File jpgDir=new File(tmpDir.getAbsolutePath() + "/jpg");
File pngDir=new File(tmpDir.getAbsolutePath()+"/png");
jpgDir.mkdirs();
pngDir.mkdirs();
while (entrys.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entrys.nextElement();
is = zipFile.getInputStream(zipEntry);
String imgPath = path + "\\" + zipEntry.getName();
File imgFile = new File(imgPath);
FileUtils.forceMkdirParent(imgFile);
String jpgPath = jpgDir.getAbsolutePath()+"/"+ zipEntry.getName();
File imgFile = new File(jpgPath);
log.info("开始解压图片到:" + imgFile.getAbsolutePath());
byte buf[] = new byte[buffer];
fos = new FileOutputStream(imgFile);
@ -92,14 +96,16 @@ public class PixivUtil implements LogManger{
}
bos.close();
is.close();
ImageIO.write(ImageIO.read(imgFile),"png",new File(imgPath.replace("jpg","png")));
File pngFile=new File(jpgPath.replaceAll("jpg","png"));
ImageIO.write(ImageIO.read(imgFile),"png",pngFile);
}
int length = new File(path).listFiles().length;
int length = jpgDir.listFiles().length;
log.info(length + "张图片解压完毕,耗时:" + (System.currentTimeMillis() - a) + "ms");
if (length > 0) {
jpgToGif(document.title().split("/")[0].replaceAll("[「,」]", ""), path, node.get("frames"));
jpgToGif(document.title().split("/")[0].replaceAll("[「,」]", ""),jpgDir.listFiles());
}
} else {
@ -118,7 +124,7 @@ public class PixivUtil implements LogManger{
if(zipFile != null){
zipFile.close();
}
FileUtils.deleteDirectory(tmpDir);
} catch (IOException e) {
log.error(e);
}
@ -141,38 +147,36 @@ public class PixivUtil implements LogManger{
/**
* jpg合成gif
* @param gifName gif文件名
* @param jpgPath jpg文件目录
* @param
*/
public static void jpgToGif(String gifName,String jpgPath,JsonNode node){
public static void jpgToGif(String gifName,File ...jpgFile){
AnimatedGifEncoder animatedGifEncoder=new AnimatedGifEncoder();
OutputStream outputStream= null;
InputStream inputStream=null;
File file=new File(jpgPath);
File gifDir=new File(gifPath);
if(!gifDir.exists()){
gifDir.mkdir();
}
File gifFile=new File(gifPath+gifName+".gif");
InputStream inputStream;
File gifDir=new File(imgPath+"/gif");
gifDir.mkdirs();
File gifFile=new File(gifDir.getAbsolutePath()+"/"+gifName+".gif");
log.info("动图将生成到"+gifFile.getAbsolutePath());
try {
gifFile.createNewFile();
FileUtils.forceMkdirParent(gifFile);
outputStream = new FileOutputStream(gifFile);
animatedGifEncoder.start(outputStream);
//数组转集合
List<File> fileList= (List<File>) FileUtils.listFiles(file,new String[]{"jpg"},false);
List<File> fileList= Arrays.asList(jpgFile);
long a=System.currentTimeMillis();
for(File f:fileList){
//获取当前帧延迟信息
int index=fileList.indexOf(f);
int delay=node.get(index).get("delay").asInt();
// int delay=node.get(index).get("delay").asInt();
inputStream=new FileInputStream(f.getAbsoluteFile());
BufferedImage image = ImageIO.read(inputStream);
//把帧添加进去合成
animatedGifEncoder.addFrame(image);
animatedGifEncoder.setDelay(delay);
// animatedGifEncoder.setDelay(1);
log.info(gifName+":正在合成第"+((index+1)+"帧"));
inputStream.close();
}
@ -190,7 +194,7 @@ public class PixivUtil implements LogManger{
if(outputStream!=null){
outputStream.close();
}
FileUtils.deleteDirectory(file);
} catch (IOException e) {
e.printStackTrace();

Loading…
Cancel
Save