ImageIO.read();挂

时间:2018-06-08 09:37:52

标签: java hang javax.imageio

虽然我看到有些人在谈论这个问题,但我找不到任何明确的答案。

在此块中,程序将挂起在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);
    }

我已经看到了使用引导参数-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

这是封装类:

...

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);
               }
    }
}

这称为:

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

我尝试解析为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");

使用BufferedImage - 但它仍然挂在同一行,'ImageIO.read();'

我正在使用Hackintosh并且听说这可能是这个操作系统的问题(我在10.12.6

我对java很新,不太确定如何继续,任何指针?

1 个答案:

答案 0 :(得分:0)

要考虑三件事:

  1. 查看prepareFile,您的输入流可能会阻塞,具体取决于您在那里所做的事情。

  2. 您的示例也未考虑未经检查RuntimeExceptionError的可能性。由于它不是具有自己的public static void main() {}的自包含程序,因此我们无法知道程序是否会终止,例如,如果此处的错误依赖项设置引发了NoClassDefFoundError

    如果您没有可以显示是否是这种情况的IDE,那么只需进行调试,您可以将整个内容包装在} catch (Throwable t) { }中进行检查。如果它确实是一个悬念,那么你可能会遇到操作系统问题,正如你所说的那样。

  3. 快速网络搜索显示Mac上的某些Java用户在从文件读取时看到ImageIO挂起,这当然很奇怪。您可以通过阅读FileInputStreambyte数组,然后将其作为ByteArrayInputStream传递给ImageIO来进一步隔离,但此时我将检查软件版本。