468,740 Members | 1,990 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 error

5 2Bits
Hey everyone, 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 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. Thanks in advance.

Code:


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. }
  125.  
4 Weeks Ago #1
4 2961
dev7060
450 Expert 256MB
Hey everyone, 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 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.
What error?‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎
4 Weeks Ago #2
UniDue
5 2Bits
The code doesnt compile at all it just says syntax error

These are the errors am getting:
https://postimg.cc/gallery/fJZW2k5
3 Weeks Ago #3
UniDue
5 2Bits
Three code errors am getting are:

When trying to rung the code i get a window saying "The selection cannot be launched, and there are no recent launches"
next to the first code line "module Image_Overlay {" am getting the error "Syntax error on token(s), misplaced construct(s)"
next to code line Nr. 7 am getting the error "The type java.awt.image.BufferedImage is not accessible"

and some others, but i think these are the main ones.
3 Weeks Ago #4
dev7060
450 Expert 256MB
The code doesnt compile at all it just says syntax error
I can compile the code posted in #1 fine (no package, same directory)


"module Image_Overlay {" am getting the error "Syntax error on token(s), misplaced construct(s)"
The code posted in #1 contains no such line.

When trying to rung the code i get a window saying "The selection cannot be launched, and there are no recent launches"
The dialogs could be specific to the IDE you're using. You may need to refer to its manual.

"The type java.awt.image.BufferedImage is not accessible"
My guess is with an IDE, one needs to configure/add/mention the required packages manually under the project settings before using them.

and some others, but i think these are the main ones.
Subsequent errors could show up because of one root error.
Attached Images
File Type: png op2.png (10.1 KB, 203 views)
3 Weeks Ago #5

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
2 posts views Thread by John | last post: by
1 post views Thread by bjjnova | last post: by
2 posts views Thread by chazzy69 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | 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.