java 尽快清除透明的 BufferedImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2367365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:48:09  来源:igfitidea点击:

Clear a transparent BufferedImage as fast as possible

javaawtbufferedimagegraphics2d

提问by adrian.tarau

I have a transparent BufferedImage created with the following code(not relevant how it is created, I think):

我有一个使用以下代码创建的透明 BufferedImage(我认为与它的创建方式无关):

            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();

            Rectangle screen = transformationContext.getScreen();

            // Create an image that supports transparent pixels
            return gc.createCompatibleImage((int) screen.getWidth(), (int) screen.getHeight(),
                    Transparency.BITMASK);

How do I clear the image(empty image in the same state as it was created) in the fastest way possible without recreating the image? Recreating the image puts a burden on GC, pausing the VM and freezing the UI.

如何在不重新创建图像的情况下以最快的方式清除图像(与创建时处于相同状态的空图像)?重新创建图像会给 GC 带来负担,暂停 VM 并冻结 UI。

回答by adrian.tarau

Got it :) used clearRect instead of fill with a transparent color.

明白了 :) 使用 clearRect 而不是填充透明颜色。

            graphics = (Graphics2D) offlineBuffer.getGraphics();
            graphics.setBackground(new Color(255, 255, 255, 0));
            Rectangle screen = transformationContext.getScreen();
            graphics.clearRect(0,0, (int)screen.getWidth(), (int)screen.getHeight());

回答by SyntaxT3rr0r

One relatively fast way, but I don't know if it's the fastest (and I'd like to see other answers) is to have another picture that you never modify and that is always "fully cleared" / "fully transparent" and then you do a raster copy, say you named that copy CLEAR:

一种相对较快的方法,但我不知道它是否是最快的(我想看到其他答案)是拥有另一张永远不会修改的图片,并且始终“完全清除”/“完全透明”,然后你做了一个光栅副本,假设你将该副本命名为 CLEAR:

imageYouWantToClear.setData( CLEAR.getRaster() );

Note that working with graphics can be very tricky when it comes to performances because there are a lot of not-very-well-documented behavior. For example your images (say the CLEAR one) maybe hardware-accelerated but you'd then lose hardware-acceleration as soon as you'd use a mutating method (like say a setRgb()) and it would prove very difficult to realize that you just lost the benefit of hardware acceleration.

请注意,当涉及到性能时,处理图形可能会非常棘手,因为有很多没有很好记录的行为。例如,您的图像(例如 CLEAR 图像)可能是硬件加速的,但是一旦您使用变异方法(例如setRgb()),您就会失去硬件加速,并且很难实现你刚刚失去了硬件加速的好处。

I think that the best place to find infos on the subject of performant BufferedImage would be in the Java game-programmers and Java game-API-programmers community/forums.

我认为在 Java game-programmers 和 Java game-API-programmers 社区/论坛中找到有关高性能 BufferedImage 主题的信息的最佳位置。

Btw make sure that both your BufferedImage are using the 'compatible' mode: TYPE_INT_ARGB may be fine on Windows but not on OS X, etc. so you want to create them doing something like:

顺便说一句,请确保您的 BufferedImage 都使用“兼容”模式:TYPE_INT_ARGB 在 Windows 上可能没问题,但在 OS X 等上可能没问题,所以您想创建它们,执行以下操作:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

Ouch the Law-of-Demeter hurts, thanks Java ;)

哎哟迪米特法则很伤人,感谢 Java ;)