I want to create a java application to change the laptop screen brightness on windows xp/7. Please help
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.
+ 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
–
Discontent 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.
© 2022 - 2024 — McMap. All rights reserved.
10->Standard Error: Get-WmiObject : Not supported At line:1 char:42 + ... myMonitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBright ... +
– Discontent