简体   繁体   中英

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()

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:

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:

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();'

I'm using a Hackintosh and heard it could be a problem with this OS (I'm on 10.12.6 )

I'm fairly new to java, and not quite sure how to continue, any pointers?

Three things to consider:

  1. Look into prepareFile , your input stream may be blocking depending on what you are doing there.

  2. Your example also does not account for the possibility of an unchecked RuntimeException or Error occurring. 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 .

    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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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