473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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. }
Jul 1 '21 #1
0 1990

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

Similar topics

2
by: Robert Peden | last post by:
Hi All, I have used javascript to chamge a button's image when the mouse moves over the button (graphic) without problems. But I have come upon a new challenge - buttons which overlay each...
7
by: JewelsT | last post by:
Hello, I'm new to both RSS & XML and I am trying to create a ColdFusion RSS feed. I have my cfm file with all my code and at the end of the file I am writing that output to an XML file. At the top...
1
by: TSalm | last post by:
Hi, After sending a request, I would get the possible error message. Not the code @@error, nor the exact content of sysmessages, but the message like it could be in the log file. TIA, TSalm
12
by: Shawn Northrop | last post by:
Ive been searching for an image resize tutorial for a while now and found this code which worked nicely. I was unable to find the full source code but i think i pieced together the code from the...
13
by: problem. | last post by:
#include <stdio.h> #include <stdlib.h> int main(void) { int a, b; float result = 0.0f; char symbol = '\0'; int loop = 0;
2
by: valy | last post by:
Hi guys, do y'all have any idea to add in date and time to a RGB image in C code and in Linux? My live webcam view left off this function. thanks.
0
by: vagueante | last post by:
Hi, I have a windows service that i connecto to a iSeries Dataqueue, i can write and read from it. The purpose of this service it's to keep reading the dataqueue and when it has some data i run...
3
by: Yousef Altaf | last post by:
hay I have this code to upload images it works OK bu there is 2 things not working 1- when the image has the same name it doesn't display the message "A file of the same name already exists" it just...
4
by: UniDue | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.