BufferedImage导致程序在MacO上冻结,但在Windows上不冻结 [英] BufferedImage causes a program freeze on MacOs but not on Windows

查看:98
本文介绍了BufferedImage导致程序在MacO上冻结,但在Windows上不冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一段代码抓取我的应用程序屏幕的屏幕截图,用于一个小组项目.在我的Macbook Pro上,代码冻结了屏幕,而在队友的PC(所有Windows)上,该代码运行得很好,并在其根目录中导出了.png文件.

I'm using a piece of code to grab a screenshot of my application screen for a group project. On my Macbook Pro the code freezes the screen whereas on my teammates's PC's (all Windows) it runs just fine and exports a .png file in their root dir.

public void screenShot(){
    //Creating an rbg array of total pixels
    int[] pixels = new int[WIDTH * HEIGHT];
    int bindex;
    // allocate space for RBG pixels
    ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3);

    // grab a copy of the current frame contents as RGB
    glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb);

    // convert RGB data in ByteBuffer to integer array
    for (int i=0; i < pixels.length; i++) {
        bindex = i * 3;
        pixels[i] =
                ((fb.get(bindex) << 16))  +
                        ((fb.get(bindex+1) << 8))  +
                        ((fb.get(bindex+2) << 0));
    }
    //Allocate colored pixel to buffered Image
    BufferedImage imageIn = null;
    try{
        //THIS LINE 
        imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
        //THIS LINE ^^^^^ 
        imageIn.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH);
    } catch (Exception e) {
        e.printStackTrace();
    }


问题

调试时,我可以看到在此行介入


The problem

When debugging I can see that when stepping in at this line

imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);

调试器不会转到BufferedImage构造函数,而是转到GLFWKeyCallbackI.callback(),然后转到GLFWCursorEnterCallbackI.callback().此后,它将完全停止.

the debugger doesn't go to the BufferedImage constructor but to GLFWKeyCallbackI.callback() and after that to GLFWCursorEnterCallbackI.callback(). After this it stops altogether.

在我的主类中,上面所有其余代码都制作了这样的缓冲图像:

In my main class above all the rest of the code making a buffered Image as such:

BufferedImage imageIn = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);

它也冻结了模拟,但实际上确实执行了该行.

It also freezes the simulation but it does seems to actually execute the line.

我不确定我还能尝试什么,我看到了从2005年到今天的其他几篇文章,询问类似的Mac问题而没有答案.

I'm not sure what else I could try, I saw a few other posts ranging between 2005 and today asking similar Mac questions without an answer.

推荐答案

我更深入地研究了问题所在.如评论中所述此处如果我提供此VM选项"-Djava.awt.headless = true",似乎可以解决此问题.

I delved a bit deeper and discovered the issue. As mentioned in a comment here if I provide this VM option "-Djava.awt.headless=true" it seems to fix the issue.

这篇关于BufferedImage导致程序在MacO上冻结,但在Windows上不冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆