'How to change laptop screen brightness from a java application?
I want to create a java application to change the laptop screen brightness on windows xp/7. Please help
Solution 1:[1]
As others have stated, there isn't any official API to use. However, using Windows Powershell (which comes with windows I believe, so no need to download anything) and WmiSetBrightness, one can create a simple workaround that should work on all windows PCs with visa or later installed.
All you need to do is copy this class into your workspace:
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();
}
}
And then call
BrightnessManager.setBrightness({brightness});
Where {brightness} is the brightness you want to set the screen display at with 0 being the dimmest supported brightness and 100 being the brightest.
Big thanks to anquegi for the powershell code found here that I adapted to run this command.
Solution 2:[2]
I don't think there is a standard API do do that in Java.
But it seems that you can do it in .NET in windows. See: What API call would I use to change brightness of laptop (.NET)?
You can always use a JNI interface to call a native method written in C++ - so this might be a workaround.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Community |
