【问题标题】:How to change laptop screen brightness from a java application?如何从 Java 应用程序更改笔记本电脑屏幕亮度?
【发布时间】:2013-03-30 14:53:18
【问题描述】:

我想创建一个 java 应用程序来更改 windows xp/7 上的笔记本电脑屏幕亮度。请帮忙

【问题讨论】:

    标签: java windows brightness


    【解决方案1】:

    我认为在 Java 中没有标准的 API 可以做到这一点。

    但您似乎可以在 Windows 中的 .NET 中执行此操作。看: What API call would I use to change brightness of laptop (.NET)?

    您始终可以使用 JNI 接口来调用用 C++ 编写的本机方法 - 所以这可能是一种解决方法。

    【讨论】:

      【解决方案2】:

      正如其他人所说,没有任何官方 API 可供使用。但是,使用 Windows Powershell(我相信 Windows 附带,因此无需下载任何东西)和WmiSetBrightness,可以创建一个简单的解决方法,该解决方法应该适用于所有安装了 visa 或更高版本的 Windows PC。

      您需要做的就是将这个类复制到您的工作区中:

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      
      public class BrightnessManager {
          public static void setBrightness(int brightness)
                  throws IOException {
              //Creates a powerShell command that will set the brightness to the requested value (0-100), after the requested delay (in milliseconds) has passed. 
              String s = String.format("$brightness = %d;", brightness)
                      + "$delay = 0;"
                      + "$myMonitor = Get-WmiObject -Namespace root\\wmi -Class WmiMonitorBrightnessMethods;"
                      + "$myMonitor.wmisetbrightness($delay, $brightness)";
              String command = "powershell.exe  " + s;
              // Executing the command
              Process powerShellProcess = Runtime.getRuntime().exec(command);
      
              powerShellProcess.getOutputStream().close();
      
              //Report any error messages
              String line;
      
              BufferedReader stderr = new BufferedReader(new InputStreamReader(
                      powerShellProcess.getErrorStream()));
              line = stderr.readLine();
              if (line != null)
              {
                  System.err.println("Standard Error:");
                  do
                  {
                      System.err.println(line);
                  } while ((line = stderr.readLine()) != null);
      
              }
              stderr.close();
      
          }
      }
      

      然后调用

      BrightnessManager.setBrightness({brightness});
      

      其中 {brightness} 是您希望将屏幕显示设置为的亮度,0 是支持的最暗亮度,100 是最亮的亮度。

      非常感谢 anquegi 为我找到了 here 的 powershell 代码,我已将其用于运行此命令。

      【讨论】:

      • 不幸的是它不起作用。它在 Windows 10->Standard Error: Get-WmiObject : Not supported At line:1 char:42 + ... myMonitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBright ... + 上给出了这个错误
      • 简历-> + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand You cannot call a method on a null-valued expression. At line:1 char:111 + ... torBrightnessMethods;$myMonitor.wmisetbrightness($delay, $brightness) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2020-02-05
      • 2010-09-28
      • 2010-09-27
      • 2021-01-02
      相关资源
      最近更新 更多