如何在R中获得屏幕分辨率

Geo*_*tas 13 windows r resolution screen system

如何获得屏幕分辨率(高度,宽度)(以像素为单位)?

Mar*_*rek 11

您可以使用commad-line接口到WMI

> system("wmic desktopmonitor get screenheight")
ScreenHeight  
900   
Run Code Online (Sandbox Code Playgroud)

你可以捕获结果 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" 
Run Code Online (Sandbox Code Playgroud)

使用多个屏幕,输出例如是

[1] "ScreenWidth  \r" "1600         \r" "1600         \r" ""
Run Code Online (Sandbox Code Playgroud)

我们想要除了第一个和最后一个值之外的所有值,转换为数字

as.numeric(c(
  scr_width[-c(1, length(scr_width))], 
  scr_height[-c(1, length(scr_height))]
))
# [1] 1440  900
Run Code Online (Sandbox Code Playgroud)


MS *_*nds 6

接受的答案不适用于 Windows 8 及更高版本。

用这个:

system("wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value")

要获得矢量中的屏幕分辨率,您可以将其实现为:

  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)]
Run Code Online (Sandbox Code Playgroud)

现在你将拥有一个长度为 2 的向量:

> current_resolution
[1] 1920 1080
Run Code Online (Sandbox Code Playgroud)


Ric*_*ton 5

使用JavaScript很容易:您只需

window.screen.height
window.screen.width
Run Code Online (Sandbox Code Playgroud)

您可以使用OmegaHatSpiderMonkey包从R调用JavaScript 。


您也可以使用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")
Run Code Online (Sandbox Code Playgroud)

  • 下票是什么?我知道这些想法只完成了一半,但是仍然是可行的可能性。 (2认同)
  • 在我看来,这不需要JVM就可以实现!就我个人而言,我只是包装了一些Win32 API调用,但是肯定在某个地方有一个允许您查询硬件信息的软件包。 (2认同)