Bonjour,
j'essaie de faire un applet qui affiche la webcam, mon code compilé et exécutée (Run as an applet) sous eclipse fonctionne trés bien mais quand je le déploie sur mon html ma frame n'affiche pas l'image alors que la webcam fonctionne (le témoin lumineux s'éclaire).
Etant un noob en applet jvien vous demander un peu d'aide, merci
voila le code:
l'applet:
la class pour gerer la webcam:
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 import java.awt.Graphics; import java.io.IOException; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JPanel; public class webcam extends JApplet { public void init() { DeviceHandler dh = null; try { dh = new DeviceHandler("vfw:Microsoft WDM Image Capture (Win32):0" ); long time = System.currentTimeMillis(); while (!dh.isReady()) {} JFrame jf = new JFrame("test" ); jf.setSize(640, 340); JPanel jp = new JPanel(); jp.setSize(100, 100); jf.getContentPane().add(jp); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Graphics gc = jp.getGraphics(); while (true) { if (System.currentTimeMillis() > time + 100) { time = System.currentTimeMillis(); gc.drawImage(dh.grabImage(), 0, 0, jp); //System.out.println("boucle"); } } } catch (IOException e) { e.printStackTrace(); } } }
la balise:
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 import java.awt.Image; import java.io.IOException; import javax.media.*; import javax.media.control.FrameGrabbingControl; import javax.media.format.VideoFormat; import javax.media.util.BufferToImage; public class DeviceHandler { private CaptureDeviceInfo device; private Player player; private FrameGrabbingControl frameGrabber; private Buffer buffer; private BufferToImage bufferUtil; private boolean ready; private int i; /** Creates a new instance of DeviceHandler */ public DeviceHandler(String deviceName) throws IOException {//throws Exception { this.ready = false; this.device = null; this.device = CaptureDeviceManager.getDevice(deviceName); //if (this.device == null) { throw new Exception("Device : \"" + deviceName + "\" not found !" ); } MediaLocator ml = this.device.getLocator(); //if (ml == null) { throw new Exception("Unable to find an appropriate media locator !" ); } this.player = null; try { this.player = Manager.createRealizedPlayer(ml); } catch (NoPlayerException ex) {}// throw new Exception("Unable to find an appropriate player !", ex); } catch (CannotRealizeException ex) {}// throw new Exception("Unable to get the player in realized state !", ex); } catch (IOException ex) {}// throw new Exception("Unable to create a player !", ex); } //if (this.player == null) { throw new Exception("Player is NULL !" ); } this.player.start(); while ((this.player.getState() & Player.Started) == 0) {} this.ready = true; this.frameGrabber = (FrameGrabbingControl)this.player.getControl("javax.media.control.FrameGrabbingControl" ); this.buffer = frameGrabber.grabFrame(); this.bufferUtil = new BufferToImage((VideoFormat)buffer.getFormat()); this.i = 0; } public Image grabImage() { this.i++; this.buffer = frameGrabber.grabFrame(); // juste pour tester /*if (this.buffer.getData() != null) System.out.println(this.buffer.getFormat().getEncoding() + " " + i);*/ this.bufferUtil = new BufferToImage((VideoFormat)buffer.getFormat()); return this.bufferUtil.createImage(buffer); } public boolean isReady() { return this.ready; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <object classid="java:webcam.class" codebase="bin" type="application/x-java-applet" width="300" height="80"> alt : <a href="bin/webcam.class">webcam.class</a> </object>
Partager