[英]How to check if a monitor is plugged in? (Linux)
這不是我要在linux中完成的其他問題的重復。
我正在開發板上進行圖像處理,為此,我正在筆記本電腦上進行測試/開發。 在OpenCV中,有imshow()
用於顯示圖像。 但是,只有在有顯示器的情況下,它才起作用。 因此,我想在調用監視器之前檢查它是否存在,以便在代碼在PC上運行而不是在板上運行時調用它。
我該如何完成?
...
...
if(<only-if-monitor-is-present>)
imshow(img);
...
...
您可以嘗試將xset (user preference utility for X)
與-q
標志一起使用:
q選項為您提供有關當前設置的信息。
我編寫了使用該命令並解析輸出的簡單程序:
#include <cstdlib>
#include <iostream>
bool is_monitor_present() {
int result = system("xset -q | tail -n1 | grep 'Monitor is On'");
return result == 0;
}
int main() {
bool found = is_monitor_present();
if(found) {
std::cout << "Monitor is present." << std::endl;
} else {
std::cout << "Monitor absent." << std::endl;
}
}
我的筆記本電腦在Linux Ubuntu 15.04上的執行結果如下:
監視當前。
在Raspberry上使用最新或幾乎最新的Raspbian的相同代碼:
xset:無法打開顯示“”
監視器不存在。
我不建議在生產環境中使用此代碼,但對於某些測試來說似乎可以正常工作。 至少在Debians。
方法如下:
int MonitorNotPresent = system("xset -q | tail -n1 | grep On");
if(!WEXITSTATUS(MonitorNotPresent))
imshow(img)
使用的標題:
cstdlib
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.