Phantomjs | PhantomJSPhantomJS
http://wenku.kuryun.com/ 官网例子:
http://wenku.kuryun.com/docs/phantomjs/quickstart
window版 phantomjs-2.1.1-windows.zip
linux版 phantomjs-2.1.1-linux-x86_64.tar.bz2
vue 调用和java 后台调用都可以:
/**
* 截取网页图片
*/
@ApiOperation("截取网页图片")
@GetMapping("/screenshot")
public void screenshot() {
try {
// 截屏图片大小可根据实际需求进行设置 size大小 = x+width * y+height
screenshotService.screenshot(null, "http://quote.eastmoney.com/zs000001.html", "1277px*953px");
} catch (IOException e) {
e.printStackTrace();
}
}
import com.realize.common.utils.DateUtils;
import com.realize.project.common.FileResourceUtil;
import com.realize.project.system.service.ScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
@Service
public class ScreenshotServiceImpl implements ScreenshotService {
// 设置截屏后图片存储路径
private String pdfPath = "D:\\realize\\";
@Autowired
private FileResourceUtil fileResourceUtil;
@Override
public String screenshot(HttpServletRequest re, String url, String size) throws IOException {
String img = "";
// 此处可根据操作系统获取对应phantomjs文件路径(phantomjs.exe对应Windows,phantomjs对应Linux)
/* String plugin = '';
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows")) {
plugin = resourceUtil.getFilePath("plugin/phantomjs.exe");
}else{
plugin = resourceUtil.getFilePath("plugin/phantomjs");
}*/
// 测试环境为window环境,故获取phantomjs.exe所在目录
String plugin = fileResourceUtil.getFilePath("plugin/phantomjs.exe");
String js = fileResourceUtil.getFilePath("plugin/rasterize.js");
File file = new File(this.pdfPath);
if (!file.exists()) {
file.mkdirs();
}
String str= DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS,new Date());
// 截屏后文件名可自定义
img = this.pdfPath+str+"原型图.png";
File pluginFile = new File(plugin);
if (!pluginFile.canExecute()) {
pluginFile.setExecutable(true);
}
File jsFile = new File(js);
if (!execute(plugin, jsFile.getAbsolutePath(), url, img,size)) {
return null;
}
//打开刚刚生成的图片,二次加工截取;
BufferedImage bufferedImage = ImageIO.read(new File(img));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.println("图片的宽度为width = x+width :"+width+",高度为height=y+height"+height);
//截图,这里可以根据需要各种裁剪 x ,y ,width,height
BufferedImage subimage = bufferedImage.getSubimage(627, 538, 648, 313);
File file1 = new File(this.pdfPath+str+"裁剪图.png");
//生成一张新的图片
ImageIO.write(subimage,"PNG",file1);
return img;
}
@Override
public String screenshotZoushi(HttpServletRequest re, String url, String size) throws IOException {
String img = "";
// 此处可根据操作系统获取对应phantomjs文件路径(phantomjs.exe对应Windows,phantomjs对应Linux)
/* String plugin = '';
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows")) {
plugin = resourceUtil.getFilePath("plugin/phantomjs.exe");
}else{
plugin = resourceUtil.getFilePath("plugin/phantomjs");
}*/
// 测试环境为window环境,故获取phantomjs.exe所在目录
String plugin = fileResourceUtil.getFilePath("plugin/phantomjs.exe");
String js = fileResourceUtil.getFilePath("plugin/rasterize.js");
File file = new File(this.pdfPath);
if (!file.exists()) {
file.mkdirs();
}
String str= DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS,new Date());
// 截屏后文件名可自定义
img = this.pdfPath+str+"原型图.png";
File pluginFile = new File(plugin);
if (!pluginFile.canExecute()) {
pluginFile.setExecutable(true);
}
File jsFile = new File(js);
if (!execute(plugin, jsFile.getAbsolutePath(), url, img,size)) {
return null;
}
//打开刚刚生成的图片,二次加工截取;
BufferedImage bufferedImage = ImageIO.read(new File(img));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
System.out.println("图片的宽度为"+width+",高度为"+height);
//截图,这里可以根据需要各种裁剪 x ,y ,width,height
BufferedImage subimage = bufferedImage.getSubimage(200, 1000, 600, 400);
File file1 = new File(this.pdfPath+str+"裁剪图.png");
//生成一张新的图片
ImageIO.write(subimage,"PNG",file1);
return img;
}
// 截屏图片流处理
public boolean execute(String... args) {
Process process = null;
StringBuilder msg = new StringBuilder();
try {
process = Runtime.getRuntime().exec(args);
BufferedReader input = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (IOException e) {
System.out.println("error:"+e.getCause());
msg.append("error");
}
return !msg.toString().endsWith("error");
}
}
package com.realize.project.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
@Component
public class FileResourceUtil {
@Autowired
private ResourceLoader resourceLoader;
/**
* 根据文件名字获取路径
* @param fileNameAndPath
* @return
*/
public String getFilePath(String fileNameAndPath) throws IOException {
File file = resourceLoader.getResource("file:"+ fileNameAndPath).getFile();
if(!file.exists()) {
file = resourceLoader.getResource("classpath:"+ fileNameAndPath).getFile();
}
return file.getAbsolutePath();
}
}
得到第一次原始图:
得到第二次裁切图: