/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package detection;
import detection.javacv.Detect;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
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.JTextArea;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author duo
*/
public class ControlCenter extends JFrame implements ActionListener {
JPanel mainPanel;
JLabel detected;
JToolBar tool = new JToolBar();
JButton detectButton, chooseXmlDetectionButton, chooseFileButton, exitButton;
JFileChooser chooser;
FileNameExtensionFilter filter;
protected String choosenFile = null;
protected String detectionXmlFile;
JTextArea info;
public ControlCenter() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Detecção de Pessoas");
chooseXmlDetectionButton = new JButton("XML");
chooseXmlDetectionButton.setToolTipText("Escolha o arquivo xml de cascata de detecção.");
tool.add(chooseXmlDetectionButton);
chooseFileButton = new JButton("Imagem");
chooseFileButton.setToolTipText("Escolha o arquivo de imagem para ser detectado.");
tool.add(chooseFileButton);
detectButton = new JButton("Detectar");
tool.add(detectButton);
exitButton = new JButton("Sair");
tool.add(exitButton);
detected = new JLabel();
detected.setAlignmentX(CENTER_ALIGNMENT);
info = new JTextArea("nenhuma imagem carregada.");
mainPanel = new JPanel(new BorderLayout(10, 10), true);
mainPanel.add(info, BorderLayout.NORTH);
mainPanel.add(detected, BorderLayout.WEST);
mainPanel.add(tool, BorderLayout.SOUTH);
this.getContentPane().add(mainPanel);
adjustSize();
this.setVisible(true);
}
public final void adjustSize() {
this.pack();
this.setMinimumSize(new Dimension(800, 480));
}
public String chooseFile() {
chooser = new JFileChooser();
filter = new FileNameExtensionFilter(
"JPG, PNG & GIF Images", "jpg", "jpeg", "gif", "png");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String choosenFileName = chooser.getSelectedFile().getAbsolutePath();
System.out.println("You have decided to open this file: "
+ choosenFileName);
try {
ImageIcon image = new ImageIcon(choosenFileName);
detected.setIcon(image);
setChoosenFile(choosenFileName);
info.setText("arquivo escolhido: " + choosenFile);
info.append("\n" + image.getDescription());
} catch (Exception e) {
System.out.println("Erro carregando imagem: " + e);
info.setText("nenhuma imagem carregada.");
setChoosenFile(null);
}
adjustSize();
return chooser.getSelectedFile().getAbsolutePath();
}
return null;
}
public void chooseXmlDetectionFile() {
setDetectionXmlFile(null);
String path = new File(".").getAbsolutePath();
System.out.println("path: " + path);
chooser = new JFileChooser(path + "/resources");
filter = new FileNameExtensionFilter(
"Haarscascade xml detection", "xml");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
setDetectionXmlFile(chooser.getSelectedFile().getAbsolutePath());
System.out.println("You have decided to cascade: "
+ getDetectionXmlFile());
}
}
public void setDetected(ImageIcon image) {
try {
detected.setIcon(image);
detected.setText("");
} catch (Exception e) {
System.out.println("Erro carregando imagem: " + e);
detected = new JLabel("nenhuma imagem carregada.");
}
adjustSize();
}
public String getChoosenFile() {
return choosenFile;
}
public void setChoosenFile(String choosenFile) {
this.choosenFile = choosenFile;
}
/**
* Get the value of detectionXmlFile
*
* @return the value of detectionXmlFile
*/
public String getDetectionXmlFile() {
return detectionXmlFile;
}
/**
* Set the value of detectionXmlFile
*
* @param detectionXmlFile new value of detectionXmlFile
*/
public void setDetectionXmlFile(String detectionXmlFile) {
this.detectionXmlFile = detectionXmlFile;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase(chooseFileButton.getText())) {
chooseFile();
} else if(e.getActionCommand().equalsIgnoreCase(exitButton.getText())) {
System.exit(0);
} else if(e.getActionCommand().equalsIgnoreCase(detectButton.getText())) {
if (choosenFile!=null && !choosenFile.isEmpty() &&
getDetectionXmlFile()!=null && !getDetectionXmlFile().isEmpty()) {
Detect facesDetect = new Detect();
ImageIcon image = facesDetect.detectionExecute(choosenFile, getDetectionXmlFile());
setDetected(image);
info.setText("resultado: " + facesDetect.getDetectionResult() +
" ocorrencia(s) detectada(s)");
info.append("\n" + "arquivo de cascata: " + getDetectionXmlFile());
info.append("\n" + "arquivo escolhido: " + choosenFile);
info.append("\n" + image.getDescription());
} else {
System.out.println("Não há arquivo carregado para detectar!");
}
} else if(e.getActionCommand().equalsIgnoreCase(chooseXmlDetectionButton.getText())) {
chooseXmlDetectionFile();
}
}
public static void main(String[] args) {
ControlCenter controlCenter = new ControlCenter();
controlCenter.chooseFileButton.addActionListener(controlCenter);
controlCenter.detectButton.addActionListener(controlCenter);
controlCenter.chooseXmlDetectionButton.addActionListener(controlCenter);
controlCenter.exitButton.addActionListener(controlCenter);
}
}