为什么ToolkitImage getBufferedImage()返回null? [英] Why does ToolkitImage getBufferedImage() return a null?

查看:603
本文介绍了为什么ToolkitImage getBufferedImage()返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 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?

编辑:
更大的任务(这个问题的根源)在这里:
以最快,最简单的方式扩展BufferedImage

推荐答案

如果查询 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()您描述的方法,但值得一试。

I doubt this will be any faster than the drawImage() method you described, but is worth a try.

这篇关于为什么ToolkitImage getBufferedImage()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆