【发布时间】:2017-07-18 15:56:57
【问题描述】:
我正在跟随一本关于 LWJGL3 的教程书,我尝试制作自己的图像加载代码(本书代码不使用 STB 库),并且在项目正常运行时它工作得非常好(IntelliJ IDE,运行直接从 build 文件夹),它将图像从 ByteBuffer 加载,stbi_load_from_memory 工作正常,但是一旦编译并放入 jar 文件它就停止工作,即使图像已成功加载到 ByteBuffer ,就好像 stbi_load_from_memory 函数在 Jar 中时只返回 null。
代码:
public static ByteBuffer loadImage(String fileName, IntBuffer w,
IntBuffer h, IntBuffer comp) throws Exception
{
ByteBuffer image;
//the class name is Utils
InputStream imageFile =
Utils.class.getResourceAsStream(fileName);
//The image data gets put into the byte array no matter if
//its a jar or not
byte[] imageData = new byte[imageFile.available()];
imageFile.read(imageData);
ByteBuffer imageBuffer =
BufferUtils.createByteBuffer(imageData.length);
imageBuffer.put(imageData);
imageBuffer.flip();
//imageBuffer is the right size and has the right contents
//This is where everything fails if its in a jar,
//image is set to null and the Exception is thrown
image = stbi_load_from_memory(imageBuffer, w, h, comp, 0);
if(image == null)
{
throw new Exception("Failed to load image: " + fileName);
}
return image;
}
不能是拼写错误的文件名或资源路径,因为无论是不是jar,它都会正确地将数据加载到数组中。
附加信息:
Java 版本:1.8.0_131 64 位
处理器架构:amd64
IDE 版本(如果出于某种原因需要):IntelliJ 2017.1.4 Ultimate
操作系统:Ubuntu 16.04 LTS 64 位
内核:Linux 4.8.0-58-generic amd64
LWJGL 版本:3.1.2
该项目是在 Gradle 3.3 中制作的
如果需要我会上传并发布整个源代码的链接
【问题讨论】:
标签: java intellij-idea lwjgl