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. - package stackexchange;
-
-
import java.awt.Color;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
-
public class PictureOverlayTest {
-
/*
-
* Four variables, three for the wanted BufferedImages, one String for the
-
* Path of the third Image, which does not already exist.
-
*/`
-
-
private BufferedImage image1;
-
private BufferedImage image2;
-
private BufferedImage image3;
-
-
private String pathImage3;
-
-
public PictureOverlayTest(String filePathAndName1, String filePathAndName2,
-
String filePathAndName3) throws IOException {
-
/*
-
* Constructor in order to keep this method reusable and clean. Needs
-
* three Strings. The paths and Filenames of all three images. Image 1
-
* and 2 should exist already, Image 3 will be created if all
-
* requirements are met. Constructor creates the first two buffered
-
* images, sets all needed variables and starts the checkAndCompare()
-
* method
-
*/
-
-
File file = new File(filePathAndName1);
-
this.image1 = ImageIO.read(file);
-
-
file = new File(filePathAndName2);
-
this.image2 = ImageIO.read(file);
-
-
this.pathImage3 = filePathAndName3;
-
checkAndCompare();
-
}
-
-
private void checkAndCompare() throws IOException {
-
/*
-
* This function creates the Color blue, compares the sizes of both
-
* pictures and if they are the same, creates a third image. Then it
-
* loops through the two images and compares each pixel. If the pixels
-
* are the same, the third image gets a blue pixel at that point
-
*/
-
-
Color blue = Color.blue;
-
-
if (image1.getHeight() == image2.getHeight()
-
&& image1.getWidth() == image2.getWidth()) {
-
-
image3 = new BufferedImage(image1.getWidth(), image1.getHeight(),
-
image1.getType());
-
for (int y = 0; y < image1.getHeight(); y++) {
-
for (int x = 0; x < image1.getWidth(); x++) {
-
-
int colorImage1 = image1.getRGB(x, y);
-
int colorImage2 = image2.getRGB(x, y);
-
-
if (colorImage1 == colorImage2) {
-
-
image3.setRGB(x, y, blue.getRGB());
-
-
} else {
-
-
// Whatever Color you want. By default it is black.
-
-
}
-
-
}
-
}
-
savePicture3();
-
System.out.println("Message: Image comparison is done");
-
-
} else {
-
-
System.out.println("Error: Image dimensions do not match");
-
-
}
-
-
}
-
-
private void savePicture3() throws IOException {
-
/*
-
* This method saves the created Image into a file onto your computer.
-
* The if() statement is used to check if the file was successfully
-
* created, in order to avoid unwanted errors. Keep in mind, that you
-
* have to change the "bmp" in ImageIO.write() to whatever format you
-
* actually want
-
*/
-
-
File file = new File(pathImage3);
-
if (file.createNewFile()) {
-
ImageIO.write(image3, "bmp", file);
-
}
-
}
-
-
}
-
-
package stackexchange;
-
-
import java.io.IOException;
-
-
public class Main {
-
-
public static void main(String[] args) {
-
// TODO Auto-generated method stub
-
-
try {
-
PictureOverlayTest test = new PictureOverlayTest(
-
"H:\\stackexchange\\file1.bmp",
-
"H:\\stackexchange\\file2.bmp",
-
"H:\\stackexchange\\file3.bmp");
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
}
0 1990 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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;
|
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.
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |