jpg合成gif

master
luffy9412 7 years ago
parent f71a4609f8
commit 036bde16c2
  1. 6
      core/pom.xml
  2. 30
      core/src/main/java/org/pqh/gif/PixivUtil.java

@ -100,11 +100,7 @@
<version>${jsoup.version}</version>
</dependency>
<dependency>
<groupId>ar.com.hjg</groupId>
<artifactId>pngj</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>core</finalName>

@ -17,6 +17,7 @@ import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -31,9 +32,9 @@ public class PixivUtil implements LogManger{
private static final String baseUrl="https://www.pixiv.net/member_illust.php?mode=medium&illust_id=";
private static final int buffer=10240;
private static String phpsessid;
private static final String imgPath="tmp/images/";
private static final String gifPath=imgPath+"gif/";
/**
* 合成动图
* @param illust_id
@ -45,6 +46,7 @@ public class PixivUtil implements LogManger{
BufferedOutputStream bos = null;
ZipFile zipFile = null;
String imageUrl=baseUrl+illust_id;
String path=null;
try {
log.info("图片链接:"+imageUrl);
Connection.Response response=Jsoup.connect(imageUrl).cookie("PHPSESSID",phpsessid).execute();
@ -74,7 +76,7 @@ public class PixivUtil implements LogManger{
log.info("开始解压");
long a = System.currentTimeMillis();
String path = file.getAbsolutePath().replace(".zip", "");
path = file.getAbsolutePath().replace(".zip", "");
while (entrys.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entrys.nextElement();
is = zipFile.getInputStream(zipEntry);
@ -90,6 +92,7 @@ public class PixivUtil implements LogManger{
bos.write(buf, 0, count);
}
ImageIO.write(ImageIO.read(imgFile),"png",new File(imgPath.replace("jpg","png")));
}
int length = new File(path).listFiles().length;
@ -123,6 +126,7 @@ public class PixivUtil implements LogManger{
if(zipFile != null){
zipFile.close();
}
} catch (IOException e) {
log.error(e);
}
@ -136,7 +140,7 @@ public class PixivUtil implements LogManger{
Connection.Response response = connection.execute();
log.info("图片资源体积:" + response.header("content-length"));
String zipName = src.substring(src.lastIndexOf("/") + 1);
File file = new File("tmp/images/" + zipName);
File file = new File(imgPath + zipName);
FileUtils.writeByteArrayToFile(file, response.bodyAsBytes());
log.info("图片资源下载到:" + file.getAbsolutePath());
return file;
@ -149,16 +153,18 @@ public class PixivUtil implements LogManger{
*/
public static void jpgToGif(String gifName,String jpgPath,JsonNode node){
AnimatedGifEncoder animatedGifEncoder=new AnimatedGifEncoder();
OutputStream outputStream= null;
InputStream inputStream=null;
File file=new File(jpgPath);
File gifFile=new File(jpgPath+"/"+gifName+".gif");
if(gifFile.exists()){
gifFile.delete();
File gifDir=new File(gifPath);
if(!gifDir.exists()){
gifDir.mkdir();
}
File gifFile=new File(gifPath+gifName+".gif");
log.info("动图将生成到"+gifFile.getAbsolutePath());
try {
gifFile.createNewFile();
outputStream = new FileOutputStream(gifFile);
animatedGifEncoder.start(outputStream);
//数组转集合
@ -181,6 +187,7 @@ public class PixivUtil implements LogManger{
animatedGifEncoder.finish();
long b=System.currentTimeMillis();
log.info("合成"+fileList.size()+"帧花费时间"+(b-a)+"ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
@ -193,10 +200,17 @@ public class PixivUtil implements LogManger{
if(inputStream!=null){
inputStream.close();
}
while (!FileUtils.deleteQuietly(file)){
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (gifFile.exists()) {
try {
Desktop.getDesktop().open(gifFile);

Loading…
Cancel
Save