📄 mainwindow.java
字号:
JButton color_b = new JButton("Track Color");
tc_p.add(color_b);
color_b.addActionListener(this);
JButton tw_b = new JButton("Track Window");
tc_p.add(tw_b);
tw_b.addActionListener(this);
colb_p.add("Center", col_p);
colb_p.add("South", tc_p);
color_p.add("Center", myColor);
color_p.add("South", colb_p);
/**************************** Motion Tracking JPanel ***************/
myMotion = new Motion();
JPanel motion_p = new JPanel();
JPanel mot_p = new JPanel();
mot_p.add(new JLabel("FPS"));
mFPS_l=new JLabel("0");
mot_p.add(mFPS_l);
mot_p.add(new JLabel(" "));
motion_p.setLayout(new BorderLayout());
thresh_t = new JTextField("15", 5);
mot_p.add(new JLabel("Threshold:"));
mot_p.add(thresh_t);
JButton loadframe_b = new JButton("Load Frame");
mot_p.add(loadframe_b);
loadframe_b.addActionListener(this);
JButton motion_b = new JButton("Frame Diff");
mot_p.add(motion_b);
motion_b.addActionListener(this);
motion_p.add("Center", myMotion);
motion_p.add("South", mot_p);
/******************************* Main Image JPanel *****************/
cImage = new CameraImage(352, 288);
JPanel ctmp_p = new JPanel();
JPanel dtmp_p = new JPanel();
sendFrame_b = new JButton("Grab Frame");
saveFrame_b = new JButton("Save Frame");
frameProg = new JProgressBar(0, 144);
frameProg.setValue(0);
frameProg.setStringPainted(true);
frameProg.setOrientation(JProgressBar.HORIZONTAL);
frameProg.setStringPainted(false);
sf_chan_c = new JComboBox();
sf_chan_c.addItem("All");
sf_chan_c.addItem("red");
sf_chan_c.addItem("green");
sf_chan_c.addItem("blue");
sendFrame_b.addActionListener(this);
saveFrame_b.addActionListener(this);
ctmp_p.add(sf_chan_c);
ctmp_p.add(sendFrame_b);
ctmp_p.add(saveFrame_b);
// Make menus and such
JTabbedPane tabPane1 = new JTabbedPane();
//JPanel motion_p = new JPanel();
menuBar = new JMenuBar();
JMenu file_m = new JMenu("File");
file_m.add(makeMenuItem("About"));
file_m.add(makeMenuItem("Quit"));
images_m = new JMenu("Image", true);
images_m.add(makeMenuItem("Flip Vertical"));
images_m.add(makeMenuItem("Flip Horizontal"));
images_m.add(makeMenuItem("Red Channel"));
images_m.add(makeMenuItem("Green Channel"));
images_m.add(makeMenuItem("Blue Channel"));
images_m.add(makeMenuItem("All Channels"));
JPanel main_p = new JPanel();
menuBar.add(file_m);
menuBar.add(images_m);
main_f.setJMenuBar(menuBar);
// main_f.add("Center",cImage);
main_p.setLayout(new BorderLayout());
main_p.add("Center", cImage);
JPanel argh = new JPanel();
GridLayout kl = new GridLayout(2, 1);
argh.setLayout(kl);
argh.add(select_p);
argh.add(ctmp_p);
dtmp_p.setLayout(new BorderLayout());
dtmp_p.add(argh, "Center");
dtmp_p.add(frameProg, "South");
JPanel1.add(dtmp_p);
main_p.add("South", JPanel1);
tabPane1.addTab("Camera View", main_p);
tabPane1.addTab("Config", settingspad_p);
tabPane1.addTab("Color", color_p);
tabPane1.addTab("Motion", motion_p);
tabPane1.addTab("Histogram", histogram_p);
tabPane1.addTab("Stats", statistics_p);
tabPane1.addTab("Servo", servo_p);
tabPane1.setSelectedIndex(0);
tabPane1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
stopCameraStuff();
}
});
JPanel topPain_p = new JPanel();
topPain_p.add(new JLabel("Console:"));
status_t = new JTextField(25);
topPain_p.add(status_t);
JButton stop_b = new JButton("STOP");
topPain_p.add(stop_b);
stop_b.addActionListener(this);
JButton reset_b = new JButton("RESET");
topPain_p.add(reset_b);
reset_b.addActionListener(this);
JButton clr_command_b = new JButton("Clear");
topPain_p.add(clr_command_b);
clr_command_b.addActionListener(this);
JButton send_command_b = new JButton("Send");
topPain_p.add(send_command_b);
send_command_b.addActionListener(this);
main_f.getContentPane().add(topPain_p, "North");
main_f.getContentPane().add(tabPane1, "Center");
//main_f.setSize(500, 620);
main_f.pack();
main_f.setVisible(true);
/* updateThread=new Thread(new Runnable() {
public void run() {pollCrl();}
});
updateThread.start();
*/
mySerial = new CameraSerial(cWindow.getPort());
if (cameraType == 6) {
x1.setText("1");
y1.setText("1");
x2.setText("87");
y2.setText("142");
} else {
x1.setText("1");
y1.setText("1");
x2.setText("159");
y2.setText("238");
}
commandVal = 0;
while (true) {
if (commandVal == 1)
myMotion.trackMotion(hd_c.getSelectedIndex());
else if (commandVal == 2)
myHistogram.getHistogram();
else if (commandVal == 3)
myColor.trackColor();
else if (commandVal == 4)
myStats.getMean();
else if (commandVal == 5 || commandVal == 6)
cImage.sendFrame(sf_chan_c.getSelectedIndex());
else if (commandVal == -1) {
stopCameraStuff();
} else {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
}
/*private void pollCrl()
{
FileInputStream fi;
int tempVal=-1,cnt;
while(true)
{
int new_file=1;
String fname = new String( "img_log/image" + file_cnt + ".ppm");
try{
fi = new FileInputStream(fname);
fi.close();
}catch(Exception e) {new_file=0;}
if(new_file==1)
{
images_m.add(makeMenuItem(fname));
tempVal=cImage.loadImage(fname);
file_cnt++;
}
try{
Thread.sleep(1);
} catch(InterruptedException e) {}
}
}*/
/*
This is where the menus do clean up and set the commandVal value.
*/
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (commandVal != 0) {
commandVal = -1;
while (commandVal == -1)
try {
Thread.sleep(100);
} catch (InterruptedException d) {
}
} else if (command.equals("Flip Horizontal")) {
cImage.flip(0);
if (cImage.flipx == 0)
cImage.flipx = 1;
else
cImage.flipx = 0;
} else if (command.equals("Flip Vertical")) {
cImage.flip(1);
if (cImage.flipy == 0)
cImage.flipy = 1;
else
cImage.flipy = 0;
}
else if (command.equals("Red Channel"))
cImage.setChannel(0);
else if (command.equals("Green Channel"))
cImage.setChannel(1);
else if (command.equals("Blue Channel"))
cImage.setChannel(2);
else if (command.equals("All Channels"))
cImage.setChannel(4);
else if (command.equals("Quit"))
System.exit(0);
else if (command.equals("About")) {
myAbout.about_f.pack();
myAbout.about_d.setVisible(true);
} else if (command.equals("STOP")) {
status_t.setText("Stopping...");
commandVal = -1;
} else if (command.equals("Send")) {
mySerial.write(status_t.getText() + "\r");
status_t.setText("Command Sent");
} else if (command.equals("Config Servos")) {
status_t.setText("Servo Configure Called");
mySerial.write(
"sp "
+ pan_range_far.getText()
+ " "
+ pan_range_near.getText()
+ " "
+ pan_step.getText()
+ " "
+ tilt_range_far.getText()
+ " "
+ tilt_range_near.getText()
+ " "
+ tilt_step.getText());
if (mySerial.readACK(1) == 1)
status_t.setText("Servos Set.");
else
status_t.setText("Servo Config Failed.");
} else if (command.equals("Clear")) {
status_t.setText("");
} else if (command.equals("RESET")) {
commandVal = 0;
as_c.setSelectedIndex(0);
hr_c.setSelectedIndex(0);
lm_c.setSelectedIndex(0);
whiteBalance.setSelectedIndex(0);
autoGain.setSelectedIndex(0);
color_c.setSelectedIndex(0);
bm_c.setSelectedIndex(0);
hd_c.setSelectedIndex(0);
ti_c.setSelectedIndex(0);
pd_c.setSelectedIndex(0);
dc_c.setSelectedIndex(1);
mySerial.readACK(0);
setWindowParams();
mySerial.write("rs\r");
mySerial.readACK(0);
status_t.setText("Camera Reset...");
} else if (command.equals("Grab Frame")) {
if (fs_c.getSelectedIndex() == 1) {
int chan = sf_chan_c.getSelectedIndex();
if (chan == 0)
mySerial.write("sf\r");
else {
mySerial.write("sf " + (chan - 1) + "\r");
}
commandVal = 5;
} else
commandVal = 6;
} else if (command.equals("Save Frame")) {
/*
JFileChooser chooser=new JFileChooser();
chooser.setSelectedFile(new File("default.jpg"));
int returnval=chooser.showSaveDialog(save);
if (returnval==JFileChooser.APPROVE_OPTION) {
cImage.writeImage( chooser.getSelectedFile().getName());
//trainCanvas.save(chooser.getSelectedFile().getName());
}*/
JFileChooser save_d = new JFileChooser();
int result =
save_d.showSaveDialog(
new JFrame("Type the name of the File to save"));
File saveFile = save_d.getSelectedFile();
if (result == JFileChooser.APPROVE_OPTION) {
cImage.writeImage(saveFile + ".jpg");
}
} else if (command.equals("clear")) {
cImage.objnum = 0;
cImage.my_drive = 0;
cImage.target_x = 0;
cImage.target_y = 0;
CameraImage.objDirt[0] = 0;
CameraImage.objDirt[1] = 0;
CameraImage.objDirt[2] = 0;
cImage.repaint();
} else if (command.equals("Get Mean")) {
status_t.setText("Get Mean");
if (mySerial.readACK(0) == 0) {
status_t.setText("Get Mean Failed");
return;
}
mySerial.write("gm\r");
MainWindow.status_t.setText("Get Mean ");
commandVal = 4;
} else if (command.equals("read frame")) {
mySerial.write("rf");
if (mySerial.readACK(0) == 0) {
MainWindow.status_t.setText("Read Frame Failed");
return;
}
MainWindow.status_t.setText("New Frame Loaded...");
} else if (command.equals("Load Frame")) {
if (mySerial.readACK(0) == 0) {
status_t.setText("Load Frame Failed");
return;
}
mySerial.write("lf");
if (frame_diff_linemode == 3) {
mySerial.write("\r");
myMotion.trackMotion(hd_c.getSelectedIndex());
} else if (mySerial.readACK(1) == 0) {
MainWindow.status_t.setText("Load Frame Failed");
return;
}
MainWindow.status_t.setText("Frame Loaded...");
} else if (command.equals("Set Window")) {
status_t.setText("Set Virtual Window Called");
mySerial.write(
"vw "
+ x1.getText()
+ " "
+ y1.getText()
+ " "
+ x2.getText()
+ " "
+ y2.getText());
if (mySerial.readACK(1) == 1)
status_t.setText("Virtual Window Set.");
else
status_t.setText("Virtual Window Failed.");
} else if (command.equals("Down Sample")) {
status_t.setText("Down Sampling Called");
mySerial.write("ds " + dsx.getText() + " " + dsy.getText());
if (mySerial.readACK(1) == 1)
status_t.setText("Down Sampling Set.");
else
status_t.setText("Down Sampling Failed.");
} else if (command.equals("Track Color")) {
status_t.setText("Track Color Called");
if (mySerial.readACK(0) == 0) {
status_t.setText("Tracking Failed");
return;
}
mySerial.write(
"tc "
+ rmin_t.getText()
+ " "
+ rmax_t.getText()
+ " "
+ gmin_t.getText()
+ " "
+ gmax_t.getText()
+ " "
+ bmin_t.getText()
+ " "
+ bmax_t.getText()
+ "\r");
MainWindow.status_t.setText("Tracking Color ");
commandVal = 3;
} else if (command.equals("Track Window")) {
status_t.setText("Track Window Called");
if (mySerial.readACK(0) == 0) {
status_t.setText("Tracking Failed");
return;
}
mySerial.write("tw\r");
setWindowParams();
MainWindow.status_t.setText("Tracking Window ");
commandVal = 3;
} else if(command.equals("Update ALL"))
{
UpdateAllCommands();
} else if (command.equals("Set NF")) {
status_t.setText("Setting Noise Filter");
mySerial.write("nf " + nf_t.getText());
if (mySerial.readACK(1) == 1)
status_t.setText("Noise Filter Set.");
else
status_t.setText("Noise Filter Failed.");
} else if (command.equals("Frame Diff")) {
status_t.setText("Frame Diff Called");
if (mySerial.readACK(0) == 0) {
status_t.setText("Tracking Failed");
return;
}
mySerial.write("fd " + thresh_t.getText() + "\r");
MainWindow.status_t.setText("Motion Track " + thresh_t.getText());
commandVal = 1;
} else if (command.equals("Get Histogram")) {
status_t.setText("Get Histogram");
if (mySerial.readACK(0) == 0) {
status_t.setText("Histogram Failed");
return;
}
mySerial.write("gh " + channel_c.getSelectedIndex() + "\r");
MainWindow.status_t.setText(
"Get Histogram " + channel_c.getSelectedIndex());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -