我想创建一个Java应用程序来更改Windows XP/7上笔记本电脑的屏幕亮度。请帮忙
最佳答案
正如其他人所说,没有任何官方API可以使用。但是,使用Windows Powershell(我相信Windows附带了Windows Powershell,因此无需下载任何内容)和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提供的Powershell代码here,我使它适合于运行此命令。