Created
January 21, 2016 09:47
-
-
Save Skyost/ace970290542eaec70de to your computer and use it in GitHub Desktop.
Project using webcam-capture (webcam-capture.sarxos.pl).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.hdelaunay.webcam; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import com.github.sarxos.webcam.Webcam; | |
import com.github.sarxos.webcam.WebcamPanel; | |
import com.github.sarxos.webcam.ds.ipcam.IpCamAuth; | |
import com.github.sarxos.webcam.ds.ipcam.IpCamDeviceRegistry; | |
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver; | |
import com.github.sarxos.webcam.ds.ipcam.IpCamMode; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.swing.GroupLayout; | |
import javax.swing.GroupLayout.Alignment; | |
import javax.swing.JButton; | |
import javax.swing.JOptionPane; | |
import javax.swing.LayoutStyle.ComponentPlacement; | |
import javax.swing.filechooser.FileNameExtensionFilter; | |
import javax.swing.UIManager; | |
/** | |
* Classe mise sur https://gist.github.com/Skyost/ace970290542eaec70de. | |
*/ | |
public class WebcamProject extends JFrame { | |
private static final long serialVersionUID = 1L; | |
private static final String NAME = "IPCamera 4"; | |
private static final String IP = "http://192.168.0.145"; | |
private static final String FILE = "video2.mjpg"; | |
private static final String USERNAME = "admin"; | |
private static final String PASSWORD = ""; | |
private String back; | |
static { | |
Webcam.setDriver(new IpCamDriver()); | |
} | |
public WebcamProject(final Webcam webcam) { | |
final String name = this.getClass().getName(); | |
this.setTitle(name.substring(name.lastIndexOf(".") + 1)); | |
this.setSize(312, 302); | |
this.setLocationRelativeTo(null); | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
final WebcamPanel panel = new WebcamPanel(webcam); | |
panel.setFPSLimit(1); | |
final JButton btnTakeSnapshot = new JButton("Prendre une photo..."); | |
btnTakeSnapshot.addActionListener(new ActionListener() { | |
@Override | |
public final void actionPerformed(final ActionEvent event) { | |
final BufferedImage image = webcam.getImage(); | |
final JFileChooser chooser = new JFileChooser(); | |
chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); | |
chooser.setFileFilter(new FileNameExtensionFilter("Image PNG (*.png)", "png")); | |
chooser.setMultiSelectionEnabled(false); | |
if(chooser.showSaveDialog(WebcamProject.this) == JFileChooser.APPROVE_OPTION) { | |
try { | |
String path = chooser.getSelectedFile().getPath(); | |
if(!path.endsWith(".png")) { | |
path += ".png"; | |
} | |
ImageIO.write(image, "PNG", new File(path)); | |
} | |
catch(final Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
}); | |
final JButton btnRecordVideo = new JButton("Enregistrer la vid\u00E9o..."); | |
btnRecordVideo.addActionListener(new ActionListener() { | |
@Override | |
public final void actionPerformed(final ActionEvent event) { | |
if(back == null) { | |
back = btnRecordVideo.getText(); | |
btnTakeSnapshot.setEnabled(false); | |
btnRecordVideo.setText("Arrêter l'enregistrement"); | |
JOptionPane.showMessageDialog(WebcamProject.this, "Bientôt !"); | |
} | |
else { | |
btnTakeSnapshot.setEnabled(true); | |
btnRecordVideo.setText(back); | |
back = null; | |
} | |
} | |
}); | |
final GroupLayout groupLayout = new GroupLayout(getContentPane()); | |
groupLayout.setHorizontalGroup( | |
groupLayout.createParallelGroup(Alignment.LEADING) | |
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(btnTakeSnapshot, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(btnRecordVideo) | |
.addContainerGap()) | |
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 296, Short.MAX_VALUE) | |
); | |
groupLayout.setVerticalGroup( | |
groupLayout.createParallelGroup(Alignment.LEADING) | |
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup() | |
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE) | |
.addPreferredGap(ComponentPlacement.UNRELATED) | |
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) | |
.addComponent(btnTakeSnapshot) | |
.addComponent(btnRecordVideo)) | |
.addContainerGap()) | |
); | |
getContentPane().setLayout(groupLayout); | |
} | |
public static final void main(final String[] args) { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
IpCamDeviceRegistry.register(NAME, IP + "/" + FILE, IpCamMode.PUSH, new IpCamAuth(USERNAME, PASSWORD)); | |
new WebcamProject(Webcam.getWebcams().get(0)).setVisible(true); | |
} | |
catch(final Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment