【发布时间】: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 绘制一条线,然后尝试显示它。 结果如下:
所以它起作用了。因此,原始图像(或发生的转换)一定有问题
编辑 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