62,614
社区成员
发帖
与我相关
我的任务
分享
package aaaa;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestDemo extends JFrame implements ActionListener {
JTextField txt1 = new JTextField(20);
JTextField txt2 = new JTextField(20);
// JFileChooser choose1 = new JFileChooser();//创建文件打开对话框
JButton btn1 = new JButton("打开图片1");
JButton btn2 = new JButton("打开图片2");
JButton btn3 = new JButton("RGB颜色值");
JButton btn4 = new JButton("16进制");
JFileChooser choose1 = new JFileChooser();//创建文件打开对话框
JLabel lbl1 = new JLabel("尚无照片1");
JLabel lbl2 = new JLabel("尚无照片2");
JTextArea area = new JTextArea(5, 18);//多行文本框
JScrollPane jsp1 = new JScrollPane(lbl1);
JScrollPane jsp2 = new JScrollPane(lbl2);
JScrollPane jsp3 = new JScrollPane(area);
JPanel panel = new JPanel();
Robot robot = null;
int num=0;
int[]bottle1;
int[]bottle2;
public TestDemo() {
super("吴健制作");
this.panel.setLayout(null);
this.txt1.setBounds(15, 10, 300, 20);
this.txt2.setBounds(15, 40, 300, 20);
this.btn1.setBounds(350, 10, 100, 20);
this.btn2.setBounds(350, 40, 100, 20);
this.jsp1.setBounds(15, 80, 175, 175);
this.jsp2.setBounds(250, 80, 175, 175);
this.btn3.setBounds(15, 285, 90, 20);
this.btn4.setBounds(15, 315, 90, 20);
jsp3.setBounds(130, 285, 260, 60);
// choose1.setDialogTitle("打开图片");
txt1.setEditable(false);
txt2.setEditable(false);
panel.add(txt1);
panel.add(btn1);
panel.add(txt2);
panel.add(btn2);
panel.add(jsp1);
panel.add(jsp2);
panel.add(btn3);
panel.add(btn4);
panel.add(jsp3);
btn1.addActionListener(this);//添加监听
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
this.getContentPane().add(panel);
this.setBounds(100, 100, 470, 400);//窗体起始坐标大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭的处理
this.setVisible(true);//窗体是否可看
this.setResizable(false);//窗体时候可以调整大小
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
this.choose1.showSaveDialog(null);// 弹出一个文件选择器对话框。
this.choose1.setDialogTitle("打开图片");
File file = this.choose1.getSelectedFile();//返回选中的文件。
if(file!=null){
txt1.setText(file.getAbsolutePath());
Icon icon= new ImageIcon(file.getAbsolutePath());
lbl1.setIcon(icon);//显示第一张图片
lbl1.setText("");//文本字符串
}else{
txt1.setText(null);
}
}
if (e.getSource() == btn2) {
this.choose1.showSaveDialog(null);
this.choose1.setDialogTitle("打开图片");
File file = this.choose1.getSelectedFile();
if(file!=null){
txt2.setText(file.getAbsolutePath());
Icon icon= new ImageIcon(file.getAbsolutePath());
lbl2.setIcon(icon);//显示第一张图片
lbl2.setText("");//文本字符串
}else{
txt1.setText(null);
}
}
if (e.getSource() == btn3) {
area.setText("");
int count = 0;
File file1 = new File(txt1.getText());
File file2 = new File(txt2.getText());
int width = new ImageIcon(file1.getAbsolutePath()).getIconWidth();
int height = new ImageIcon(file1.getAbsolutePath()).getIconHeight();
int width2 = new ImageIcon(file2.getAbsolutePath()).getIconWidth();
int height2 = new ImageIcon(file2.getAbsolutePath())
.getIconHeight();
if (width == width2 && height == height2) {
try {
BufferedImage bi1= ImageIO.read(file1);
BufferedImage bi2= ImageIO.read(file2);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int a = bi1.getRGB(i, j);//返回一张图片的RPG值
int b = bi2.getRGB(i, j);//第二张图片
String tmp1 = Integer.toHexString(a);
String tmp2 = Integer.toHexString(b);
bottle1[num++]=a;
bottle2[num++]=b;
if (a != b) {
tmp1 = tmp1.substring(tmp1.length() - 6);
tmp2 = tmp2.substring(tmp2.length() - 6);
area.append(this.getRGBColor(tmp1) + "\t"
+ this.getRGBColor(tmp2) + "\n"
);
count++;
}
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
area.append("共有" + count + "处不同\n");
} else {
area.append("图片大小不一样,请重新选择图片\n");
}
}
if (e.getSource() == btn4) {
area.setText("");
int count = 0;
File file1 = new File(txt1.getText());
File file2 = new File(txt2.getText());
int width = new ImageIcon(file1.getAbsolutePath()).getIconWidth();
int height = new ImageIcon(file1.getAbsolutePath()).getIconHeight();
int width2 = new ImageIcon(file2.getAbsolutePath()).getIconWidth();
int height2 = new ImageIcon(file2.getAbsolutePath())
.getIconHeight();
if (width == width2 && height == height2) {
try {
BufferedImage bi1 = ImageIO.read(file1);
BufferedImage bi2 = ImageIO.read(file2);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int a = bi1.getRGB(i, j);
int b = bi2.getRGB(i, j);
if (a != b) {
area.append(Integer.toHexString(a)
.toUpperCase()
+ "\t"
+ Integer.toHexString(b).toUpperCase()
+ "\n");
count++;
}
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
area.append("共有" + count + "处不同\n");
} else {
area.append("图片大小不一样,请重新选择图片\n");
}
}
}
private String getRGBColor(String src) {
int red = Integer.parseInt(src.substring(0, 2), 16);
int green = Integer.parseInt(src.substring(2, 4), 16);
int blue = Integer.parseInt(src.substring(4, 6), 16);
return "Ox" + Integer.toHexString(red).toUpperCase()
+ Integer.toHexString(green).toUpperCase()
+ Integer.toHexString(blue).toUpperCase();
}
public static void main(String[] args) {
new TestDemo();
}
}