使用phantomjs对网页截图

1.官网:http://phantomjs.org/,里面有下载地址,不过速度很慢,常常下载失败。

我是在windows系统做的试验,下载完zip文件解压到任意目录就可以了。

加入环境变量:系统变量Path中加入F:\Program Files\phantomjs-2.1.1-windows\bin,然后cmd模式下输入phantomjs -v看能不能正常输出版本号,正常说明加入成功。

2.截图

2.1js文件

var page = require('webpage').create(), system = require('system'), url, st, domain;

var url = system.args[1];
var st = system.args[2];
var domain = system.args[3];

var c = '{"name":"S_T","value":"'+st+'","domain":"'+domain+'"}';
phantom.addCookie(JSON.parse(c));

page.open(url, function (status) {
    if (status === "success") {
		page.zoomFactor = 1;
		setTimeout(function(){
			console.log(page.renderBase64());
			phantom.exit();}, 1000);
    } else {
		phantom.exit();
	}
    
});
url,st,domain分别是java调用js时传入的参数,

url:访问页面的url

st:cookie中需要设置的登录信息

domain:cookie中需要设置的domain,注意设置cookie的话,domain必须要传,不然cookie设置不成功。

var c = '{"name":"S_T","value":"'+st+'","domain":"'+domain+'"}';
这是把参数拼接成json格式,下面一句是转换成json格式,不这么做的话,cookie设置会不成功。
JSON.parse(c)

page.zoomFactor

这是页面缩放倍数,1:不缩放,0.5就是页面0.5倍

console.log(page.renderBase64());

page.renderBase64()是把页面截图转换成base64格式,用console.log输出到java端进行处理。

2.2java调用js

package com.xxx;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;

import sun.misc.BASE64Decoder;

public class Main {

	public static void main(String[] args) {
		// TODO 1.参数:URL,cookie中的S_T,domain
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("输入URL");
		String url = sc.nextLine().trim();
		System.out.println("输入cookie");
		String cookie = sc.nextLine().trim();
		System.out.println("输入domain");
		String domain = sc.nextLine().trim();
		
		String picture = getSrcContent(url, cookie, domain);
		System.out.println(picture);
		if (picture != null) {
			toImage(picture);
		}
	}

	public static String getSrcContent(String url, String cookie, String domain) {
		// windows下phantomjs位置
		String path = "F:/phantomjs-2.1.1-windows/bin/";
		// 拼接url
		StringBuilder builder = new StringBuilder();
		builder.append(path);
		builder.append("phantomjs.exe");
		builder.append(" ");
		builder.append("F:/screenShot3.js");
		builder.append(" ");
		builder.append(url);
		builder.append(" ");
		builder.append(cookie);
		builder.append(" ");
		builder.append(domain);
		
		Runtime rt = Runtime.getRuntime();
		Process process = null;
		try {
			process = rt.exec(builder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		InputStream is = process.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		StringBuffer sbf = new StringBuffer();
		String tmp = "";
		try {
			while ((tmp = br.readLine()) != null) {
				sbf.append(tmp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return sbf.toString();
	}
	
	public static void toImage(String str) {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(str);
			// 生成jpeg图片
			String imgFilePath = "f://temp.jpg";
			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
		} catch (Exception e) {
		}
	}
}





var c = '{"name":"S_T","value":"'+st+'","domain":"'+domain+'"}';

你可能感兴趣的:(使用phantomjs对网页截图)