Peut etre un peu tard mais bon.. voici un extrait d'un de mes codes
Il faut installer la JMF (Java Media Framework, go ask google)
import javax.media.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
private Player playerWebcam = null;
private MediaLocator ml = new MediaLocator("vfw://1");
try {
playerWebcam = Manager.createRealizedPlayer(ml);
playerWebcam.start();
} catch(Exception e) {
System.out.println("Webcam not available");
}
if (playerWebcam != null) {
webcam = playerWebcam.getVisualComponent();
isCamReady = true;
} else {
webcam = new JLabel("Webcam not available");
isCamReady = false;
}
(webcam est un objet Component, donc affichable dans des interfaces :P, y a plus qu'a a jouter ce composant a une JFrame)
Vous aurez sans doute besoin de changer la ligne:
private MediaLocator ml = new MediaLocator("vfw://1"); (Windows uniquement, sous linux je ne sais pas ce qu'il faut mettre a la place de vfw://x Si quelqu'un sait, merci de me faire signe)
en
private MediaLocator ml = new MediaLocator("vfw://0");
ou autre
Mais la ca depend de votre config.
Prendre une photo a partir de la webcam:
java.awt.Image image = null;
while (image == null) {
FrameGrabbingControl fgc = (FrameGrabbingControl)playerWebcam.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = fgc.grabFrame();
// Convert it to an image
BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
image = btoi.createImage(buf);
try {
photoPath = new File(System.getenv("Temp") + "/photo" + System.currentTimeMillis() + ".jpg");
writeImage((BufferedImage)image,photoPath, 0.75f);
} catch(Exception ex) {
}
}
Pour enregistrer l'image:
public void writeImage(BufferedImage i, File f, float quality) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(f));
java.util.Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter)iter.next();
javax.imageio.plugins.jpeg.JPEGImageWriteParam iwp = (javax.imageio.plugins.jpeg.JPEGImageWriteParam)(writer.getDefaultWriteParam());
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
javax.imageio.stream.MemoryCacheImageOutputStream output = new javax.imageio.stream.MemoryCacheImageOutputStream(bos);
writer.setOutput(output);
IIOImage image2 = new IIOImage(i, null, null);
writer.write(null, image2, iwp);
}
bos.flush();
bos.close();
}
Voila, bon y a surement des erreurs de syntaxe dans ce code mais g la flemme de tout reprendre. (Copier Coller Powaaaaa :P)