Título: Webcam Shots Aleatorios Publicado por: kira23ele en 2 Enero 2014, 11:14 am Hola amigos, llevo 1 mes encallado en el desarrollo de una aplicacion que haga webcam shots aleatorios cada 10 segundos, de momento solo he conseguido que reporoduzca video, y no hay manera de hacer que haga webcamshots, he probado de todo.
Aqui les dejo el codigo que tengo hasta ahora espero qe puedan ayudarme :huh: import java.awt.Component; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import javax.media.Buffer; import javax.media.CaptureDeviceInfo; 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 javax.swing.JLabel; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class pruebawebcam { public static void main(String[] args) { // TODO Auto-generated method stu otro perro=new otro(); perro.show(); perro.proceso(); // public ImagePanel imgpanel = null; } } class otro extends JFrame{ JLabel Imagen; otro(){ Imagen=new JLabel(); Imagen.setBounds(30,40,20,20); add(Imagen); setBounds(400,400,400,400); setLayout( null ); // use a BorderLayou setTitle("Prueba de Camara Web"); } public void actionPerformed(ActionEvent e) { JButton capture = null; JComponent c = (JComponent) e.getSource(); capture = new JButton("Capture"); if (c == capture) { CaptureDeviceInfo di = null; MediaLocator ml = null; Player player = null; VideoFormat vf = null; BufferToImage btoi = null; // ImagePanel imgpanel = null; Buffer buf = null; Image img = null; // 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"); } } public void proceso(){ Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true ); try{ MediaLocator ml = new MediaLocator("vfw://0"); Player p = Manager.createRealizedPlayer(ml); Component video = p.getVisualComponent(); video.setBounds(20,30,600,600); if ( video != null ){ // agragar el video al componente add( video); } p.start(); }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); } 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"); } } } |