package macaca.client.commands;

import com.alibaba.fastjson.JSONObject;

import macaca.client.common.DriverCommand;
import macaca.client.common.MacacaDriver;
import macaca.client.common.Utils;

public class ScreenShot {

	private MacacaDriver driver;
	private Utils utils = new Utils();

	public ScreenShot(MacacaDriver driver) {
		this.driver = driver;
	}

	public Object takeScreenshot() throws Exception {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("sessionId", driver.getSessionId());
		return utils.request("GET", DriverCommand.SCREENSHOT, jsonObject);
	}
	
	/**
	 * Save screenshot of the current page.
	 * @param filename
	 * @throws Exception
	 */
	public void saveScreenshot(String filename) throws Exception {
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(takeScreenshot().toString());
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成图片
			OutputStream out = new FileOutputStream(filename);
			out.write(b);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -186,16 +186,109 @@
 n.Utils;
+%0Aimport java.io.FileOutputStream;%0Aimport java.io.OutputStream;%0Aimport sun.misc.BASE64Decoder;
 %0A%0Apublic
@@ -716,29 +716,140 @@
 page
-.%0A%09 * @param filename
+ from base64 decoder.%0A%09 * @param fileName The absolute path of the image filename%0A%09 * @return The currently instance of MacacaClient
 %0A%09 *
@@ -1000,17 +1000,22 @@
 %09%09//
+ Decode
  Base64
-%E8%A7%A3%E7%A0%81
 %0A%09%09%09
@@ -1139,17 +1139,8 @@
 0) %7B
-// %E8%B0%83%E6%95%B4%E5%BC%82%E5%B8%B8%E6%95%B0%E6%8D%AE
 %0A%09%09%09
@@ -1175,12 +1175,31 @@
 %09// 
-%E7%94%9F%E6%88%90%E5%9B%BE%E7%89%87
+generate the image file
 %0A%09%09%09

Update ScreenShot.java