Accueil > > > CAPTURE DE PHOTOS À PARTIR D'UNE WEBCAM
CAPTURE DE PHOTOS À PARTIR D'UNE WEBCAM
Information sur la source
Description
Ca faisait longtemps que je cherchait, mais j'ai enfin trouvé ! Ce code permet de prendre une photos à partir d'une webcam et de l'enregistrer sur votre disque dur. Je met le code tel que je l'ai trouvé (après vérifications, bien entendu). Je tiens à préciser que ce code n'est pas de moi. Il y a besoin du JMF ( Java Media Framework ), que l'on peut trouver sur le site de sun : http://java.sun.com/products/java-media/jmf/
Source
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.image.BufferedImage;
- import java.io.FileOutputStream;
-
- import javax.media.Buffer;
- import javax.media.CaptureDeviceInfo;
- import javax.media.CaptureDeviceManager;
- import javax.media.Manager;
- import javax.media.MediaLocator;
- import javax.media.Player;
- import javax.media.control.FrameGrabbingControl;
- import javax.media.format.VideoFormat;
- import javax.media.util.BufferToImage;
- import javax.swing.JButton;
- import javax.swing.JComponent;
-
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
- public class SwingCapture extends Panel implements ActionListener
- {
- public static Player player = null;
- public CaptureDeviceInfo di = null;
- public MediaLocator ml = null;
- public JButton capture = null;
- public Buffer buf = null;
- public Image img = null;
- public VideoFormat vf = null;
- public BufferToImage btoi = null;
- public ImagePanel imgpanel = null;
-
- public SwingCapture()
- {
- setLayout(new BorderLayout());
- setSize(320,550);
-
- imgpanel = new ImagePanel();
- capture = new JButton("Capture");
- capture.addActionListener(this);
-
- String str1 = "vfw:Logitech USB Video Camera:0";
- String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
- di = CaptureDeviceManager.getDevice(str2);
- ml = new MediaLocator("vfw://0");
-
- try
- {
- player = Manager.createRealizedPlayer(ml);
- player.start();
- Component comp;
-
- if ((comp = player.getVisualComponent()) != null)
- {
- add(comp,BorderLayout.NORTH);
- }
- add(capture,BorderLayout.CENTER);
- add(imgpanel,BorderLayout.SOUTH);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
-
- public static void main(String[] args)
- {
- Frame f = new Frame("SwingCapture");
- SwingCapture cf = new SwingCapture();
-
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- playerclose();
- System.exit(0);}});
-
- f.add("Center",cf);
- f.pack();
- f.setSize(new Dimension(320,550));
- f.setVisible(true);
- }
-
-
- public static void playerclose()
- {
- player.close();
- player.deallocate();
- }
-
-
- public void actionPerformed(ActionEvent e)
- {
- JComponent c = (JComponent) e.getSource();
-
- if (c == capture)
- {
- // Grab a frame
- FrameGrabbingControl fgc = (FrameGrabbingControl)
- player.getControl("javax.media.control.FrameGrabbingControl");
- buf = fgc.grabFrame();
-
- // Convert it to an image
- btoi = new BufferToImage((VideoFormat)buf.getFormat());
- img = btoi.createImage(buf);
-
- // show the image
- imgpanel.setImage(img);
-
- // save image
- saveJPG(img,"c:\\test.jpg");
- }
- }
-
- class ImagePanel extends Panel
- {
- public Image myimg = null;
-
- public ImagePanel()
- {
- setLayout(null);
- setSize(320,240);
- }
-
- public void setImage(Image img)
- {
- this.myimg = img;
- repaint();
- }
-
- public void paint(Graphics g)
- {
- if (myimg != null)
- {
- g.drawImage(myimg, 0, 0, this);
- }
- }
- }
-
-
- public static void saveJPG(Image img, String s)
- {
- BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
- Graphics2D g2 = bi.createGraphics();
- g2.drawImage(img, null, null);
-
- FileOutputStream out = null;
- try
- {
- out = new FileOutputStream(s);
- }
- catch (java.io.FileNotFoundException io)
- {
- System.out.println("File Not Found");
- }
-
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
- param.setQuality(0.5f,false);
- encoder.setJPEGEncodeParam(param);
-
- try
- {
- encoder.encode(bi);
- out.close();
- }
- catch (java.io.IOException io)
- {
- System.out.println("IOException");
- }
- }
-
- }
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JComponent;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class SwingCapture extends Panel implements ActionListener
{
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public JButton capture = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{
setLayout(new BorderLayout());
setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = new MediaLocator("vfw://0");
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null)
{
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.CENTER);
add(imgpanel,BorderLayout.SOUTH);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame f = new Frame("SwingCapture");
SwingCapture cf = new SwingCapture();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
playerclose();
System.exit(0);}});
f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
public static void playerclose()
{
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();
if (c == capture)
{
// Grab a frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
// save image
saveJPG(img,"c:\\test.jpg");
}
}
class ImagePanel extends Panel
{
public Image myimg = null;
public ImagePanel()
{
setLayout(null);
setSize(320,240);
}
public void setImage(Image img)
{
this.myimg = img;
repaint();
}
public void paint(Graphics g)
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
public static void saveJPG(Image img, String s)
{
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io)
{
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
Historique
- 22 octobre 2004 13:52:03 :
- Cela fait plusieurs fois que l'on me pose des questions sur un problème, qui est causé par l'absence du JMF, alors je précise qu'il y en a besoin, et où on peut le télécharger.
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril SHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KITSHAREPOINT 15 TECHNICAL PREVIEW MANAGED OBJECT MODEL SOFTWARE DEVELOPMENT KIT par Matthew
http://www.microsoft.com/download/en/details.aspx?id=28768&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MicrosoftDownloadCenter+(Microsoft+Download+Center) ...
Cliquez pour lire la suite de l'article par Matthew
Forum
APPLICATION JARAPPLICATION JAR par yasseramiral
Cliquez pour lire la suite par yasseramiral
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|