import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.imageio.ImageIO;
/**
* 屏幕截圖
* <一句话功能简述>
* <功能详细描述>
*
* @author wKF
* @version [版本号, 2013-7-17]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class Utils
{
public Utils()
{
}
public static String formatNowDate()
{
SimpleDateFormat ft = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss");
return ft.format(new Date());
}
/**
* <一句话功能简述>
* <功能详细描述>
* @param format format
* @return string
* @see [类、类#方法、类#成员]
*/
public static String formatNowDate(String format)
{
SimpleDateFormat ft = new SimpleDateFormat(format);
return ft.format(new Date());
}
public static String formatDate(Date date)
{
SimpleDateFormat ft = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss");
return ft.format(date);
}
public static String formatDate(Date date, String format)
{
SimpleDateFormat ft = new SimpleDateFormat(format);
return ft.format(date);
}
public static String captureScreenSnapshot()
{
String outFileName = "";
try
{
String str = "E:\\ant 1.8.4";
File f = new File(str);
if (!f.exists())
{
f.mkdir();
}
outFileName =
(new StringBuilder(String.valueOf(str))).append(File.separator)
.append("Snapshot_")
.append(formatDate(Calendar.getInstance().getTime(),
"yyyy_MM_dd_HH_mm_ss_SSS"))
.append(".jpg")
.toString();
Toolkit toolkit = Toolkit.getDefaultToolkit();
java.awt.Dimension screenSize = toolkit.getScreenSize();
Rectangle screenRect = new Rectangle(screenSize);
Robot robot = new Robot();
java.awt.image.BufferedImage image =
robot.createScreenCapture(screenRect);
ImageIO.write(image, "jpeg", new File(outFileName));
System.out.println((new StringBuilder(
"\u4FDD\u5B58\u5C4F\u5E55\u622A\u56FE \"")).append(outFileName)
.append("\".")
.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
return outFileName;
}
public static void main(String[] args)
{
captureScreenSnapshot();
}
}