简体   繁体   English

ImageIO.read(); 挂

[英]ImageIO.read(); Hang

Whilst I've seen some people talking about this problem I haven't been able to find any definitive answer. 虽然我已经看到一些人在谈论这个问题,但我还没有找到任何明确的答案。

In this block, the program WILL hang on ImageIO.read() 在此块中,程序将挂在ImageIO.read()上

System.out.println("1");
BufferedImage image = null;
System.out.println("2");
FileInputStream fis = prepareFile("./Textures/" + loc); <<-- "./Textures/TreeSmall.png"
System.out.println("3");
try {
    System.out.println("4"); <<- Logs
    image = ImageIO.read(fis); <<--Hangs
    System.out.println("5"); <<- Never reached
} catch (IOException e) {
    System.err.println("Unable to load Texture");
        System.exit(1);
    }

I've seen the suggestion to use the boot argument -Djava.awt.headless=true , and whilst this does stop it from hanging - it just releases this error and doesn't read the file: 我已经看到了使用引导参数-Djava.awt.headless=true的建议,尽管这样做确实阻止了它挂起-它只是释放了此错误并且不读取文件:

4
2018-06-08 10:29:42.742 java[2345:45920] [JRSAppKitAWT markAppIsDaemon]: Process manager already initialized: can't fully enable headless mode.
5

Here is the encapsulating class: 这是封装类:

... ...

public class TextureEngine {
    public int textureID;
    public TextureEngine(String loc){ 
            try {

                System.out.println("1");
               BufferedImage image = null;
               System.out.println("2");
               FileInputStream fis = prepareFile("./Textures/" + loc);
               System.out.println("3");

                   System.out.println("4");
                   image = ImageIO.read(fis);
                   System.out.println("5");


              int[] pixels = new int[image.getWidth() * image.getHeight()];
                image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

                ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
                System.out.println("3");
                for(int y = 0; y < image.getHeight(); y++){
                    for(int x = 0; x < image.getWidth(); x++){
                        int pixel = pixels[y * image.getWidth() + x];
                        buffer.put((byte) ((pixel >> 16) & 0xFF));   
                        buffer.put((byte) ((pixel >> 8) & 0xFF));   
                        buffer.put((byte) (pixel & 0xFF));   
                        buffer.put((byte) ((pixel >> 24) & 0xFF)); 
                    }

                buffer.flip();

                textureID = glGenTextures(); 
                glBindTexture(GL_TEXTURE_2D, textureID); 

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
                glBindTexture(GL_TEXTURE_2D, 0);

           }
               } catch (Throwable t) {
                   System.err.println("Unable to load Texture");
                   System.exit(1);
               }
    }
        public static FileInputStream prepareFile(String loc){
            File file = new File(loc);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return fis;
        }
        protected void finalize() throws Throwable{
            glDeleteTextures(textureID);
            super.finalize();
        }
           public void bind(int sampler){
               if(sampler >= 0 && sampler <= 31){
                    glActiveTexture(GL_TEXTURE0 + sampler);
                    glBindTexture(GL_TEXTURE_2D, textureID);
               }
    }
}

Which is called as such: 这样称呼:

TextureEngine tex = new TextureEngine("TreeSmall.png");
    //Menu Loop
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
        tex.bind(0);
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2f(-0.9f, 0.9f);

        glColor4f(0,0,0,0);
        glTexCoord2f(0,1);
        glVertex2f(0.9f, 0.9f);
        glTexCoord2f(1,1);
        glVertex2f(0.9f, -0.9f);
        glTexCoord2f(1,0);
        glVertex2f(-0.9f, -0.9f);
        glEnd();

I tried parsing to a ByteArray as such: 我试图解析成这样的ByteArray:

Path texture = Paths.get("./Textures/" + loc);
byte[] fis = Files.readAllBytes(texture);
ByteArrayInputStream textureBIS = new
ByteArrayInputStream(fis);
System.out.println("4");
image = ImageIO.read(textureBIS);
System.out.println("5");

And using BufferedImage - but it still hangs on the same line, 'ImageIO.read();' 并使用BufferedImage-但它仍然挂在同一行'ImageIO.read();'

I'm using a Hackintosh and heard it could be a problem with this OS (I'm on 10.12.6 ) 我正在使用Hackintosh,听说此操作系统可能有问题(我在10.12.6

I'm fairly new to java, and not quite sure how to continue, any pointers? 我对Java还是很陌生,并且不太确定如何继续使用任何指针?

Three things to consider: 要考虑的三件事:

  1. Look into prepareFile , your input stream may be blocking depending on what you are doing there. 查看prepareFile ,您的输入流可能会阻塞,具体取决于您在此处的操作。

  2. Your example also does not account for the possibility of an unchecked RuntimeException or Error occurring. 您的示例也没有考虑发生未经检查的RuntimeExceptionError可能性。 Since it is not a self-contained program with its own public static void main() {} , we can't know whether the program would terminate if, for example, a bad dependency setup here threw a NoClassDefFoundError . 由于它不是具有自己的public static void main() {}的自包含程序,因此,例如,如果此处不良的依赖性设置引发了NoClassDefFoundError ,我们将无法知道该程序是否会终止。

    If you don't have an IDE that can show whether this is the case, then just for debugging, you can wrap the whole thing in } catch (Throwable t) { } to check. 如果没有显示该情况的IDE,则仅用于调试,您可以将整个内容包装在} catch (Throwable t) { }进行检查。 If it is indeed a hang, then you may have OS issues, as you've said. 如前所述,如果确实是挂起,则可能是操作系统问题。

  3. A quick web search shows that some Java users on Mac have seen ImageIO hang when reading from a file, which certainly is strange. 快速的网络搜索显示,某些Mac上的Java用户在从文件读取时看到ImageIO挂起,这当然很奇怪。 You can isolate this further by reading the FileInputStream to a byte array, and then passing this as ByteArrayInputStream to ImageIO, but at this point I would be checking software versions. 您可以通过将FileInputStream读取到byte数组,然后将其作为ByteArrayInputStream传递到ImageIO来进一步隔离,但是此时我将检查软件版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM