简介

根据传入的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]);});
}

http://www.taodudu.cc/news/show-4225272.html

相关文章:

  • node--puppeteer网页截屏
  • 用 StarRocks on ES 实现 分词
  • FastDFS V6.06 阿里云集群安装配置双IP(踩坑)
  • DirSync: List of attributes that are synced by the Azure Active Directory Sync Tool
  • FastDFS集群tracker实现负载均衡
  • RHCE 作业一
  • ros使用自动驾驶数据集KITTI【1】介绍与可视化
  • vagrant的同步文件配置,配置虚拟机ip映射
  • fastdfs安装部署整理
  • PXC+haproxy+keepalived环境搭建
  • 缓存分布式锁
  • Vagrant 手册之同步目录 - 基本用法
  • notion自建服务器,【速报】Notion的特色「双向链接」方案来了,Synced Block 登场...
  • Keep your fork synced
  • vagrant Linux虚拟机与win共享目录时的大小写及共享失败问题(vagrant share folder/synced_folder case sensitivity)
  • vagrant 设置除默认工项目之外的synced_folder一个坑
  • Vagrant 入门 - 同步目录(synced folders)
  • Curent branch is not synced with latest origin/master!
  • timed out waiting for to be synced
  • 链乔教育在线|数字化工作管理工具—Notion(四):同步块(Synced block)
  • ElasticSearch 升级过程中给涉及到的术语-1、同步刷新synced  flush
  • caffe源码分析--SyncedMemory 内存管理机制
  • Caffe 理解blob之SyncedMen类同步CPU和GPU
  • 通过java解析域名获得IP地址
  • java解析ip地址_通过JAVA解析IP地址
  • 域名与IP信息解析
  • 曲面显示器和平面显示器玩游戏买哪个好
  • 笔记本外接2K显示器问题记录(集显Intel HD Graphic 3000)
  • ibm服务器显示器接口,显示器接口类型怎么选,4种主流接口要了解
  • 推荐2个适合程序员使用的显示器

Java phantomjs 网页截图相关推荐

  1. java web 截图_如何以Java实现网页截图技术

    今天看到某网友关于"如何以Java实现网页截图技术"的咨询帖,由于出现该咨询的地点非常不适合较长回复,故以博文形式回答. 事实上,如果您想以Java实现网页截图,也就是" ...

  2. 如何以Java实现网页截图技术

    今天看到某网友关于"如何以Java实现网页截图技术"的咨询帖,由于出现该咨询的地点非常不适合较长回复,故以博文形式回答. 事实上,如果您想以Java实现网页截图,也就是" ...

  3. java selenium 网页截图 可控制分辨率

    项目地址如下 https://gitee.com/ogisosetsuna_kong/screenshot screenshot 用SpringBoot以及selenium实现了一个无chromeGu ...

  4. java webengine_如何以Java实现网页截图技术

    展开全部 使用JavaFX8(JDK8打包在一起) public void start(Stage primaryStage) throws Exception { final WebView vie ...

  5. 如何以Java实现网页截图技术,根据URL得到网页快照

    // 此方法仅适用于JdK1.6及以上版本 Desktop.getDesktop().browse( new URL("http://www.csdn.net/").toURI() ...

  6. phantomjs linux 中文不显示,linux 安装phantomJs 用于截图,处理中文乱码,和样式问题...

    1,下载安装phantomJs 下载后解压文件,看情况配置环境变量 tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 2 相关踩坑 2.1 问题演示 中文乱 ...

  7. 使用PhantomJS实现网页截图服务

    使用PhantomJS实现网页截图服务 2015-12-12来源:Java教程人气:99 这是上半年遇到的一个小需求,想实现网页的抓取,并保存为图片.研究了不少工具,效果都不理想,不是显示太差了(Ca ...

  8. 网页截图和svg模版动态生成图片Java实现

    网页截图和svg模版动态生成图片Java实现 使用场景 需要Java语言动态生成图片 用流程图简单说明下我这边工作中使用的场景 仅供参考 所以这里就需要生成证书了 我先给大家看下最终实现的图片效果 这 ...

  9. 利用java调用谷歌浏览器驱动进行网页截图

    最近做项目,需要用到截图的功能,就是给一个url地址,然后自动打开,截取出一个封面图片,保存到服务器上.前端有js可以支持截图,像phantomjs等js插件都可以实现,但是效果不是很好,有些页面渲染 ...

最新文章

  1. IC/FPGA校招笔试题分析(四)再看Moore状态机实现序列检测器
  2. crontab执行不了php的解决方法
  3. IOS中的枚举的写法...
  4. 利用Excel VBA批量计算长时间序列植被物候动态阈值(逐像元)
  5. selenium定位方式
  6. 怎样对生产环境10T RAC新增Adg 不对现有主库产生任何负载,不占用网络带宽?
  7. 数据结构中confusing的地方整理
  8. 如果延迟退休势在必行,区块链如何助力“养老助老”?
  9. Android获取年月日时分秒
  10. 关于移动TD测试用户的800元月话费补贴的解释
  11. 云渲染和渲染农场有什么区别?看完你就明白了
  12. 【ppt入门教程】Win7系统怎样实现ppt双屏显示不同的内容
  13. html制作唐诗三百首,《唐诗三百首》
  14. Volatility内存分析工具 - 某即时通讯软件Windows端数据库密钥的分析
  15. 教你用Qt手写优酷客户端界面
  16. wx朋友圈Hook(一)
  17. 计算机科学与技术部分图书书评
  18. 使用setTimeOut来做倒计时
  19. 【拆机】SG105五口千兆交换机
  20. Flawfinder的安装及使用

热门文章

  1. Ogre开发日记(一)
  2. 前一个标签自动增加,后面的标签自动减小,如微信的群发功能
  3. JMeter源码学习- 5.0版本源码本地构建
  4. 苹果将于4月20日举行产品发布会
  5. 传统大地测量数字考点
  6. 通过websocket抓取斗鱼弹幕和礼物消息
  7. android 视频剪辑工具,微剪辑视频剪辑工具
  8. JAVA服务端的解码
  9. 计算机考试的知识要记忆背,2017计算机等级考试(NCRE)备考小技巧
  10. python turtle画表情包--牛bi