问题描述
我想将 Image
转换为 BufferedImage
。我知道以下内容:
I want to convert an Image
to a BufferedImage
. I know the following:
Image tmp;
... //read, scale
BufferedImage buffered = new BufferedImage(SMALL_SIZE, SMALL_SIZE,
BufferedImage.TYPE_INT_RGB);
buffered.getGraphics().drawImage(tmp, 0, 0, null);
但它真的很慢。我需要 BufferedImage
,因为我必须得到像素颜色数据。
我发现了一种可行的方法,没有绘图:
But it's really slow. I need BufferedImage
, because I have to get pixel color data.I found a possible way to do it, without drawing:
ToolkitImage ti = (ToolkitImage) tmp;
BufferedImage buffered = tmp.getBufferedImage();
但它总是返回 null
即可。任何人都可以为此提供解决方案吗?
But it always returns null
. Can anyone offer a solution for this?
编辑:
更大的任务(这个问题的根源)在这里:
推荐答案
如果查询 getHeight()
或
方法将触发内部 ToolkitImage
上的getWidth() BufferedImage
的生成。所以你的代码将成为:
If you query the getHeight()
or getWidth()
methods on the ToolkitImage
you will trigger the generation of the internal BufferedImage
. So your code would become:
ToolkitImage ti = (ToolkitImage) tmp;
ti.getWidth();
BufferedImage buffered = ti.getBufferedImage();
我怀疑这会比 drawImage()$更快c $ c>您描述的方法,但值得一试。
I doubt this will be any faster than the drawImage()
method you described, but is worth a try.
这篇关于为什么ToolkitImage getBufferedImage()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!