Java BuffereImage不显示(全黑),但可以显示图像

Java BuffereImage不显示(全黑),但可以显示图像,java,bufferedimage,graphics2d,Java,Bufferedimage,Graphics2d,我不熟悉Java图形(一般来说是计算机图形)和堆栈溢出,所以请帮助我解决问题并帮助我更好地表达我的问题 目前,我正试图在JavaGUI中显示原始图像旁边的BuffereImage。这是我的代码: Image oriImage = robot.getQwerkController().getVideoStreamService().getFrame(); //load the original image Graphics2D hi = bufferedImage.createGraphics()

我不熟悉Java图形(一般来说是计算机图形)和堆栈溢出,所以请帮助我解决问题并帮助我更好地表达我的问题

目前,我正试图在JavaGUI中显示原始图像旁边的BuffereImage。这是我的代码:

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
“put”功能如下所示:

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如下所示

因此图像可以工作,但BuffereImage不能。我认为它是有效的,因为BuffereImage是Image的一个子类。。。 有什么想法吗?如果需要其他代码,请告诉我~提前感谢:)

编辑1:

我做了一些测试,并没有使用原始图像,而是创建了一个全新的BuffereImage,并使用Graphics2D绘制一条线,然后尝试显示它。 结果如下:

所以它起作用了。因此,原始图像(或发生的转换)一定有问题

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

编辑3: 我修复了这个问题,在drawImage周围放置了一个while循环,让它完成图像绘制(因为如果它还没有完成图像绘制,它将返回false),并且成功了!:D

boolean x = false;
while (!x) {
    x = hi.drawImage(oriImage, 0,0, null); //draw the original image into the BufferedImage
}
抱歉,但我猜您正在使用即时绘制(推送)。一般来说,java是以另一种方式(拉)实现的

JavaSwing/awt绘制事件驱动的图像:不会立即绘制图像,但可以通过失效或重新绘制来触发图像。您可以使用帧速率启动swing计时器并在那里调用repaint()

通常,JPanel子级可能会覆盖
paintComponent(Graphics g)
并在
g
上绘制


然而,这很奇怪。您可以尝试记录
oriImage.getHeight(null)
,以查看(不太可能)图像是否异步生成(开始时高度为-1)。

如果只传递
ImageObserver
(正在绘制的
JFrame
JPanel
实例),您应该能够获得相同的结果进入
drawImage
方法,而不是
null
。这将为您处理异步图像加载。

您能否发布最初创建
buffereImage
对象的代码。
//同步加载原始图像(等待图像加载)或异步加载(图像加载时代码继续)?为了更快地获得更好的帮助,请发布一个。如果我可以发布一个SSCCE,我会这样做,问题是整个项目相当大,很多代码与问题无关。(我们现在得到的图像来自一个机器人上安装的网络摄像头)不过我会编辑更多信息。谢谢作为BuffereImage启动的BuffereImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);是的,我在看过代码文档后意识到了这一点。