文章目录
1. 导入如下包
2. 直接使用即可
WinDef.HWND parentWindow = User32.INSTANCE.FindWindow(null, "xxx");
// WinDef.HWND parentWindow= User32.INSTANCE.FindWindowEx();
User32.INSTANCE.GetWindowRect(parentWindow, dimensionsOfWindow );
3. 如果直接使用的时,没有响应的方法,此时需要自定义方法
interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(WinDef.HWND hWnd, byte[] lpString, int nMaxCount);
}
4. kernel32和user32的区别
kernel包含有:进程处理相关、文件相关等等系统相关的
user32包含有视窗处理
5. java截获窗口区域生产图片样例代码
package sample.main;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class javavideo {
public static void main(String[] args) {
getChildWindowName();
startScreencapture();
}
static void getChildWindowName(){
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WinUser.WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(WinDef.HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
User322.INSTANCE.GetWindowTextA(hWnd,windowText,512);
String wText= null;
try {
wText = new String(windowText,"gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (wText.isEmpty()) {
return true;
}
if(!"".equals(wText)){
System.out.println("Found window with text " + hWnd + ", total " + ++count
+ " Text: " + wText+"--"+wText.length());
}
return true;
}
}, null);
}
static void startScreencapture() {
WinDef.RECT dimensionsOfWindow = new WinDef.RECT();
//获取谷歌浏览器某一个标签的窗口
WinDef.HWND parentWindow = User32.INSTANCE.FindWindow(null, "xxx - Google Chrome");
User32.INSTANCE.GetWindowRect(parentWindow, dimensionsOfWindow );
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
System.out.println(dimensionsOfWindow.toRectangle());
BufferedImage buf = robot.createScreenCapture(dimensionsOfWindow.toRectangle());
File output = new File("www.png");
try {
ImageIO.write(buf, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
interface User322 extends StdCallLibrary {
User322 INSTANCE = (User322) Native.loadLibrary("user32", User322.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(WinDef.HWND hWnd, byte[] lpString, int nMaxCount);
}
6. java直接使用BufferedImage 动态显示到javafx
package sample;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import javax.imageio.ImageIO;
import javax.net.ssl.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
// primaryStage.setTitle("Hello World");
// primaryStage.setScene(new Scene(root, 800, 500));
// primaryStage.show();
ImageView imageView = new ImageView();
WinDef.RECT dimensionsOfWindow = new WinDef.RECT();
//获取谷歌浏览器某一个标签的窗口
WinDef.HWND parentWindow = User32.INSTANCE.FindWindow(null, "Flink常规运维课程.pptx - PowerPoint");
User32.INSTANCE.GetWindowRect(parentWindow, dimensionsOfWindow);
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Robot robot = new Robot();
BufferedImage buf = robot.createScreenCapture(dimensionsOfWindow.toRectangle());
//将BufferedImage 转换成WritableImage 显示到imageview上
WritableImage image = SwingFXUtils.toFXImage(buf, null);
imageView.setImage(image);
Thread.sleep(200);
} catch (AWTException | InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
VBox vBox = new VBox(imageView);
Scene scene1 = new Scene(vBox, 1000, 700);
primaryStage.setTitle("Hello!");
primaryStage.setScene(scene1);
primaryStage.show();
}
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name veri
// }
//
//
//
//}fier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
launch(args);
}
static void getChildWindowName() {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WinUser.WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(WinDef.HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
User322.INSTANCE.GetWindowTextA(hWnd, windowText, 512);
String wText = null;
try {
wText = new String(windowText, "gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (wText.isEmpty()) {
return true;
}
if (!"".equals(wText)) {
System.out.println("Found window with text " + hWnd + ", total " + ++count
+ " Text: " + wText + "--" + wText.length());
}
return true;
}
}, null);
}
static void startScreencapture() {
WinDef.RECT dimensionsOfWindow = new WinDef.RECT();
//获取谷歌浏览器某一个标签的窗口
WinDef.HWND parentWindow = User32.INSTANCE.FindWindow(null, "Flink常规运维课程.pptx - PowerPoint");
User32.INSTANCE.GetWindowRect(parentWindow, dimensionsOfWindow);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
System.out.println(dimensionsOfWindow.toRectangle());
BufferedImage buf = robot.createScreenCapture(dimensionsOfWindow.toRectangle());
File output = new File("www.png");
try {
ImageIO.write(buf, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
interface User322 extends StdCallLibrary {
User322 INSTANCE = (User322) Native.loadLibrary("user32", User322.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(WinDef.HWND hWnd, byte[] lpString, int nMaxCount);
}