Java 图像叠加/比较和像素颜色变化
我需要比较两个相似图像中的像素,创建保存的第三个图像,其中两个图像中相同的像素变为蓝色。在开始比较之前,代码还需要检查图像的大小是否相同,如果它们的大小不相同,代码将带着错误退出控制台 这是我的密码。我仍然需要覆盖这两幅图像,并将相似的像素变成蓝色 我需要使用Filechooser.pickAFile,但除此之外,我完全不知道如何使用它 卡尔·托马斯 //kthoma34 //周一@4:00 导入java.awt.Color; 公共类PP2KA34 { 公共静态void main(字符串[]args) { //原画 图片p1;//创建变量 字符串文件名=FileChooser.pickAFile(); FileChooser.setMediaPath(文件名); System.out.println(文件名); p1=新图片(文件名); //原始图片的宽度和长度 int width1=p1.getWidth(); int height1=p1.getHeight(); 像素[]像素阵列1=p1.getPixels(); System.out.println(像素阵列1.length+“像素”); System.out.println(“”); //修改后的图片 Picture p2;//创建变量 字符串文件名2; filename2=FileChooser.pickAFile(); FileChooser.setMediaPath(filename2); System.out.println(filename2); p2=新图片(文件名2); //操纵图片的宽度和高度 int width2=p2.getWidth(); int height2=p2.getHeight(); 像素[]像素阵列2=p2.getPixels(); System.out.println(pixelArray2.length+“像素”); System.out.println(“”); //检查图像的大小是否相同 如果((宽度1!=宽度2)| |(高度1!=高度2)) { System.err.println(“错误:图像尺寸不匹配”); 系统出口(1); } //保存第三张图像,以蓝色突出显示相似性。 字符串文件名3; filename3=FileChooser.pickAFile(); p2.写入(文件名3); }Java 图像叠加/比较和像素颜色变化,java,javascript,image,overlay,Java,Javascript,Image,Overlay,我需要比较两个相似图像中的像素,创建保存的第三个图像,其中两个图像中相同的像素变为蓝色。在开始比较之前,代码还需要检查图像的大小是否相同,如果它们的大小不相同,代码将带着错误退出控制台 这是我的密码。我仍然需要覆盖这两幅图像,并将相似的像素变成蓝色 我需要使用Filechooser.pickAFile,但除此之外,我完全不知道如何使用它 卡尔·托马斯 //kthoma34 //周一@4:00 导入java.awt.Color; 公共类PP2KA34 { 公共静态void main(字符串[]a
}//结束这将帮助您: 这是一个关于使用BuffereImage在映像中循环的回答线程,因此您应该能够在Java API中找到大量解释(关于使用和函数) 在回答线程中,它还显示了如何将图像的像素设置为特定颜色 请记住,所有关于该线颜色的东西都应该根据您的需要进行定制 也就是说,您必须首先创建具有正确测量值的第三张图片(同样使用BuffereImage),然后检查两张图片中所选像素的颜色,如果它们匹配,则第三张图片将相同像素设置为蓝色,如果不匹配,则按照您的想法执行 如果你需要更多的细节,只管说出来,我会看看我能为你做些什么 编辑:(16.03.2015) 下面是一个工人阶级,他们做的正是你想要的。我试着尽可能清楚地评论每件事,但根据经验,我知道这对其他人来说并不总是足够清楚。您可以随意以任何方式使用此代码 快速免责声明:您必须将包名更改为自己的包,并且需要在main.class中创建此类的对象。尽可能少地编码到主类中总是更漂亮、更高效 比较类
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();
}
}
}
看到这个唯一的问题是它使用了缓冲图像,而不是filchooser。我不是这方面的专家,在将其转化为有效的东西时遇到了很大的困难。我可以将一幅图像覆盖在另一幅图像上,现在我只需要将所有相同的像素变成蓝色。它们都是相同的图片,只是在一些地方进行了一些编辑。编辑了我的答案,希望我的代码示例能够帮助您我的编辑对您的问题有帮助吗?