强制加载由 Java Image 对象表示的图像数据
Forcing the loading of image data represented by a Java Image object
有没有办法,使用Image
, to force the loading of the corresponding image's data so that e.g. Image.getWidth(ImageObserver)
returns类型的实例引用有效数据? — 例如:
Image image = getImageFromSomewhereElse();
image.load(); // This method doesn't exist but it would be wonderful if it did
Image otherImage = getImageFromYetAnotherPlace();
otherImage.load();
// "compareImages" from <
// "toBufferedImage" from <
if (compareImages(toBufferedImage(image), toBufferedImage(otherImage))){
System.out.println("Yay! Images are the same!");
}
背景
在现有的应用程序中,我现在需要测试对 Image
的对象的两个引用是否实际上是“相同”的图片(但是可以做到最好):为了做到这一点,我我正在尝试按照 an answer to a related question. In order to do this with the provided solution, however, I need to convert the Image
instances I've got into BufferedImage
instances, e.g. by constructing a new BufferedImage
and then drawing the other image's data onto it as suggested in another post 中的建议进行逐像素比较。但是,与发布到链接答案的评论相对应,这在我的情况下是不可能的,因为相关图像尚未加载:
...
Caused by: java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
java.awt.image.BufferedImage.(BufferedImage.java:333)
...
不幸的是,创建 Image
实例的代码非常复杂,所以我不愿意开始重构,例如而是检查用于创建 Image
实例的数据是否等效。因此,最简单的方法是在进行逐像素检查之前简单地确保以某种方式加载每个图像。
有个精彩class
MediaTracker mt=new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
}
catch (InterruptedException me) { System.out.println("error); }
然后您可以使用MediaTracker.html中的任何错误方法来检查可能的errors/misloading。
使用Toolkit#get/createImage
或java.awt.Image
使用后台线程加载图片,你可以使用ImageObserver
(通过MediaTracker
)来监控图片的加载状态或者你可以只使用 ImageIO.read
来代替,它会在返回
之前完全加载图像
有关详细信息,请参阅 Reading/Loading an image
有没有办法,使用Image
, to force the loading of the corresponding image's data so that e.g. Image.getWidth(ImageObserver)
returns类型的实例引用有效数据? — 例如:
Image image = getImageFromSomewhereElse();
image.load(); // This method doesn't exist but it would be wonderful if it did
Image otherImage = getImageFromYetAnotherPlace();
otherImage.load();
// "compareImages" from <
// "toBufferedImage" from <
if (compareImages(toBufferedImage(image), toBufferedImage(otherImage))){
System.out.println("Yay! Images are the same!");
}
背景
在现有的应用程序中,我现在需要测试对 Image
的对象的两个引用是否实际上是“相同”的图片(但是可以做到最好):为了做到这一点,我我正在尝试按照 an answer to a related question. In order to do this with the provided solution, however, I need to convert the Image
instances I've got into BufferedImage
instances, e.g. by constructing a new BufferedImage
and then drawing the other image's data onto it as suggested in another post 中的建议进行逐像素比较。但是,与发布到链接答案的评论相对应,这在我的情况下是不可能的,因为相关图像尚未加载:
...
Caused by: java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
java.awt.image.BufferedImage.(BufferedImage.java:333)
...
不幸的是,创建 Image
实例的代码非常复杂,所以我不愿意开始重构,例如而是检查用于创建 Image
实例的数据是否等效。因此,最简单的方法是在进行逐像素检查之前简单地确保以某种方式加载每个图像。
有个精彩class
MediaTracker mt=new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
}
catch (InterruptedException me) { System.out.println("error); }
然后您可以使用MediaTracker.html中的任何错误方法来检查可能的errors/misloading。
使用Toolkit#get/createImage
或java.awt.Image
使用后台线程加载图片,你可以使用ImageObserver
(通过MediaTracker
)来监控图片的加载状态或者你可以只使用 ImageIO.read
来代替,它会在返回
有关详细信息,请参阅 Reading/Loading an image