BufferedImage不显示(全黑),但可以显示图像 [英] BufferedImage not displaying (all black) but Image can be displayed

查看:224
本文介绍了BufferedImage不显示(全黑),但可以显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java图形(一般是计算机图形)和Stack Overflow的新手,所以请帮助我,帮助我更好地表达我的问题。

I'm new to Java graphics (computer graphics in general) and Stack Overflow, so please help me out and help me phrase my problem better.

在此刻,我试图在Java GUI中与原始图像一起显示BufferedImage。这是我的代码:

At the moment, I'm trying to display a BufferedImage alongside the original image in a Java GUI. This is my code:

Image oriImage = robot.getQwerkController().getVideoStreamService().getFrame(); //load the original image
Graphics2D hi = bufferedImage.createGraphics();
hi.drawImage(oriImage, 0,0, null); //draw the original image into the BufferedImage
hi.dispose();
images[0].put(oriImage, 1); //draws the original image in the first frame
images[1].put(bufferedImage, 2); //draws the BufferedImage in the second frame

中绘制BufferedImage, put函数如下:

and the "put" function is as follows:

public synchronized void put(Image image, int frameNumber) {
    icon.setImage(image); //sets the image displayed by this icon
    display.paintImageIcon(icon, frameNumber); //paint the image onto the gui
    imageSet = true;
    notifyAll();
}

但是,生成的GUI如下

However, the resulting GUI is as follows

因此Image有效,但BufferedImage不起作用。我认为它是有效的,因为BufferedImage是Image的子类...
有什么想法吗?请让我知道是否需要其他代码〜预先感谢:)

So the Image works, but BufferedImage doesn't. I'd assume it works because BufferedImage is a subclass of Image... Any idea? Please let me know if additional code is needed~ Thanks in advance :)

编辑1:

我做到了经过一些测试,而不是使用原始图像,我创建了一个全新的bufferedImage,并使用Graphics2D绘制了一条线,然后尝试显示它。
是这里的结果:

I did some testing, and instead of using the original image, I created an whole new bufferedImage, and used Graphics2D to draw a line and then try and display it. here is the result:

http://i.imgur.com/rJAdp.jpg

这样就可以了。因此,原始图像(或发生的转换)肯定有问题

So it works. Therefore there must be something wrong with the original image (or the conversion that happened)

编辑2:
我对bufferedImage做了类似的事情并画了一条线。结果与EDIT 1相同,因此我认为drawImage函数存在一些问题。

EDIT 2: I did a similar thing with bufferedImage and drew a line. The result are the same as EDIT 1, and therefore I think there's some problem with drawImage function.

EDIT 3:
我花了一段时间解决了这个问题循环执行drawImage以完成图像绘制(因为如果尚未完成绘制图像,则返回false),并且可以正常工作! :D

EDIT 3: I fixed the problem by putting a while loop around the drawImage to let it complete the image drawing (because it returns false if it hasn't finish drawing an image yet) and it worked! :D

boolean x = false;
while (!x) {
    x = hi.drawImage(oriImage, 0,0, null); //draw the original image into the BufferedImage
}


推荐答案

如果仅通过 ImageObserver JFrame JPanel 实例)放入 drawImage 方法而不是 null 。然后,这将为您处理异步图像的加载。

You should be able to achieve the same result if you just pass the ImageObserver (the JFrame or JPanel instance into which you're drawing) into the drawImage method instead of null. This will then take care of the asynchronous image loading for you.

这篇关于BufferedImage不显示(全黑),但可以显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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