IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Multimédia Java Discussion :

[JMF] Capture Webcam & createRealizedPlayer


Sujet :

Multimédia Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Janvier 2007
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 10
    Points : 5
    Points
    5
    Par défaut [JMF] Capture Webcam & createRealizedPlayer
    Bonjour,

    j'ai un problème avec un bout de code en Java...

    Mon objectif est de capturer une image à partir d'une webcam et de l'enregistrer sur le disque.

    Dans mon cas, il ne s'agit pas vraiment d'une webcam, mais d'un adaptateur S-Video->USB2.0 , mais qui au final se comporte exactement comme une webcam (périphérique de capture WDM), ou presque...

    J'ai trouvé ce code sur le net.
    Je l'ai légèrement modifié entre les 10ème et 12ème ligne du Constructeur, mais au final je rencontre toujours le même problème qu'avec le code original: ça coince au niveau du createRealizedProcess(), et si je débranche puis rebranche la prise USB, tout se lance normalement...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
     
    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);
     
            try
            {
                CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
                Player player = Manager.createRealizedPlayer(deviceInfo.getLocator());
                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");
     
            f.addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        playerclose();
                        System.exit(0);
                    }
                });
     
            SwingCapture cf = new SwingCapture();
     
            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");
            }
        }
     
    }
    J'ai également essayé le code trouvé sur cette page: http://www.uk-dave.com/bytes/java/jmf-framegrab.php qui me paraît plus simple et plus propre, mais j'obtiens toujours le même problème, à savoir que le process bloque sur le createRealizedPlayer() tant que je ne débranche puis rebranche pas l'adaptateur USB, mais après tout fonctionne...

    j'ai essayé en remplaçant createRealizedPlayer() par createPlayer() + player.realize() mais ça bloque toujours sur le createPlayer()...


    J'ai remarqué que ça me fait le même problème dans le JMF registry editor, si je clique sur le bouton "Detect Capture Device", le process de détection bloque tant que je ne débranche-rebranche pas l'adaptateur USB... ça pourrait venir du périphérique ?

    enfin, je suis coincé
    si quelqu'un a une idée...

    je pense tester avec une webcam classique, mais j'en ai pas sous la main

  2. #2
    Futur Membre du Club
    Inscrit en
    Janvier 2007
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Bon, ben, je me réponds tout seul, le problème a changé, ça fonctionne bien , mais c'est trèèèèèèèèèèèès lent...

    en gros si je laisse tourner le prog plusieurs minutes sans réaction apparente et plus il finit par s'y connecter

    donc c'est (à moitié) résolu...

    y'a plus qu'à comprendre pourquoi retirer-remettre la prise accélère les choses

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 5
    Points : 6
    Points
    6
    Par défaut Capture necessitant rebranchement
    Petite idée: Si tu es sous windows, essaie alors ton bout de code java sous un linux récent (chez un copain ?) gérant bien le montage live de périphériques usb, peut être y aura t-il du mieux, en tout
    cas cela te dira si le pb vient du driver/pseudo driver windows ou non.

    Si tu souhaites essayer sous linux, les infos des sites V4L (video for linux = http://www.exploits.org/v4l/) pourront certainement t'être utiles en ce qui concerne la connaissance du support de ta "webcam" ou pas.

  4. #4
    Invité
    Invité(e)
    Par défaut [JMF] Capture Webcam & createRealizedPlayer
    Je ne sais pas si ton problème a été résolu ou non.
    De mon côté ça marche modulo quelques modifications à ton programme.
    La variable player dans dans le constructeur de SwingCapture masquait celle statique de ton programme.

    Ci dessous ma version de ton programme corrigée.

    A+

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
     
    /**
     * $Id:$
     * 
     * $Log:$
     *
     */
     
    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 {
    	class ImagePanel extends Panel {
    		public Image myimg = null;
     
    		public ImagePanel() {
    			setLayout(null);
    			setSize(320, 240);
    		}
     
    		@Override
    		public void paint(Graphics g) {
    			if (myimg != null) {
    				g.drawImage(myimg, 0, 0, this);
    			}
    		}
     
    		public void setImage(Image img) {
    			this.myimg = img;
    			repaint();
    		}
    	}
     
    	public static void main(String[] args) {
    		Frame f = new Frame("SwingCapture");
     
    		final SwingCapture cf = new SwingCapture();
     
    		f.addWindowListener(new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				cf.playerclose();
    				System.exit(0);
    			}
    		});
     
    		f.add("Center", cf);
    		f.pack();
    	//	f.setSize(new Dimension(320, 550));
    		f.setVisible(true);
    	}
     
    	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);
    			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");
    			}
    		} catch (java.io.FileNotFoundException io) {
    			System.out.println("File Not Found");
    		}
    	}
     
    	public 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);
     
    		try {
    			CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
    			player = Manager.createRealizedPlayer(deviceInfo.getLocator());
    			player.start();
    			Component comp = player.getVisualComponent();
    			if (comp != null) {
    				add(comp, BorderLayout.NORTH);
    			}
    			add(capture, BorderLayout.CENTER);
    			add(imgpanel, BorderLayout.SOUTH);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	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, "test.jpg");
    		}
    	}
     
    	public void playerclose() {
    		player.close();
    		player.deallocate();
    	}
     
    }

  5. #5
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2011
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Février 2011
    Messages : 71
    Points : 48
    Points
    48
    Par défaut
    Je suis sous le dernier Ubuntu
    les driver de la cam sont reconnu par cheese en tout cas

    j'ai le message d'erreur suivant

    Description Resource Path Location Type
    Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library /usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar SwingCapture.java /ter/src/paquet line 81 Java Problem

    des idées?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 07/03/2014, 12h18
  2. [VB.NET]Capture Webcam WIA
    Par shinji_rem dans le forum Windows Forms
    Réponses: 11
    Dernier message: 03/08/2012, 13h13
  3. Capture webcam JMF
    Par undasight dans le forum Multimédia
    Réponses: 0
    Dernier message: 14/03/2011, 03h42
  4. Capture WebCam avec Silverlight ?
    Par Invité dans le forum Silverlight
    Réponses: 5
    Dernier message: 07/12/2009, 14h07
  5. [VB6] Capture Webcam
    Par Vesta dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 07/02/2007, 14h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo