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> <version>${jsoup.version}</version>
</dependency> </dependency>
<dependency>
<groupId>ar.com.hjg</groupId>
<artifactId>pngj</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>core</finalName> <finalName>core</finalName>

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

Loading…
Cancel
Save