用Java拍摄指定小程序或程序的屏幕截图?
如何拍摄指定程序窗口或脚本运行的小程序的屏幕截图 我有下面的当前代码,它拍摄了整个计算机屏幕的屏幕截图用Java拍摄指定小程序或程序的屏幕截图?,java,applet,window,screenshot,Java,Applet,Window,Screenshot,如何拍摄指定程序窗口或脚本运行的小程序的屏幕截图 我有下面的当前代码,它拍摄了整个计算机屏幕的屏幕截图 public void robo() { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a"); Calendar now = Calendar.getInstance(); Robot robot = null; try
public void robo() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a");
Calendar now = Calendar.getInstance();
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
}
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
try {
ImageIO.write(screenShot, "JPG",
new File("C:\\..." + formatter.format(now.getTime())
+ ".jpg"));
} catch (IOException e) {
}
System.out.println(formatter.format(now.getTime()));
}
public boolean main() {
//SCC s2i = new SCC();
while (1 == 1) {
robo();
return true;
// sleep(10000);
}
}
如果您使用的是Windows机器,那么平台特定的解决方案可能是使用JNA。例如,我的Use32接口看起来是这样的:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.LONG_PTR;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.win32.StdCallLibrary;
/**
* JNA interface with Window's user32.dll
*/
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(Pointer hWnd, Pointer arg);
}
public static final int GW_OWNER = 4; // used with GetWindow to get win owner
public static final int GW_HWNDNEXT = 2; // used with GetNextWindow
public static final int GA_ROOT = 2; // used with GetAncestor
public static final int GWL_EXSTYLE = -20; // used with GetWindowLong
public static final long WS_EX_APPWINDOW = 0x00040000L;
public static final Pointer HWND_TOP = new Pointer(0L); // used with
// SetWindowPos
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData);
int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);
int SetForegroundWindow(Pointer hWnd);
Pointer GetForegroundWindow();
boolean GetWindowRect(Pointer hWnd, RECT rect);
boolean SetWindowPos(Pointer hWnd, Pointer hWndInsertAfter, int x, int y,
int cx, int cy, int uFlags);
boolean MoveWindow(Pointer hWnd, int x, int y, int nWidth, int nHeight, boolean bRepaint);
boolean IsWindow(Pointer hWnd);
Pointer GetWindow(Pointer hWnd, int uCmd);
LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex);
Pointer GetParent(Pointer hWnd);
Pointer GetAncestor(Pointer hWnd, int gaFlags);
boolean IsWindowVisible(Pointer hWnd);
}
具有必要的例外类:
public class JnaUtilException extends Exception {
private static final long serialVersionUID = 1L;
public JnaUtilException(String text) {
super(text);
}
}
它可以与Java一起使用,类如下:
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.LONG_PTR;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
/**
* static methods to allow Java to call Windows code. user32.dll code is as
* specified in the JNA interface User32.java
*/
public class JnaUtil {
private static final User32 user32 = User32.INSTANCE;
private static Pointer callBackHwnd;
public static boolean windowExists(final String startOfWindowName) {
return !user32.EnumWindows(new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer userData) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) {
return false;
}
return true;
}
}, null);
}
public static List<String> getAllWindowNames() {
final List<String> windowNames = new ArrayList<String>();
user32.EnumWindows(new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer arg) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
if (!wText.isEmpty()) {
windowNames.add(wText);
}
return true;
}
}, null);
return windowNames;
}
public static boolean windowExists(Pointer hWnd) {
return user32.IsWindow(hWnd);
}
public static Pointer getWinHwnd(final String startOfWindowName) {
callBackHwnd = null;
user32.EnumWindows(new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer userData) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) {
callBackHwnd = hWnd;
return false;
}
return true;
}
}, null);
return callBackHwnd;
}
public static boolean setForegroundWindow(Pointer hWnd) {
return user32.SetForegroundWindow(hWnd) != 0;
}
public static Pointer getForegroundWindow() {
return user32.GetForegroundWindow();
}
public static String getForegroundWindowText() {
Pointer hWnd = getForegroundWindow();
int nMaxCount = 512;
byte[] lpString = new byte[nMaxCount];
int getWindowTextResult = user32
.GetWindowTextA(hWnd, lpString, nMaxCount);
if (getWindowTextResult == 0) {
return "";
}
return Native.toString(lpString);
}
public static boolean isForegroundWindow(Pointer hWnd) {
return user32.GetForegroundWindow().equals(hWnd);
}
public static boolean setForegroundWindow(String startOfWindowName) {
Pointer hWnd = getWinHwnd(startOfWindowName);
return user32.SetForegroundWindow(hWnd) != 0;
}
public static Rectangle getWindowRect(Pointer hWnd) throws JnaUtilException {
if (hWnd == null) {
throw new JnaUtilException(
"Failed to getWindowRect since Pointer hWnd is null");
}
Rectangle result = null;
RECT rect = new RECT();
boolean rectOK = user32.GetWindowRect(hWnd, rect);
if (rectOK) {
int x = rect.left;
int y = rect.top;
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
result = new Rectangle(x, y, width, height);
}
return result;
}
/**
* set window at x and y position with w and h width. Set on top of z-order
*
* @param hWnd
* @param x
* @param y
* @param w
* @param h
* @return boolean -- did it work?
*/
public static boolean setWindowPos(Pointer hWnd, int x, int y, int w, int h) {
int uFlags = 0;
return user32.SetWindowPos(hWnd, User32.HWND_TOP, x, y, w, h, uFlags);
}
public static boolean moveWindow(Pointer hWnd, int x, int y, int nWidth,
int nHeight) {
boolean bRepaint = true;
return user32.MoveWindow(hWnd, x, y, nWidth, nHeight, bRepaint );
}
public static Rectangle getWindowRect(String startOfWindowName)
throws JnaUtilException {
Pointer hWnd = getWinHwnd(startOfWindowName);
if (hWnd != null) {
return getWindowRect(hWnd);
} else {
throw new JnaUtilException("Failed to getWindowRect for \""
+ startOfWindowName + "\"");
}
}
public static Pointer getWindow(Pointer hWnd, int uCmd) {
return user32.GetWindow(hWnd, uCmd);
}
public static String getWindowText(Pointer hWnd) {
int nMaxCount = 512;
byte[] lpString = new byte[nMaxCount];
int result = user32.GetWindowTextA(hWnd, lpString, nMaxCount);
if (result == 0) {
return "";
}
return Native.toString(lpString);
}
public static Pointer getOwnerWindow(Pointer hWnd) {
return user32.GetWindow(hWnd, User32.GW_OWNER);
}
public static String getOwnerWindow(String childTitle) {
Pointer hWnd = getWinHwnd(childTitle);
Pointer parentHWnd = getOwnerWindow(hWnd);
if (parentHWnd == null) {
return "";
}
return getWindowText(parentHWnd);
}
public static Pointer getNextWindow(Pointer hWnd) {
if (hWnd == null) {
return null;
}
return user32.GetWindow(hWnd, User32.GW_HWNDNEXT);
}
public static boolean isWindowVisible(Pointer hWnd) {
return user32.IsWindowVisible(hWnd);
}
public static Pointer getParent(Pointer hWnd) {
return user32.GetParent(hWnd);
}
public static Pointer getRoot(Pointer hWnd) {
return user32.GetAncestor(hWnd, User32.GA_ROOT);
}
public static LONG_PTR getWindowLongPtr(Pointer hWndP, int nIndex) {
HWND hwnd = new HWND(hWndP);
return user32.GetWindowLongPtr(hwnd, nIndex);
}
// **** test method to show that this works.
// **** be sure to have an empty untitled new NotePad app running
public static void main(String[] args) throws InterruptedException {
List<String> winNameList = getAllWindowNames();
for (String winName : winNameList) {
System.out.println(winName);
}
String[] testStrs = { "Untitled-Notepad", "Untitled - Notepad",
"Untitled - Notepad", "Java-Epic", "Java - Epic", "Fubars rule!",
"The First Night", "New Tab", "Citrix X", "EHR PROD - SVC" };
for (String testStr : testStrs) {
Pointer hWnd = getWinHwnd(testStr);
boolean isWindow = windowExists(hWnd);
System.out.printf("%-22s %5b %16s %b%n", testStr,
windowExists(testStr), hWnd, isWindow);
}
String notePad = "Untitled - Notepad";
Pointer hWnd = getWinHwnd(notePad);
System.out.println("is it foreground window? " + isForegroundWindow(hWnd));
boolean foo = setForegroundWindow(notePad);
System.out.println("foregroundwindow: " + foo);
Thread.sleep(400);
System.out.println("is it foreground window? " + isForegroundWindow(hWnd));
Thread.sleep(1000);
System.out.println("here A");
try {
Rectangle rect = getWindowRect(notePad); //!!
System.out.println("rect: " + rect);
Robot robot = new Robot();
System.out.println("here B");
BufferedImage img = robot.createScreenCapture(rect);
System.out.println("here C, img is " + img);
Thread.sleep(500);
ImageIcon icon = new ImageIcon(img);
System.out.println("here D. icon is null? " + icon);
Thread.sleep(500);
final JLabel label = new JLabel(icon);
System.out.println("here E. label is null? " + label);
Thread.sleep(500);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("here F");
JOptionPane.showMessageDialog(null, label);
System.out.println("here G");
}
});
} catch (AWTException e) {
e.printStackTrace();
} catch (JnaUtilException e) {
e.printStackTrace();
}
}
}
import java.awt.AWTException;
导入java.awt.Rectangle;
导入java.awt.Robot;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.ImageIcon;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.SwingUtilities;
导入com.sun.jna.Native;
导入com.sun.jna.Pointer;
导入com.sun.jna.platform.win32.BaseTSD.LONG\u PTR;
导入com.sun.jna.platform.win32.WinDef.HWND;
导入com.sun.jna.platform.win32.WinDef.RECT;
/**
*允许Java调用Windows代码的静态方法。user32.dll代码如下所示
*在JNA接口User32.java中指定
*/
公共类JnaUtil{
私有静态final User32 User32=User32.INSTANCE;
私有静态指针callBackHwnd;
公共静态布尔windowExists(最终字符串startOfWindowName){
return!user32.EnumWindows(新user32.WNDENUMPROC(){
@凌驾
公共布尔回调(指针hWnd、指针userData){
字节[]windowText=新字节[512];
user32.GetWindowTextA(hWnd,windowText,512);
字符串wText=Native.toString(windowText.trim();
如果(!wText.isEmpty()&&wText.startsWith(startOfWindowName)){
返回false;
}
返回true;
}
},空);
}
公共静态列表getAllWindowNames(){
最终列表windowNames=new ArrayList();
user32.EnumWindows(新user32.WNDENUMPROC(){
@凌驾
公共布尔回调(指针hWnd、指针arg){
字节[]windowText=新字节[512];
user32.GetWindowTextA(hWnd,windowText,512);
字符串wText=Native.toString(windowText.trim();
如果(!wText.isEmpty()){
windowNames.add(wText);
}
返回true;
}
},空);
返回窗口名;
}
存在公共静态布尔窗口(指针hWnd){
返回user32.IsWindow(hWnd);
}
公共静态指针getWinHwnd(最终字符串startOfWindowName){
callBackHwnd=null;
user32.EnumWindows(新user32.WNDENUMPROC(){
@凌驾
公共布尔回调(指针hWnd、指针userData){
字节[]windowText=新字节[512];
user32.GetWindowTextA(hWnd,windowText,512);
字符串wText=Native.toString(windowText.trim();
如果(!wText.isEmpty()&&wText.startsWith(startOfWindowName)){
callBackHwnd=hWnd;
返回false;
}
返回true;
}
},空);
返回callBackHwnd;
}
公共静态布尔setForegroundWindow(指针hWnd){
返回user32.SetForegroundWindow(hWnd)!=0;
}
公共静态指针getForegroundWindow(){
返回user32.GetForegroundWindow();
}
公共静态字符串GetForeGroundIndowText(){
指针hWnd=getForegroundWindow();
int nMaxCount=512;
byte[]lpString=新字节[nMaxCount];
int getWindowTextResult=user32
.GetWindowTextA(hWnd、lpString、nMaxCount);
如果(getWindowTextResult==0){
返回“”;
}
返回Native.toString(lpString);
}
公共静态布尔值isForegroundWindow(指针hWnd){
返回user32.GetForegroundWindow().equals(hWnd);
}
公共静态布尔setForegroundWindow(字符串startOfWindowName){
指针hWnd=getWinHwnd(startOfWindowName);
返回user32.SetForegroundWindow(hWnd)!=0;
}
公共静态矩形getWindowRect(指针hWnd)引发JnaUtilException{
if(hWnd==null){
抛出新的JnautileException(
“无法获取WindowRect,因为指针hWnd为空”);
}
矩形结果=空;
RECT RECT=新的RECT();
布尔rectOK=user32.GetWindowRect(hWnd,rect);
if(rectOK){
int x=矩形左;
int y=rect.top;
int width=rect.right-rect.left;
int height=rect.bottom-rect.top;
结果=新矩形(x、y、宽度、高度);
}
返回结果;
}
/**
*将窗口设置在x和y位置,宽度为w和h。设置在z顺序的顶部
*
*@param-hWnd
*@param x
*@param y
*@param w
*@param h
*@return boolean——它起作用了吗?
*/
公共静态布尔setWindowPos(指针hWnd、int x、int y、int w、int h){
int-uFlags=0;
返回user32.SetWindowPos(hWnd,user32.hWnd_TOP,x,y,w,h,uFlags);
}
公共静态布尔移动窗口(指针hWnd、int x、int y、int nWidth、,
内特(右){
布尔值bRepaint=true;
返回user32.MoveWindow(hWnd、x、y、nWidth、nHeight、bRepaint);
}
公共静态矩形getWindowRect(字符串startOfWindowName)
抛出JnaUtilException{
指针hWnd=getWinHwnd(startOfWindowName);
如果(hWnd!=null){
返回getWindowRect(hWnd);
}否则{
抛出新的JnaUtilException(“未能获取\”的WindowRect)
+startOfWindowName+“\”);
}
}
公共静态指针getWindow(指针hWnd、int uCmd){
返回user32.GetWindow(hWnd,uCmd);
}
公共静态字符串getWindowText(指针hWnd){
int nMaxCount=512;
byte[]lpString=新字节[nMaxCount];
int result=user32.GetWindowTextA(hWnd、lpString、nMaxCount);
如果(结果==0){
返回“”;
}
返回Native.toString(lpString);
}
酒吧