繁体   English   中英

ImageIO.read(); 挂

[英]ImageIO.read(); Hang

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

在此块中,程序将挂在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. 查看prepareFile ,您的输入流可能会阻塞,具体取决于您在此处的操作。

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

    如果没有显示该情况的IDE,则仅用于调试,您可以将整个内容包装在} catch (Throwable t) { }进行检查。 如前所述,如果确实是挂起,则可能是操作系统问题。

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

暂无
暂无

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

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