473,437 Members | 1,773 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,437 software developers and data experts.

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.  
Jun 23 '21 #1
4 4187
dev7060
638 Expert 512MB
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?‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎
Jun 23 '21 #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
Jul 1 '21 #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.
Jul 1 '21 #4
dev7060
638 Expert 512MB
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, 369 views)
Jul 2 '21 #5

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

Similar topics

7
by: PW | last post by:
I'm trying to interrogate an incoming value from a previous ASP like this .... myCurrentQty = request.querystring("txtBCQty" & myRecordCounter) It returns a "2" as expected. But if I try to...
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...
2
by: John | last post by:
Hi I am getting the 'Operator is not valid for type 'DBNull' and string "".' error on the following line; If (mydatatable.Rows.Item(I).Item("Forenames") Is System.DBNull.Value) Then ' 'I' is...
1
by: SonyMan | last post by:
HI I have a VB.NET application in which my Image (from a picturebox) is opened by the user in another application like MSPaint or Photoshop or whatever he chooses. The image does open there.But...
1
by: bjjnova | last post by:
I have the following string comparison that is throwing an error I cannot find ( I will include my attempts to trace the error) In the line following the asterisks, written as I have below, a...
2
by: labby | last post by:
I've application to capture video of image and grab the still image. It runs well untill I put loop to regrab the still images many times automatically.. and the error (generic error occured GDI+)...
2
by: chazzy69 | last post by:
Hi, im just trying to compare a character(char) to a set list of characters (e.g. "a", "b", etc). first i read in the value- (i have tried this two ways) scanf("%c", &var_name); //also tried...
5
by: mnflint | last post by:
I am having a problem resizing multiple images at a time. The images are 13200x20394 1bpp png images. In my sample set of 10 I can proess 4 before I get an "Out of memory" error. I am running...
0
by: ShruthiR | last post by:
I am using ubuntu 10.10. In my rails 3.1 project, i am not able to upload any type of image files. While uploading an image file it shows an validation error like /tmp/image1.jpg is not recognized...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.