How to change laptop screen brightness from a java application?
Asked Answered
W

2

6

I want to create a java application to change the laptop screen brightness on windows xp/7. Please help

Weihs answered 8/4, 2013 at 13:36 Comment(0)
H
6

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.

Hardandfast answered 3/12, 2015 at 17:50 Comment(2)
Unfortunately it doesn't work.It gives this error on Windows 10->Standard Error: Get-WmiObject : Not supported At line:1 char:42 + ... myMonitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBright ... +Discontent
Resume-> + 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 : InvokeMethodOnNullDiscontent
R
0

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.

Redeeming answered 8/4, 2013 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.