【问题标题】:BufferedImage not displaying (all black) but Image can be displayedBufferedImage 不显示(全黑)但可以显示图像
【发布时间】:2012-07-03 03:50:34
【问题描述】:

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

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

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

所以 Image 有效,但 BufferedImage 无效。我认为它有效,因为 BufferedImage 是 Image 的子类... 任何的想法?如果需要额外的代码,请告诉我~提前谢谢:)

编辑 1:

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

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

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

编辑 2: 我对 bufferedImage 做了类似的事情并画了一条线。结果与 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
}

【问题讨论】:

  • 能否请您发布您最初如何创建bufferedImage 对象的代码。
  • //load the original image 同步(等待图像加载)还是异步(代码在图像加载时继续)?为了尽快获得更好的帮助,请发帖SSCCE
  • 如果我可以发布SSCCE,我会的,问题是整个项目很大,很多代码与问题无关。 (我们现在得到的图像来自加载在机器人上的网络摄像头)但是我会编辑以获取更多信息。谢谢!
  • bufferedImage 初始化为 bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

标签: java bufferedimage graphics2d


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    对不起,我猜你正在使用即时绘画(推)。一般来说,java 会反过来(拉)。

    Java swing/awt 绘制事件驱动:不会立即绘制图像,但可以通过无效或重绘来触发它。你可以用你的帧速率启动一个摇摆定时器并在那里调用 repaint()。

    一般来说,JPanel 子级可能会覆盖 paintComponent(Graphics g) 并使用 g

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

    【讨论】:

    • 是的,我在查看代码文档后意识到了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多