要實現Windows的桌面截圖功能,網上能找到不少源碼,實現也不是很困難。但若是但願截圖功能有很強的可用性,應付各類狀況,卻也不容易,有不少坑要填。java
這裏介紹的開源代碼WinRobot@github,主要有如下優勢:git
這些代碼並非爲了展現而實現的簡單demo,而是擁有良好設計和測試的可用於生產環境的代碼github
可以截取Winlogon、UAC、屏保、DirectShowOverlay的圖像session
高性能、使用進程間共享內存的方式儘可能避免截圖時數據的copy,在常規配置PC上能達到25fpsapp
支持java,且兼容java.awt.Robot接口性能
支持Windows 2000及以上平臺,支持3二、64位系統測試
#ifdef _WIN64 #import "WinRobotCorex64.dll" raw_interfaces_only, raw_native_types,auto_search,no_namespace #import "WinRobotHostx64.exe" auto_search,no_namespace #else #import "WinRobotCorex86.dll" raw_interfaces_only, raw_native_types,auto_search,no_namespace #import "WinRobotHostx86.exe" auto_search,no_namespace #endif CComPtr<IWinRobotService> pService; hr = pService.CoCreateInstance(__uuidof(ServiceHost) ); //get active console session CComPtr<IUnknown> pUnk; hr = pService->GetActiveConsoleSession(&pUnk); CComQIPtr<IWinRobotSession> pSession = pUnk; // capture screen pUnk = 0; hr = pSession->CreateScreenCapture(0,0,1024,768,&pUnk); // get screen image data(with file mapping) CComQIPtr<IScreenBufferStream> pBuffer = pUnk; CComBSTR name; ULONG size = 0; pBuffer->get_FileMappingName(&name); pBuffer->get_Size(&size); CFileMapping fm; fm.Open(name,size,false); // do something with fm...
import com.caoym.WinRobot; //... WinRobot robot; BufferedImage screen = robot.createScreenCapture(new Rectangle(0, 0, 1024, 768));