当前位置: 代码迷 >> 综合 >> Java phantomjs 网页截图
  详细解决方案

Java phantomjs 网页截图

热度:95   发布时间:2023-09-14 08:59:57.0

简介

根据传入的url对网页进行截图。打开网页和截取图片通过软件phantomjs.exe在后台静默完成。注:该过程不需要打开浏览器
优点:
1.该软件支持多个平台,包括Linux,Windows。
2.截取的界面和浏览器看到的相同,注:浏览器全屏模式下。
缺点:
1.页面打开时间慢
2.无法精确判断页面加载完成
3.为解决以上问题,截取图片的时间需要设置的比较长

phantomjs.exe下载

根据运行环境下载相应的版本https://phantomjs.org/download.html

网页截图工具类

* 网页截图工具** @author archie* @date 2018-11-21*/
public class ScreenShotUtil {private static final Logger LOGGER = LoggerFactory.getLogger(ScreenShotUtil.class);private static ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) SpringContextUtil.getBean("taskExecutor");/*** 截图** @param url 网址* @param filePath 截图保存位置* @param resSubType*/public static void screenWaveShot(String url, String filePath, Integer resSubType) {WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();ServletContext servletContext = webApplicationContext.getServletContext();//软件部署路径String realPath = servletContext.getRealPath("/") + "phantomjs/";String exePath = realPath + "phantomjs.exe  ";String jsPath = realPath + "screenshotSize.js  ";Process process = null;//设置偏移量 长 宽int top = 85;int left = 100;int width = 100;int height = 100;try {String waitTime = ConfigKit.use("config").get(CommonConstant.SHOT_URL_WAVE_TIME);process = Runtime.getRuntime().exec(exePath + jsPath + url + "  " + filePath + "  " + top + "  " +left + "  " + width + "  " + height + "  " + waitTime);//防止程序阻塞taskExecutor.execute(new InputStreamRunnable(process.getErrorStream(), filePath, 1));taskExecutor.execute(new InputStreamRunnable(process.getInputStream(), null, 1));process.waitFor();LOGGER.info(url);} catch (Exception e) {e.printStackTrace();} finally {if (process != null) {process.destroy();}}}
}

输出缓存区的内容 防止阻塞

/*** 读取InputStream的线程** @author * @date 2018-12-03*/
public class InputStreamRunnable implements Runnable {private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamRunnable.class);private BufferedReader bReader = null;private String fileUrl = null;private Integer logOut = null;/*** 输出inputStream的内容** @param is     inputstream 流* @param url    截图的地址* @param logOut 是否输出日志 1:输出 0:不输出*/public InputStreamRunnable(InputStream is, String url, Integer logOut) {try {bReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(is), "GBK"));fileUrl = url;this.logOut = logOut;} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@Overridepublic void run() {StringBuffer line = new StringBuffer();try {String str = "";while ((str = bReader.readLine()) != null) {line.append(str).append(System.lineSeparator());}if (logOut != null && logOut == 1) {if (!StringUtils.isEmpty(line.toString())) {LOGGER.info(line.toString());}}} catch (Exception e) {e.printStackTrace();} finally {try {if (bReader != null) {bReader.close();}} catch (IOException e) {e.printStackTrace();}}}
}

phantomJs 脚本

/*** phantomJs 脚本*/
var page = require('webpage').create(), system = require('system'), address, output, size;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
//page.settings.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
/*page.onConsoleMessage = function (msg, line, source) {console.log(msg);
};*/
if (system.args.length < 3 || system.args.length > 10) {phantom.exit(1);
} else {address = system.args[1];output = system.args[2];//定义宽高page.viewportSize = {width: 1920,height: 1080};page.open(address, function (status) {if (status !== "success") {console.log('FAIL to load the address');phantom.exit(1);}var bb = page.evaluate(function () {return document.getElementsByTagName('html')[0].getBoundingClientRect();});page.clipRect = {top: system.args[3],left: system.args[4],width: system.args[5],height: system.args[6]};window.setTimeout(function () {//console.log(address);page.render(output);page.close();phantom.exit();}, system.args[7]);});
}
  相关解决方案