代码之家  ›  专栏  ›  技术社区  ›  online.0227

如何在Java上检索图形卡信息?

  •  8
  • online.0227  · 技术社区  · 13 年前

    有没有任何可能的方法可以使用Java API检索我的图形卡适配器的信息?

    我知道DirectX可以很容易地做到这一点,然而,我只是想知道Java是否可以做到这一步。。。?

    喜欢下面的图片。。DirectX找到了与我的硬件集成的GPU适配器,以及它支持的分辨率列表。

    我的问题是,有没有Java可以做这种事情的API? 我真的很想知道Java是否能够获得有关视频卡的信息。

    enter image description here

    非常感谢。

    2 回复  |  直到 13 年前
        1
  •  9
  •   dic19    13 年前

    正如@Sergey K.在回答中所说,有几种方法可以做到这一点。其中之一是使用 dxdiag 工具(显然它只适用于Windows) dxdiag /t 将输出重定向到给定文件的变量。然后,您可以处理该文件以获取所需信息:

    public static void main(String[] args) {        
        try {
    
            String filePath = "./foo.txt";
            // Use "dxdiag /t" variant to redirect output to a given file
            ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","dxdiag","/t",filePath);
            System.out.println("-- Executing dxdiag command --");
            Process p = pb.start();
            p.waitFor();
    
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line;
            System.out.println(String.format("-- Printing %1$1s info --",filePath));
            while((line = br.readLine()) != null){
                if(line.trim().startsWith("Card name:") || line.trim().startsWith("Current Mode:")){
                    System.out.println(line.trim());
                }
            }
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }
    
    }
    

    生成的文件如下所示:

    enter image description here

    输出将如下所示:

    --执行dxdiag命令--
    --打印/foo.txt信息--
    卡名:Intel(R)HD Graphics系列
    当前模式:1366 x 768(32位)(60Hz)

        2
  •  3
  •   Sergey K.    13 年前

    在Java中有几种方法可以做到这一点。但它们最终都使用DirectX/OpenGL/C++/WinAPI/任何东西作为后端。

    这两种API都需要Java绑定。或者,您可以用C/C++编写代码,并通过JNI使用它。