截图小程序

最近的项目中有一个需求开发,情况是这个样子的:将页面上的某一个部分拿下来以后。以图片的形式保存,在网上搜了一下,采取的方式大致分为三种。

下面是最简单的一个小demo

 

public class GuiCamera {
	/**
	 * 默认的文件前缀为GuiCamera,文件格式为PNG格式 The default construct will use the default
	 * Image file surname "GuiCamera", and default image format "png"
	 */
	private final static String FORMAT_PNG = "png";
	private final static String FORMAT_JPG = "jpg";
	private static String filePath = "";// 存放路径
	private static String fileName = "GuiCamera_"; // 文件的前缀
	private static int serialNum = 0;
	private static String imageFormat = GuiCamera.FORMAT_PNG; // 图像文件的格式

	private static Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

	/**
	 * 对屏幕进行拍照 snapShot the Gui once
	 */
	public static void snapShot() throws Exception {
		try {
			GuiCamera.filePath = "D://";
			// 拷贝屏幕到一个BufferedImage对象screenshot
			BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(1087, 71, 135,
					21));
			serialNum++;
			// 根据文件前缀变量和文件格式变量,自动生成文件名
			Calendar c = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");
			String name = filePath + fileName + sdf.format(c.getTime()) + "." + imageFormat;
			File f = new File(name);
			// 将screenshot对象写入图像文件
			ImageIO.write(screenshot, imageFormat, f);
		} catch (Exception e) {
			throw e;
		}
	}

	public static void main(String[] args) throws Exception {
		GuiCamera.filePath = "D://";
		GuiCamera.snapShot();
	}
}


 

转载于:https://www.cnblogs.com/thankyou/p/4352064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值