[英]How can I get the screen resolution in R
如何获得屏幕分辨率(高度,宽度)(以像素为单位)?
You could use commad-line interface to WMI 您可以使用commad-line接口到WMI
> system("wmic desktopmonitor get screenheight")
ScreenHeight
900
You could capture result with system
您可以使用
system
捕获结果
(scr_width <- system("wmic desktopmonitor get screenwidth", intern=TRUE))
# [1] "ScreenWidth \r" "1440 \r" "\r"
(scr_height <- system("wmic desktopmonitor get screenheight", intern=TRUE))
# [1] "ScreenHeight \r" "900 \r" "\r"
With multiple screens, the output is, eg, 使用多个屏幕,输出例如是
[1] "ScreenWidth \r" "1600 \r" "1600 \r" ""
We want all but the first and last values, converted to numbers 我们想要除了第一个和最后一个值之外的所有值,转换为数字
as.numeric(c(
scr_width[-c(1, length(scr_width))],
scr_height[-c(1, length(scr_height))]
))
# [1] 1440 900
It's easy with JavaScript: you just do 使用JavaScript很容易:你就是这么做的
window.screen.height
window.screen.width
You can call JavaScript from R using the SpiderMonkey package from OmegaHat . 您可以使用OmegaHat的SpiderMonkey包从R调用JavaScript。
You could also solve this with Java , and use the rJava package to access it. 您也可以使用Java解决此问题 ,并使用rJava包来访问它。
library(rJava)
.jinit()
toolkit <- J("java.awt.Toolkit")
default_toolkit <- .jrcall(toolkit, "getDefaultToolkit")
dim <- .jrcall(default_toolkit, "getScreenSize")
height <- .jcall(dim, "D", "getHeight")
width <- .jcall(dim, "D", "getWidth")
Accepted answer does not work on Windows 8 and later. 接受的答案在Windows 8及更高版本中不起作用。
Use this: 用这个:
system("wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value")
To get you screen resolution in a vector, you can implement it as: 要在矢量中获得屏幕分辨率,您可以将其实现为:
suppressWarnings(
current_resolution <- system("wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value", intern = TRUE) %>%
strsplit("=") %>%
unlist() %>%
as.double()
)
current_resolution <- current_resolution[!is.na(current_resolution)]
Now you'll have a vector with length 2: 现在你将有一个长度为2的向量:
> current_resolution
[1] 1920 1080
On Windows you can call GetSystemMetrics
passing SM_CXSCREEN
and SM_CYSCREEN
. 在Windows上,您可以调用
GetSystemMetrics
传递SM_CXSCREEN
和SM_CYSCREEN
。 This returns the width/height of the screen of the primary display monitor, in pixels. 这将返回主显示监视器屏幕的宽度/高度(以像素为单位)。
DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.