pankaj_ar
pankaj_ar

Reputation: 765

How to get the path of current active desktop wallpaper using Java

I want to make some changes on active desktop wallpaper like adding watermark.

For that I need to get the path of the active wallpaper. Adding the watermark I can do.

This can be done using JNA library, but I can't access the file path.

Upvotes: 3

Views: 2318

Answers (3)

Mahozad
Mahozad

Reputation: 24532

Using JNA library:

implementation("net.java.dev.jna:jna-platform:5.12.1")

I was able to get the current Wallpaper path like this (tested on Windows 11):

Java:

import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;

public class Main {

    public static final String REGISTRY_PATH = "Control Panel\\Desktop";
    public static final String REGISTRY_Key = "WallPaper";

    public static void main(String[] args) {
        var hKey = new HKEYByReference();
        var registryAccessResult = openRegistryKey(hKey);
        validate(registryAccessResult);
        var wallpaperPath = getWallpaperPath();
        System.out.println(wallpaperPath);
    }

    public static int openRegistryKey(HKEYByReference hKey) {
        return Advapi32.INSTANCE.RegOpenKeyEx(
                WinReg.HKEY_CURRENT_USER,
                REGISTRY_PATH,
                0,
                WinNT.KEY_READ,
                hKey
        );
    }

    public static void validate(int registryAccessResult) {
        if (registryAccessResult != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(registryAccessResult);
        }
    }

    public static String getWallpaperPath() {
        return Advapi32Util.registryGetStringValue(
                WinReg.HKEY_CURRENT_USER,
                REGISTRY_PATH,
                REGISTRY_Key
        );
    }
}

Kotlin:

const val REGISTRY_PATH = "Control Panel\\Desktop"
const val REGISTRY_Key = "WallPaper"

fun main() {
    val hKey = HKEYByReference()
    openRegistryKey(hKey).validate()
    val wallpaperPath = getWallpaperPath()
    println(wallpaperPath)
}

fun openRegistryKey(hKey: HKEYByReference) =
    Advapi32.INSTANCE.RegOpenKeyEx(
        WinReg.HKEY_CURRENT_USER,
        REGISTRY_PATH,
        0,
        WinNT.KEY_READ,
        hKey
    )

fun RegistryAccessResult.validate() =
    takeIf { it == W32Errors.ERROR_SUCCESS }
        ?: throw Win32Exception(this)

fun getWallpaperPath(): String =
    Advapi32Util.registryGetStringValue(
        WinReg.HKEY_CURRENT_USER,
        REGISTRY_PATH,
        REGISTRY_Key
    )

You can also listen for wallpaper changes and get its new path:

waitForWallpaperChange(hKey).validate() // Waits until wallpaper is changed
val newWallpaper = getWallpaperPath()

fun waitForWallpaperChange(hKey: HKEYByReference) =
    Advapi32.INSTANCE.RegNotifyChangeKeyValue(
        hKey.value,
        false,
        WinNT.REG_NOTIFY_CHANGE_LAST_SET,
        null,
        false
    )

Upvotes: 0

LemonJuiz
LemonJuiz

Reputation: 21

As Low Flying Pelican and ee said,

HKEY_CURRENT_USER\Control Panel\Desktop

contains the key Wallpaper which has a pointer to the wallpaper. For the command prompt, you can use

reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper

to get the location, or using this for native java support:

Runtime.getRuntime().exec('reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper');

Upvotes: 2

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

The way to obtain the current desktop wallpaper could different based on the operating system, for windows 7 it can be obtained from following registry path,

HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper

to read the registry path you can use the method described in following question

read/write to Windows Registry using Java

Upvotes: 3

Related Questions