jpg合成gif

master
luffy9412 7 years ago
parent b8b3910a51
commit 16106e927b
  1. 12
      core/pom.xml
  2. 1
      core/src/main/java/org/pqh/core/annotation/ConfigurationSelector.java
  3. 190
      core/src/main/java/org/pqh/gif/PixivUtil.java
  4. 1292
      core/src/main/java/org/pqh/gif/gifmaker/AnimatedGifEncoder.java
  5. 6
      pom.xml
  6. 2
      web/pom.xml

@ -9,7 +9,11 @@
<artifactId>core</artifactId> <artifactId>core</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>core Maven Webapp</name> <name>core Maven Webapp</name>
<url>https://gitee.com/WuXianChaoPin/webcrawler</url> <url>https://gitee.com/WuXianChaoPin/webCrawler</url>
<properties>
<jsoup.version>RELEASE</jsoup.version>
</properties>
<dependencies> <dependencies>
@ -89,6 +93,12 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>core</finalName> <finalName>core</finalName>

@ -7,6 +7,7 @@ import org.springframework.core.type.AnnotationMetadata;
/** /**
* Created by reborn on 2017/9/20. * Created by reborn on 2017/9/20.
* 按条件导入配置类
*/ */
public class ConfigurationSelector implements ImportSelector,LogManger{ public class ConfigurationSelector implements ImportSelector,LogManger{
@Override @Override

@ -0,0 +1,190 @@
package org.pqh.gif;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.pqh.core.util.LogManger;
import org.pqh.gif.gifmaker.AnimatedGifEncoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static org.pqh.gif.PixivUtil.Image.big;
import static org.pqh.gif.PixivUtil.Image.small;
/**
* Created by reborn on 2017/9/24.
*/
public class PixivUtil implements LogManger{
private static final String host="http://i4.pixiv.net";
private static final int buffer=10240;
public static void downLoadImg(String imageUrl){
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ZipFile zipFile = null;
try {
log.info("图片链接:"+imageUrl);
Document document=Jsoup.connect(imageUrl).header("Cookie","PHPSESSID=11866657_32c3f92f0e2bf8b1607dabc04eecf787;").get();
String script=document.select("#wrapper>script").html();
int start=script.indexOf(small.keyword);
int end=script.indexOf(big.keyword);
if(start>-1&&end>-1) {
String json = script.substring(start, end).replaceAll(".*=", "");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode node = objectMapper.readTree(json);
String src=node.get("src").asText().replace(small.size,big.size);
log.info("获取到图包地址:"+src);
Connection connection= Jsoup.connect(src).header("referer",host).ignoreContentType(true);
connection.request().maxBodySize(1024*1024*1024);
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);
FileUtils.writeByteArrayToFile(file,response.bodyAsBytes());
log.info("图包下载到:"+file.getAbsolutePath());
zipFile=new ZipFile(file);
Enumeration entrys=zipFile.entries();
log.info("开始解压");
long a= System.currentTimeMillis();
String path=file.getAbsolutePath().replace(".zip","");
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);
log.info("开始解压图片到:"+imgFile.getAbsolutePath());
byte buf[]=new byte[buffer];
fos=new FileOutputStream(imgFile);
bos=new BufferedOutputStream(fos,buffer);
int count;
while((count=is.read(buf))>-1){
bos.write(buf,0,count);
}
}
int length=new File(path).listFiles().length;
log.info(length+"张图片解压完毕,耗时:"+(System.currentTimeMillis()-a)+"ms");
if(length>0){
jpgToGif(document.title().split("/")[0].replaceAll("[「,」]",""),path,node.get("frames"));
}
}else{
log.error("图包地址获取失败");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bos != null){
bos.close();
}
if(fos != null) {
fos.close();
}
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
} catch (IOException e) {
log.error(e);
}
}
}
/**
* jpg合成gif
* @param gifName gif文件名
* @param jpgPath jpg文件目录
*/
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();
}
log.info("动图将生成到"+gifFile.getAbsolutePath());
try {
outputStream = new FileOutputStream(gifFile);
animatedGifEncoder.start(outputStream);
//数组转集合
List<File> fileList= (List<File>) FileUtils.listFiles(file,new String[]{"jpg"},false);
long a=System.currentTimeMillis();
for(File f:fileList){
//获取当前帧延迟信息
int index=fileList.indexOf(f);
int delay=node.get(index).get("delay").asInt();
inputStream=new FileInputStream(f.getAbsoluteFile());
BufferedImage image = ImageIO.read(inputStream);
//把帧添加进去合成
animatedGifEncoder.addFrame(image);
animatedGifEncoder.setDelay(delay);
log.info(gifName+":正在合成第"+((index+1)+"帧"));
}
//添加完所有帧开始合成
animatedGifEncoder.finish();
long b=System.currentTimeMillis();
log.info("合成"+fileList.size()+"帧花费时间"+(b-a)+"ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(outputStream!=null){
outputStream.close();
}
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (gifFile.exists()) {
try {
Desktop.getDesktop().open(gifFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
enum Image{
small("pixiv.context.ugokuIllustData","600x600"),
big("pixiv.context.ugokuIllustFullscreenData","1920x1080");
private String keyword;
private String size;
Image(String keyword, String size) {
this.keyword = keyword;
this.size = size;
}
}
}

@ -10,7 +10,7 @@
<module>web</module> <module>web</module>
</modules> </modules>
<name>webcrawler Maven Webapp</name> <name>webcrawler Maven Webapp</name>
<url>https://gitee.com/WuXianChaoPin/webcrawler</url> <url>https://gitee.com/WuXianChaoPin/webCrawler</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -26,8 +26,8 @@
<log4j.version>RELEASE</log4j.version> <log4j.version>RELEASE</log4j.version>
<commons-io.version>RELEASE</commons-io.version> <commons-io.version>RELEASE</commons-io.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<servlet-api.version>3.1.0</servlet-api.version> <servlet-api.version>RELEASE</servlet-api.version>
<druid.version>1.1.3</druid.version> <druid.version>RELEASE</druid.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>

@ -9,7 +9,7 @@
<artifactId>web</artifactId> <artifactId>web</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<name>achieve Maven Webapp</name> <name>achieve Maven Webapp</name>
<url>https://gitee.com/WuXianChaoPin/webcrawler</url> <url>https://gitee.com/WuXianChaoPin/webCrawler</url>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.pqh</groupId> <groupId>org.pqh</groupId>

Loading…
Cancel
Save