Is there a simple way to compare BufferedImage instances?

前端 未结 8 900
刺人心
刺人心 2020-12-05 19:01

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-p

相关标签:
8条回答
  • 2020-12-05 19:27

    I changed function that equals by pixels in Groovy, may be helpful:

    boolean imagesAreEqual(BufferedImage image1, BufferedImage image2) {
        if (image1.width != image2.width || image1.height != image2.height) {
             return false
        }
        for (int x = 1; x < image2.width; x++) {
            for (int y = 1; y < image2.height; y++) {
                 if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
                     return false
                 }
            }
        }
        return true
    }
    
    0 讨论(0)
  • 2020-12-05 19:29

    I can't think of anything besides a brute force "do loop":

      BufferedImage bi1, bi2, ...
       ...
      Raster r1 = bi1.getData();
      DataBuffer db1 = r1.getDataBuffer();
      if (db1.getSize() != db2.getSize ())
         ...
      for (int i = 0; i < db1.getSize(); i++) {  
        int px = db1.getElem(i);
      }
    
    0 讨论(0)
提交回复
热议问题