Quero parar meu while quando clicar o mouse ou pressionar enter… tem jeito?
Outra duvida minha é sobre Threads, estou querendo criar uma thread atualizando meu label tem jeito?
Abraços e fique com Deus!
Quero parar meu while quando clicar o mouse ou pressionar enter… tem jeito?
Outra duvida minha é sobre Threads, estou querendo criar uma thread atualizando meu label tem jeito?
Abraços e fique com Deus!
Bom dia Patrick.
Só não tem jeito para morte, rsrsr…
posta o código que ajudamos você.
Irmao, esta é a classe que abre a web cam
package abrircam;
/*
Programa para testar e abrir uma Web Cam via codigo
* Patrick Francis Gomes Rocha <[email removido]>
*/
import javax.swing.ImageIcon;
import abrircam.TesteForm;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.awt.PopupMenu;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AbrirCam extends Thread {
static ImageIcon imagem;
@Override
public void run(){
try {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start(); // abrindo o Diver da WebCam
IplImage frame = grabber.grab(); // mandando imagem da cam para variavel
PopupMenu teste = new PopupMenu();
CanvasFrame canvasFrame = new CanvasFrame("Abrir Cam"); // criando janela
canvasFrame.setCanvasSize(frame.width(), frame.height()); // tamanho da janela
canvasFrame.setLocation(0, 0);
canvasFrame.add(teste);
while (canvasFrame.isVisible() && frame != null && !Thread.interrupted()) { // testando enquanto naum fechar janela <<<<<< Aqui é a condição que eu quero
frame = grabber.grab(); // mandando imagem da cam para variavel
canvasFrame.showImage(frame); // janela blitando imagem da frame
imagem = new ImageIcon (frame.getBufferedImage());
}
grabber.stop(); // desligando cam
canvasFrame.dispose(); // fechando janela
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(AbrirCam.class.getName()).log(Level.SEVERE, null, ex);
}
}
// public static void main(String[] args) throws Exception {
// Thread thread = new AbrirCam();
// thread.start();
// }
public static ImageIcon Foto (){
return AbrirCam.imagem;
}
}
Esse é o Form que chama a Classe em Thread…
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TesteForm.java
*
* Created on 12/03/2012, 21:52:54
*/
package abrircam;
import java.awt.Image;
import javax.swing.ImageIcon;
/**
*
* @author Patrick Francis
*/
public class TesteForm extends javax.swing.JFrame {
Thread thread = null;
boolean sair = false;
ImageIcon icone = null;
/** Creates new form TesteForm */
public TesteForm() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Cam = new javax.swing.JLabel();
Chamar = new javax.swing.JToggleButton();
Foto = new javax.swing.JToggleButton();
jLayeredPane1 = new javax.swing.JLayeredPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Cam.setBorder(javax.swing.BorderFactory.createEtchedBorder());
Cam.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
CamMouseClicked(evt);
}
});
Cam.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
CamCaretPositionChanged(evt);
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
}
});
Cam.addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
CamAncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
Chamar.setText("CAM");
Chamar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ChamarActionPerformed(evt);
}
});
Foto.setText("OK");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(Chamar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Foto))
.addComponent(Cam, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(28, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(206, 206, 206)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(Cam, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Chamar)
.addComponent(Foto))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ChamarActionPerformed(java.awt.event.ActionEvent evt) {
thread = new AbrirCam();
thread.start();
}
private void CamCaretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
private void CamAncestorAdded(javax.swing.event.AncestorEvent evt) {
}
private void CamMouseClicked(java.awt.event.MouseEvent evt) {
if(thread.isAlive()){
ImageIcon foto = null;
foto = abrircam.AbrirCam.Foto();
Cam.setIcon(new ImageIcon(foto.getImage().getScaledInstance(Cam.getWidth(),Cam.getHeight(), Image.SCALE_DEFAULT)));
sair = true;
thread.interrupt();
}
//
// Cam.setIcon(new ImageIcon(icone.getImage().getScaledInstance(Cam.getWidth(),Cam.getHeight(), Image.SCALE_DEFAULT)));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TesteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TesteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TesteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TesteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TesteForm testeForm = new TesteForm();
testeForm.setLocationRelativeTo(null);
testeForm.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Cam;
private javax.swing.JToggleButton Chamar;
private javax.swing.JToggleButton Foto;
private javax.swing.JLayeredPane jLayeredPane1;
// End of variables declaration
}
Na verdade eu queria o seguinte: Ao inves de abrir uma janela ela jogasse em thread direto para minha label… só que eu não consigo!
Ela abre a imagem na cam, e dai como não deu certo com um click ou enter fechasse a tela da cam e captura o ultimo frame e joga no label, isso eu sei fazer… mais não sei colocar apertar enter, ou click… tem jeito?
Agora eu agradeceria muito se vc me desse um caminho para do thread eu atualizasse meu label… ai sim heim irmao~hahaha
Mais já agradeço pela disposição da ajuda… Foi dificil encontrar alguem aqui rsrsrs
Fico no aguardo