Já procurei em vários tópicos mas não estou conseguindo achar uma resposta.
Meu atual formulário GUI mostra a imagem da webcam e é possível tirar fotos e salva-las no computador. Porém eu queria que quando o usuário apertasse ‘Gravar’ começasse a gravar e o botoão mudasse para ‘Parar’. Quando ele clicar de novo a gravação para e salva o arquivo.
As outras coisas eu sei fazer, como os Botões e o JFileChooser, mas eu só queria saber COMO GRAVAR?
OBS: Para tirar fotos uso o JFM.
importjava.awt.Image;importjava.awt.BorderLayout;importjava.awt.Component;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.image.RenderedImage;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.media.Buffer;importjavax.media.CaptureDeviceInfo;importjavax.media.CaptureDeviceManager;importjavax.media.Manager;importjavax.media.MediaLocator;importjavax.media.Player;importjavax.media.control.FrameGrabbingControl;importjavax.media.format.VideoFormat;importjavax.media.util.BufferToImage;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JRootPane;publicclassWebCamCaptureextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionUID=1L;privatePlayerplayer;privateCaptureDeviceInfodi;privateMediaLocatorml;privateJPanelpainelbotoes;privateJButtonstart=null;privateJButtoncapture=null;publicJButtonclearimage=null;publicJButtonconfirm=null;publicBufferbuf=null;publicImageimg=null;// public VideoFormat vf = null;publicBufferToImagebtoi=null;publicComponentwebcam=null;publicstaticvoidmain(Stringargs[]){JFramewcam=newWebCamCapture();wcam.setTitle("Captura de Imagem");}publicWebCamCapture(){setUndecorated(true);getRootPane().setWindowDecorationStyle(JRootPane.FRAME);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(newBorderLayout());painelbotoes=newJPanel();painelbotoes.setLayout(newGridLayout(1,4));start=newJButton("Iniciar");start.addActionListener(this);capture=newJButton("Capture");capture.addActionListener(this);capture.setEnabled(false);clearimage=newJButton("Limpar");clearimage.addActionListener(this);clearimage.setEnabled(false);confirm=newJButton("Gravar");confirm.addActionListener(this);confirm.setEnabled(false);painelbotoes.add(start);painelbotoes.add(capture);painelbotoes.add(clearimage);painelbotoes.add(confirm);add(painelbotoes,BorderLayout.NORTH);di=CaptureDeviceManager.getDevice("vfw//0");ml=newMediaLocator("vfw://0");pack();this.setSize(400,400);this.setLocationRelativeTo(this);this.setVisible(true);}publicvoidplayerClose(){player.close();player.deallocate();player=null;}publicvoidactionPerformed(ActionEvente){if(e.getSource()==start){try{player=Manager.createRealizedPlayer(ml);webcam=player.getVisualComponent();if(webcam!=null)add(webcam,BorderLayout.CENTER);player.start();validate();}catch(Exceptionex){JOptionPane.showMessageDialog(null,"Não foi possível conectar ao dispositivo de captura\n"+ex.getMessage());ex.printStackTrace();}start.setEnabled(false);capture.setEnabled(true);}elseif(e.getSource()==capture){FrameGrabbingControlfgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");buf=fgc.grabFrame();btoi=newBufferToImage((VideoFormat)buf.getFormat());img=btoi.createImage(buf);playerClose();capture.setEnabled(false);clearimage.setEnabled(true);confirm.setEnabled(true);}elseif(e.getSource()==clearimage){remove(webcam);webcam=null;clearimage.setEnabled(false);confirm.setEnabled(false);start.setEnabled(true);}elseif(e.getSource()==confirm){gravaImg(img);System.exit(0);}}publicvoidgravaImg(Imageimagem){Filearquivo=newFile("C:/Temp/Teste.jpg");try{if(!arquivo.exists()){FileOutputStreamfo=newFileOutputStream(arquivo);}ImageIO.write((RenderedImage)imagem,"JPG",arquivo);}catch(IOExceptionex){JOptionPane.showMessageDialog(null,"Não foi possível encontrar o caminho especificado");ex.printStackTrace();}}}
Execute e vê se é o que voce queria.
J
juliocbq
GustavoBorba:
E em JFM, não haveria um jeito?
A jmf usa o vídeo for windows(vfw), que está para ser excluído das próximas versões do windows(7 em diante). A dsj usa directshow e pinta o vídeo em cima do direct3d, com a vmr9. Além da dsj ser bem mais simples de se usar.
A aplicação abaixo realiza uma simples captura para um arquivo de vídeo.
/**dsj demo code.You may use, modify and redistribute this code under the terms laid out in the header of the DSJDemo application.copyright 2005-7N.Petershumatic GmbHBerlin, Germany**/importde.humatic.dsj.*;publicclassSimpleCaptureimplementsjava.beans.PropertyChangeListener{privateDSCapturegraph;publicSimpleCapture(){}publicvoidcreateGraph(){javax.swing.JFramef=newjavax.swing.JFrame("dsj SimpleCapture");DSFilterInfo[][]dsi=DSCapture.queryDevices();graph=newDSCapture(DSFiltergraph.RENDER_NATIVE,dsi[0][2],false,DSFilterInfo.doNotRender(),this);f.add(java.awt.BorderLayout.CENTER,graph.asComponent());f.add(java.awt.BorderLayout.SOUTH,newSwingMovieController(graph));finaljavax.swing.JButtontoFile=newjavax.swing.JButton("set capture file");toFile.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){if(graph.getState()==DSCapture.PREVIEW){graph.setCaptureFile("captureTest.asf",DSFilterInfo.doNotRender(),DSFilterInfo.doNotRender(),true);toFile.setText("set preview");graph.record();}else{graph.setPreview();toFile.setText("set capture file");}}});f.add(java.awt.BorderLayout.NORTH,toFile);f.pack();f.setVisible(true);javax.swing.JFramejf=newjavax.swing.JFrame("Device control");jf.setLayout(newjava.awt.GridLayout(0,1));if(((DSCapture)graph).getActiveVideoDevice()!=null&&((DSCapture)graph).getActiveVideoDevice().getControls()!=null){for(inti=CaptureDeviceControls.BRIGHTNESS;i<CaptureDeviceControls.FOCUS;i++)try{jf.add(((DSCapture)graph).getActiveVideoDevice().getControls().getController(i,0,true));}catch(Exceptionex){}}if(((DSCapture)graph).getActiveAudioDevice()!=null)for(inti=CaptureDeviceControls.MASTER_VOL;i<CaptureDeviceControls.TREBLE;i++)try{jf.add(((DSCapture)graph).getActiveAudioDevice().getControls().getController(i,0,true));}catch(Exceptionex){}if(jf.getContentPane().getComponentCount()==0)return;jf.pack();jf.setVisible(true);/** Don't do this at home. This demo relies on dsj closing and disposing off filtergraphs when the JVM exits. This is OK for a "open graph, do something & exit" style demo, but real world applications should take care of calling dispose() on filtergraphs they're done with themselves. **/f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);}publicvoidpropertyChange(java.beans.PropertyChangeEventpe){System.out.println("received event or callback from "+pe.getPropagationId());}publicstaticvoidmain(String[]args){newSimpleCapture().createGraph();}}
G
GustavoBorba
Olá, discorpio.
Esse código eu já tenho. Eu gostaria é de gravar um VÍDEO.
Abraços, Gustavo Borba.
J
juliocbq
GustavoBorba:
Olá, discorpio.
Esse código eu já tenho. Eu gostaria é de gravar um VÍDEO.
Abraços, Gustavo Borba.
/**dsj demo code.You may use, modify and redistribute this code under the terms laid out in the header of the DSJDemo application.copyright 2005-7N.Petershumatic GmbHBerlin, Germany**/importde.humatic.dsj.*;publicclassSimpleCaptureimplementsjava.beans.PropertyChangeListener{privateDSCapturegraph;publicSimpleCapture(){}publicvoidcreateGraph(){javax.swing.JFramef=newjavax.swing.JFrame("dsj SimpleCapture");DSFilterInfo[][]dsi=DSCapture.queryDevices();graph=newDSCapture(DSFiltergraph.RENDER_NATIVE,dsi[0][2],false,DSFilterInfo.doNotRender(),this);f.add(java.awt.BorderLayout.CENTER,graph.asComponent());f.add(java.awt.BorderLayout.SOUTH,newSwingMovieController(graph));finaljavax.swing.JButtontoFile=newjavax.swing.JButton("set capture file");toFile.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEvente){if(graph.getState()==DSCapture.PREVIEW){graph.setCaptureFile("captureTest.asf",DSFilterInfo.doNotRender(),DSFilterInfo.doNotRender(),true);toFile.setText("set preview");graph.record();}else{graph.setPreview();toFile.setText("set capture file");}}});f.add(java.awt.BorderLayout.NORTH,toFile);f.pack();f.setVisible(true);javax.swing.JFramejf=newjavax.swing.JFrame("Device control");jf.setLayout(newjava.awt.GridLayout(0,1));if(((DSCapture)graph).getActiveVideoDevice()!=null&&((DSCapture)graph).getActiveVideoDevice().getControls()!=null){for(inti=CaptureDeviceControls.BRIGHTNESS;i<CaptureDeviceControls.FOCUS;i++)try{jf.add(((DSCapture)graph).getActiveVideoDevice().getControls().getController(i,0,true));}catch(Exceptionex){}}if(((DSCapture)graph).getActiveAudioDevice()!=null)for(inti=CaptureDeviceControls.MASTER_VOL;i<CaptureDeviceControls.TREBLE;i++)try{jf.add(((DSCapture)graph).getActiveAudioDevice().getControls().getController(i,0,true));}catch(Exceptionex){}if(jf.getContentPane().getComponentCount()==0)return;jf.pack();jf.setVisible(true);/** Don't do this at home. This demo relies on dsj closing and disposing off filtergraphs when the JVM exits. This is OK for a "open graph, do something & exit" style demo, but real world applications should take care of calling dispose() on filtergraphs they're done with themselves. **/f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);}publicvoidpropertyChange(java.beans.PropertyChangeEventpe){System.out.println("received event or callback from "+pe.getPropagationId());}publicstaticvoidmain(String[]args){newSimpleCapture().createGraph();}}