I am working on part of a Java application that takes an image as a byte array, reads it into a java.awt.image.BufferedImage
instance and passes it to a third-party library for processing.
For a unit test, I want to take an image (from a file on disk) and assert that it is equal to the same image that has been processed by the code.
- My expected
BufferedImage
is read from a PNG file on disk usingImageIO.read(URL)
. - My test code reads the same file into a
BufferedImage
and writes that to a byte array as PNG to provide to the system under test.
When the system under test writes the byte array to a new BufferedImage
I want to assert that the two images are equal in a meaningful way. Using equals()
(inherited from Object
) doesn’t work (of course). Comparing BufferedImage.toString()
values also doesn’t work because the output string includes object reference information.
Does anybody know any shortcuts? I would prefer not to bring in a third-party library for a single unit test in a small part of a large application.
.equals()
won't work? – InamoratoBufferedImage
does not overrideObject#equals()
. docjar.com/html/api/java/awt/image/BufferedImage.java.html – ConvenanceBufferedImage
instances, would the fastest check of comparison between those images answer your question? 2) Is memory a huge problem? (Are they 4000x3000px images?) – Blockish