如何获取R中的屏幕分辨率

发布于 2024-12-02 20:11:54 字数 31 浏览 3 评论 0原文

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

How can I get screen resolution (height, width) in pixels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜还是长夜 2024-12-09 20:11:54

您可以使用命令行界面 WMI

> system("wmic desktopmonitor get screenheight")
ScreenHeight  
900   

您可以使用 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" 

对于多个屏幕,输出例如:

[1] "ScreenWidth  \r" "1600         \r" "1600         \r" ""

我们希望将除第一个和最后一个值之外的所有值转换为数字

as.numeric(c(
  scr_width[-c(1, length(scr_width))], 
  scr_height[-c(1, length(scr_height))]
))
# [1] 1440  900

You could use commad-line interface to WMI

> system("wmic desktopmonitor get screenheight")
ScreenHeight  
900   

You could capture result with 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, e.g.,

[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
愛放△進行李 2024-12-09 20:11:54

使用 JavaScript 很简单:您只需

window.screen.height
window.screen.width

使用 SpiderMonkey 包即可从 R 调用 JavaScript href="http://www.omegahat.org/" rel="nofollow noreferrer">OmegaHat。


您还可以使用 Java 解决此问题,并使用 < a href="http://cran.r-project.org/web/packages/rJava/index.html" rel="nofollow noreferrer">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")

It's easy with JavaScript: you just do

window.screen.height
window.screen.width

You can call JavaScript from R using the SpiderMonkey package from OmegaHat.


You could also solve this with Java, and use the rJava package to access it.

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")
快乐很简单 2024-12-09 20:11:54

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

使用:

system("wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizo​​ntalResolution /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)]

现在您将拥有一个矢量长度为 2:

> current_resolution
[1] 1920 1080

Accepted answer does not work on Windows 8 and later.

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:

> current_resolution
[1] 1920 1080
時窥 2024-12-09 20:11:54

在 Windows 上,您可以调用 GetSystemMetrics 传递 SM_CXSCREENSM_CYSCREEN。这将返回主显示监视器屏幕的宽度/高度(以像素为单位)。

DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);

On Windows you can call GetSystemMetrics passing SM_CXSCREEN and 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);
牵你的手,一向走下去 2024-12-09 20:11:54

rpanel 包有一个返回屏幕分辨率的函数:

rpanel::rp.screenresolution()
$width
[1] 1540

$height
[1] 845

The rpanel package has a function that returns the screen resolution:

rpanel::rp.screenresolution()
$width
[1] 1540

$height
[1] 845
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文