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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| import java.awt.Component;
import java.awt.Dimension;
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 javax.swing.JFrame;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Fenetrewebcam extends Panel implements ActionListener {
public static Player lecteur = null;
public CaptureDeviceInfo cdi = 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 Fenetrewebcam() {
JFrame f = new JFrame("Webcam aMsky"); // Création d'une fenetre
setSize(320,550); // Définition de sa taille
imgpanel = new ImagePanel();
capture = new JButton("Capture"); // Création du bouton Capture
capture.addActionListener(this); // l'utilisation du bouton provoquera un événement
String driver = "vfw:Microsoft WDM Image Capture (Win32):0"; // Driver pour la webcam
cdi = CaptureDeviceManager.getDevice(driver); // Recherche des périphériques de capture disponibles sur le système
ml = new MediaLocator("vfw://0"); // l'emplacement du contenu du média (webcam)
try {
lecteur = Manager.createRealizedPlayer(ml); // Spécification du point d'accès à la vidéo.
lecteur.start();
Component comp;
if ((comp = lecteur.getVisualComponent()) != null) {
add(comp);
}
add(capture); // Ajoute le bouton Capture à l'interface
add(imgpanel);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
arretwebcam();
System.exit(0);}});
f.add("Center",this); // Positionnement de l'image de la webcam
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void arretwebcam() {
lecteur.close();
lecteur.deallocate(); // Pour libérer la mémoire allouée par la webcam.
}
int i =0;
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if (c == capture) {
CapturePhoto();
i=i+1;
}
}
public void CapturePhoto() {
FrameGrabbingControl fgc = (FrameGrabbingControl)
lecteur.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// Affichage de la capture sur l'interface
imgpanel.setImage(img);
// Chemin où est sauvegarder l'image
Jpegencode(img,"c:\\Photo webcam aMsky"+i+".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 Jpegencode(Image webcamimage,String aMsky) {
// création du buffer qui va contenir l'image
// getWidth et getHeight pour définir la longueur et largeur de l'image
// TYPE_INT_RGB représente une image en couleur de 8 bits
BufferedImage bufferImage = new BufferedImage(webcamimage.getWidth(null), webcamimage.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Graphics2D sert à manipuler l’image à travers la variable "graphimg"
// La méthode "drawImage(variable qui sert à stocker l'image,l'abscisse et l'ordonnée)" permet de tracer une image
Graphics2D graphimg = bufferImage.createGraphics();
graphimg.drawImage(webcamimage, null, null);
// Encodage JPEG de l'image
// la classe "FileOutputStream" permet l'écriture de flux telles que les octets de données d'image
FileOutputStream fluxsortie = null;
try {
fluxsortie = new FileOutputStream(aMsky);
} catch (java.io.FileNotFoundException io) { // Gestion des exceptions en cas de fichier introuvable
System.out.println("File Not Found");
}
// Encode le flux de données de "ByteArrayOutputStream()" en JPEG
JPEGImageEncoder jpegencoder = JPEGCodec.createJPEGEncoder(fluxsortie);
// Définition des paramètres de l'image JPEG
JPEGEncodeParam jpegparam = jpegencoder.getDefaultJPEGEncodeParam(bufferImage);
// Réglage la qualité de l'image, 0.75 : haute qualité, 0.50 : qualité moyenne, 0.25 : faible qualité
jpegparam.setQuality(0.75f,false);
jpegencoder.setJPEGEncodeParam(jpegparam);
try {
jpegencoder.encode(bufferImage);
fluxsortie.close();
} catch (java.io.IOException io) { // Gestion des exceptions lors de la fermeture du FileOutputStream (fluxsortie)
System.out.println("IOException");
}
}
} |
Partager