大家好,我还是一个编程新手,我正面临一个代码问题,我希望有人能帮助我。我找到了下面的代码,用于叠加和比较在线论坛中两幅图像的像素。当我试图编译代码时,我得到了一个语法错误。希望你能帮我找到错误。提前谢谢。
代码:
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();
}
}
}
4条答案
按热度按时间7uhlpewt1#
什么错误?
iih3973s2#
代码根本不编译,只是说语法错误
这些是我得到的错误:
https://postimg.cc/gallery/fJZW2k5
gev0vcfq3#
我得到的三个代码错误是:
当尝试运行代码时,我看到一个窗口,显示“无法启动所选内容,并且最近没有启动”
在第一行代码“模块图像_覆盖{“am获取错误”的旁边,标记上的语法错误,错误放置的构造”
在代码行第7行旁边,am得到错误“java.awt.image.BufferedImage类型不可访问”
还有其他一些,但我认为这些是主要的。
bbmckpt74#
我可以编译#1中发布的代码(没有包,相同目录)
在#1中发布的代码不包含这样的行。
对话框可能特定于您正在使用的IDE。您可能需要参考其手册。
我的猜测是,对于IDE,在使用它们之前,需要在项目设置下手动配置/添加/提及所需的包。
由于一个根错误,可能会出现后续错误。