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

Loading…
Cancel
Save