- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试仅从 Activity 浏览器/窗口捕获屏幕截图时,屏幕底部的其他窗口或其他打开的应用程序(最小化)将显示在任务栏上。
那么如何仅从 Activity/光标聚焦的浏览器捕获并忽略屏幕截图中任务底部的其他窗口
以下是我迄今为止编写的用于捕获屏幕截图的代码,但它仍然捕获屏幕截图中的其他打开的应用程序。
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
height));
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
String fileNameToSaveTo = "D:/Pictures/" + PictureName + "_"
+ createTimeStampStr() + ".PNG";
writeImage(bi, fileNameToSaveTo, "PNG");
System.out.println("Screen Captured Successfully and Saved to:\n"+fileNameToSaveTo);
最佳答案
解决方案是不捕获整个屏幕,而仅捕获包含您感兴趣的窗口的矩形。不过,要获得此窗口,您需要超越 Java,例如使用第 3 方实用程序,或通过 JNI 或 JNA。我已经使用 JNA 成功完成了此操作。我会发布我的代码,但它是在超过 100 个类的大型项目中。
我可以展示我使用的一些特定的类和接口(interface),但它们很旧,而且无论如何都不是“专业品质”:
User32.java
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;
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);
}
JnaUtilException.java
public class JnaUtilException extends Exception {
private static final long serialVersionUID = 1L;
public JnaUtilException(String text) {
super(text);
}
}
JnaUtil.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
*
* @author Pete S
*
*/
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);
}
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 ehrProd = "EHR PROD - SVC";
Pointer hWnd = getWinHwnd(ehrProd);
System.out
.println("is it foreground window? " + isForegroundWindow(hWnd));
boolean foo = setForegroundWindow(ehrProd);
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(ehrProd);
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();
}
}
}
方法JnaUtil.getWindowRect(hWnd);
获取矩形并像这样使用:
new SwingWorker<Void, ImageIcon>() {
protected Void doInBackground() throws Exception {
Pointer hWnd = JnaUtil.getWinHwnd(winText);
if (hWnd == null) {
String errTxt = "Application Window \"" + winText
+ "\" not open. Try again";
JOptionPane.showMessageDialog(pollingGui.getMainPanel(), errTxt,
"App Not Found", JOptionPane.ERROR_MESSAGE);
return null;
}
try {
rect = JnaUtil.getWindowRect(hWnd);
fullWindowText = JnaUtil.getWindowText(hWnd);
关于java - 如何在任务栏底部没有其他窗口的情况下捕获 Activity 浏览器的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252686/
我是 Java 新手,这是我的代码, if( a.name == b.name && a.displayname == b.displayname && a.linknam
在下面的场景中,我有一个 bool 值。根据结果,我调用完全相同的函数,唯一的区别是参数的数量。 var myBoolean = ... if (myBoolean) { retrieve
我是一名研究 C++ 的 C 开发人员: 我是否正确理解如果我抛出异常然后堆栈将展开直到找到第一个异常处理程序?是否可以在不展开的情况下在任何 throw 上打开调试器(即不离开声明它的范围或任何更高
在修复庞大代码库中的错误时,我观察到一个奇怪的情况,其中引用的动态类型从原始 Derived 类型更改为 Base 类型!我提供了最少的代码来解释问题: struct Base { // some
我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。 使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即
这可能是一个哲学问题。 假设您正在向页面发出 AJAX 请求(这是使用 Prototype): new Ajax.Request('target.asp', { method:"post", pa
我有以下 HTML 代码,我无法在所有浏览器中正常工作: 我试图在移动到
我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两
我设置了一个详细 View ,我是否有几个 Nib 文件根据在 Root View Controller 的表中选择的项目来加载。 我发现,对于 Nibs 的类,永远不会调用 viewDidUnloa
我需要动态访问 json 文件并使用以下代码。在本例中,“bpicsel”和“temp”是变量。最终结果类似于“data[0].extit1” var title="data["+bpicsel+"]
我需要使用第三方 WCF 服务。我已经在我的证书存储中配置了所需的证书,但是在调用 WCF 服务时出现以下异常。 向 https://XXXX.com/AHSharedServices/Custome
在几个 SO 答案(1、2)中,建议如果存在冲突则不应触发 INSERT 触发器,ON CONFLICT DO NOTHING 在触发语句中。也许我理解错了,但在我的实验中似乎并非如此。 这是我的 S
如果进行修改,则会给出org.hibernate.NonUniqueObjectException。在我的 BidderBO 类(class)中 @Override @Transactional(pr
我使用 indexOf() 方法来精细地查找数组中的对象。 直到此刻我查了一些资料,发现代码应该无法正常工作。 我在reducer中尝试了上面的代码,它成功了 let tmp = state.find
假设我有以下表格: CREATE TABLE Game ( GameID INT UNSIGNED NOT NULL, GameType TINYINT UNSIGNED NOT NU
代码: Alamofire.request(URL(string: imageUrl)!).downloadProgress(closure: { (progress) in
我是一名优秀的程序员,十分优秀!