Bonjour,

J'aimerais faire du traitement d'image dès que la webcam met à jour des images. J'ai bien essayé de lancer une thread parallèlement à player.start(); mais ça ne marche pas comme je veux... Voici le code qui marche (donc sans la thread)...

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
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.color.ColorSpace;
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.awt.image.BufferedImageOp;
import java.awt.image.ColorConvertOp;
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 javax.swing.JPanel;
 
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 {
	private static final long serialVersionUID = 1L;
 
	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 ImagePanel imgpanelout = null;
 
	public SwingCapture() {
		setLayout(new BorderLayout());
		imgpanel = new ImagePanel();
		imgpanelout = 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.WEST);
			}
 
			add(capture, BorderLayout.CENTER);
 
			JPanel out = new JPanel();
			out.add(imgpanel, BorderLayout.CENTER);
			out.add(imgpanelout, BorderLayout.SOUTH);
 
			add(out, BorderLayout.EAST);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	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 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);
 
			img = convertToGrayscale((BufferedImage) img);
			imgpanel.setImage(img);
			saveJPG(img, "test.jpg");
		}
	}
 
	public void playerclose() {
		player.close();
		player.deallocate();
	}
 
	public BufferedImage convertToGrayscale(BufferedImage source) {
		BufferedImageOp op = new ColorConvertOp(ColorSpace
				.getInstance(ColorSpace.CS_GRAY), null);
		return op.filter(source, null);
	}
 
	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.setVisible(true);
	}
}
Je réussis par exemple à traiter mon image quand je clique sur le bouton mais j'aimerais automatiser le processus...

Merci d'avance