更改壁纸使用Java。

huangapple go评论139阅读模式
英文:

Change wallpaper using java

问题

我在互联网上找到了这段代码,用于使用JNA库更改Windows壁纸,它正好符合我的需求,但只适用于.jar程序外部的图像(这是一段代码)

public class Wallpaper {

    public Wallpaper() {
        User32.INSTANCE.SystemParametersInfo(0x0014, 0, "C:\\Users\\Public\\Pictures\\Sample Pictures\\bg.jpg", 1);
    }
    
    public static interface User32 extends Library {

        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);

        boolean SystemParametersInfo(int one, int two, String s, int three);
    }
}

有没有办法从资源中获取文件?当我将文件路径替换为以下内容时,它不起作用:

getClass().getResource("bg.jpg").getPath()
英文:

I found this code on the internet for changing windows wallpaper using JNA library and it works just as I wanted but only with images outside of .jar program (here is a code)

public class Wallpaper {

public Wallpaper() {
    User32.INSTANCE.SystemParametersInfo(0x0014, 0, "C:\\Users\\Public\\Pictures\\Sample Pictures\\bg.jpg", 1);
}
public static interface User32 extends Library {

    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);

    boolean SystemParametersInfo(int one, int two, String s, int three);
}

}

is there a way to get the file from resources ? It doesn't work when I replace the file path with this

getClass().getResource("bg.jpg").getPath()

答案1

得分: 3

getResource 从您的类文件所在的位置检索资源,这可以是网络、实时生成的,或者最有可能是来自于一个jar文件,并且它们不是文件。您正在调用的Windows API 需要一个文件。

因此,答案是:你想要的是不可能的。

但你可以绕过它。

您可以将资源作为流获取,然后在某个地方打开一个文件,将字节传输过去(将资源“复制”到文件中),然后提供文件。类似于:

Path p = Paths.get(System.getProperty("user.home"), "backgrounds", "bg.jpg");
Files.createDirectories(p.getParent());
try (InputStream in = Wallpaper.class.getResourceAsStream("bg.jpg");
  OutputStream out = Files.newOutputStream(p)) {

    in.transferTo(out);
}
User32.INSTANCE.SystemParametersInfo(0x0014, 0, p.getAbsolutePath().toString(), 1);

注意:getResource 的正确用法不是通过 getClass() 而是:Wallpaper.class.getResource。如果进行子类化,getClass() 会出错。

英文:

getResource retrieves resources from whereever your class files live, and this could be a network, generated on the fly, or, most likely, from within a jar file, and those aren't files. The windows API you are calling requires a file.

Thus, the answer is :What you want, is impossible.

But you can work around it.

You can get the resource as a stream, open a file someplace, transfer the bytes over ('copying' the resource to a file) and then provide the file. Something like:

Path p = Paths.get(System.getProperty("user.home"), "backgrounds", "bg.jpg");
Files.createDirectories(p.getParent());
try (InputStream in = Wallpaper.class.getResourceAsStream("bg.jpg");
  OutputStream out = Files.newOutputStream(p)) {

    in.transferTo(out);
}
User32.INSTANCE.SystemParametersInfo(0x0014, 0, p.getAbsolutePath().toString(), 1);

NOTE: The correct usage of getResource is not via getClass() but: Wallpaper.class.getResource. getClass() breaks if subclassing.

huangapple
  • 本文由 发表于 2020年7月31日 19:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63191272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定