File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Image Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image Comparison" Watch "Image Comparison" New topic

Image Comparison

Visakh Menon

Joined: Aug 20, 2003
Posts: 4
Hi all
I need to verify the icon displayed on the frame is the right image. Assuming I can get hold of the ImageIcon object from the GUI, and I have the image gif files in my file system, what is the best way to do a comparison ?
Thanks in advance
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Please define "is the right image". What would or wouldn't make it "the right image"?

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
Visakh Menon

Joined: Aug 20, 2003
Posts: 4
I mean I need to verify if the ImageIcon is created on the same gif file - verify if the name, size, height, width etc are matching with the gif file I have in my file system.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

You can get an Image object from an ImageIcon, imageIcon.getImage(), and then you can get the height and width of the image that way. The name should be pretty simple to get. Not sure about the size. Are you talking file size? You will probably need to use the IO API and the File object to get the size of the image if you mean file size.

GenRocket - Experts at Building Test Data
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Filenames aren't associated with Image objects in Java... Images can be built completely in memory without ever touching a file. You can get height and width, but you can scale an image, and so this won't be accurate either. The most accurate solution (but personally I think this is overkill... visual comparison would be so much simpler, unless you have to do this through a program...) would be to scale the image from the file to the same size as the image from the frame and then do a pixel by pixel comparison. There's still got to be some leeway, both at the pixel level and at the image level, but this is really the only way to do a programmatic image comparison. Oh, and to get the pixels from an Image either use PixelGrabber or a BufferedImage.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Image Comparison
It's not a secret anymore!