468,740 Members | 1,982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,740 developers. It's quick & easy.

Image overlay and comparison code error.

5 2Bits
I am still very much a beginner in programming, and i am facing an issue with a code that i am hoping someone could help me with. I found the java code below for overlaying and comparing the pixels of two images in an online forum. When i try to compile the code i get a syntax error. Hope you can help me find the error. Here is the link to three images showing the errors that i am getting ( https://postimg.cc/gallery/fJZW2k5 ). Thanks in advance.


Expand|Select|Wrap|Line Numbers
  1. package stackexchange;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. public class PictureOverlayTest {
  11.     /*
  12.      * Four variables, three for the wanted BufferedImages, one String for the
  13.      * Path of the third Image, which does not already exist.
  14.      */`
  15.  
  16.     private BufferedImage image1;
  17.     private BufferedImage image2;
  18.     private BufferedImage image3;
  19.  
  20.     private String pathImage3;
  21.  
  22.     public PictureOverlayTest(String filePathAndName1, String filePathAndName2,
  23.             String filePathAndName3) throws IOException {
  24.         /*
  25.          * Constructor in order to keep this method reusable and clean. Needs
  26.          * three Strings. The paths and Filenames of all three images. Image 1
  27.          * and 2 should exist already, Image 3 will be created if all
  28.          * requirements are met. Constructor creates the first two buffered
  29.          * images, sets all needed variables and starts the checkAndCompare()
  30.          * method
  31.          */
  32.  
  33.         File file = new File(filePathAndName1);
  34.         this.image1 = ImageIO.read(file);
  35.  
  36.         file = new File(filePathAndName2);
  37.         this.image2 = ImageIO.read(file);
  38.  
  39.         this.pathImage3 = filePathAndName3;
  40.         checkAndCompare();
  41.     }
  42.  
  43.     private void checkAndCompare() throws IOException {
  44.         /*
  45.          * This function creates the Color blue, compares the sizes of both
  46.          * pictures and if they are the same, creates a third image. Then it
  47.          * loops through the two images and compares each pixel. If the pixels
  48.          * are the same, the third image gets a blue pixel at that point
  49.          */
  50.  
  51.         Color blue = Color.blue;
  52.  
  53.         if (image1.getHeight() == image2.getHeight()
  54.                 && image1.getWidth() == image2.getWidth()) {
  55.  
  56.             image3 = new BufferedImage(image1.getWidth(), image1.getHeight(),
  57.                     image1.getType());
  58.             for (int y = 0; y < image1.getHeight(); y++) {
  59.                 for (int x = 0; x < image1.getWidth(); x++) {
  60.  
  61.                     int colorImage1 = image1.getRGB(x, y);
  62.                     int colorImage2 = image2.getRGB(x, y);
  63.  
  64.                     if (colorImage1 == colorImage2) {
  65.  
  66.                         image3.setRGB(x, y, blue.getRGB());
  67.  
  68.                     } else {
  69.  
  70.                         // Whatever Color you want. By default it is black.
  71.  
  72.                     }
  73.  
  74.                 }
  75.             }
  76.             savePicture3();
  77.             System.out.println("Message: Image comparison is done");
  78.  
  79.         } else {
  80.  
  81.             System.out.println("Error: Image dimensions do not match");
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.     private void savePicture3() throws IOException {
  88.         /*
  89.          * This method saves the created Image into a file onto your computer.
  90.          * The if() statement is used to check if the file was successfully
  91.          * created, in order to avoid unwanted errors. Keep in mind, that you
  92.          * have to change the "bmp" in ImageIO.write() to whatever format you
  93.          * actually want
  94.          */
  95.  
  96.         File file = new File(pathImage3);
  97.         if (file.createNewFile()) {
  98.             ImageIO.write(image3, "bmp", file);
  99.         }
  100.     }
  101.  
  102. }
  103.  
  104. package stackexchange;
  105.  
  106. import java.io.IOException;
  107.  
  108. public class Main {
  109.  
  110.     public static void main(String[] args) {
  111.         // TODO Auto-generated method stub
  112.  
  113.         try {
  114.             PictureOverlayTest test = new PictureOverlayTest(
  115.                     "H:\\stackexchange\\file1.bmp",
  116.                     "H:\\stackexchange\\file2.bmp",
  117.                     "H:\\stackexchange\\file3.bmp");
  118.         } catch (IOException e) {
  119.             // TODO Auto-generated catch block
  120.             e.printStackTrace();
  121.         }
  122.     }
  123.  
  124. }
3 Weeks Ago #1
0 1031

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Robert Peden | last post: by
7 posts views Thread by JewelsT | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.