如何从Java应用程序更改笔记本电脑的屏幕亮度?

前端之家收集整理的这篇文章主要介绍了如何从Java应用程序更改笔记本电脑的屏幕亮度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想创建一个java应用程序来改变Windows XP / 7上的笔记本电脑屏幕亮度.请帮忙

最佳答案
正如其他人所说,没有任何官方API可供使用.但是,使用Windows Powershell(我相信它附带的Windows,因此无需下载任何内容)和WmiSetBrightness,可以创建一个简单的解决方法,适用于所有带签证或以后安装的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,我适合运行此命令.

猜你在找的Java相关文章