Dúvida JMF - converter imagem da Webcam para vetor de pixel?

3 respostas
Metaleiro

[color=darkblue] Tenho o seguinte trecho o qual uso o JMF, ele captura a minha WebCam :[/color]

public SwingCapture() 
  {
    setLayout(new BorderLayout());
    setSize(550,550);
    
    imgpanel = new ImagePanel();
    capture = new JButton("Capture");
    capture.addActionListener(this);
    
    String str1 = "vfw:Logitech USB Video Camera:0";
    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();
    
    try 
    {
      player = Manager.createRealizedPlayer(ml);
      player.start();
      Component comp;
      
      if ((comp = player.getVisualComponent()) != null)
      {
        add(comp,BorderLayout.NORTH);
      }
      add(capture,BorderLayout.CENTER);
      add(imgpanel,BorderLayout.SOUTH);
    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }

[color=darkblue]Minha duvida é, como converter um dos dois objetos abaixo em um vetor de pixel (caso exista essa possibilidade ou uma melhor)?[/color]

CaptureDeviceInfo di
MediaLocator ml

[color=darkblue]Desde já agradeço[/color]

3 Respostas

J

Olha, MediaLocator e CaptureDeviceInfo não são dispositivos de pintura nem buffers de bytes(imagens).
O primeiro procura dispositivos de captura no seu sistema e envia um vetor de dispositivos para CaptureDeviceInfo.

Se eu fosse você encerraria a idéia de usar jmf. Isso ae já devia ter sido extinto a milhares de anos, não sei como o pessoal do guj usa ainda, ou não pesquisou algo melhor.

dê uma lida nesse link
http://www.humatic.de/htools/dsj.htm

muito melhor, mas fácil e mais robusta que jmf, que usa o finado vfw(vídeo for windows).

Embaixo segue um exemplo para capturar um snapshot de um vídeo(JMF)

Buffer buf = frameGrabber.grabFrame();
Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_RGB);

fonte
http://www.comp.rgu.ac.uk/staff/fh/CM4062/mis/jmf/FrameGrab.html

Metaleiro

[color=darkblue] O DSJ :lol: é muito bom, não conhecia, obrigado !

Na verdade o que eu preciso é algo que também não estou encontrando nele, queria editar a imagem de exibição da Webcam em tempo de captura.

Segue o código para captura com WebCan no DSJ - Java
[/color]

/**
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-10
N.Peters
humatic GmbH
Berlin, Germany
**/

import de.humatic.dsj.*;


public class SimpleCapture implements java.beans.PropertyChangeListener {

	private DSCapture graph;

	public SimpleCapture() {}

	public void createGraph() {

		javax.swing.JFrame f = new javax.swing.JFrame("dsj SimpleCapture");

		/** queryDevices returns video device infos in slot 0 / audio device infos in slot 1 **/

		DSFilterInfo[][] dsi = DSCapture.queryDevices();

		/** this sample only uses video **/

		graph = new DSCapture(DSFiltergraph.DD7, dsi[0][0], false, DSFilterInfo.doNotRender(), this);

		f.add(java.awt.BorderLayout.CENTER, graph.asComponent());

		f.add(java.awt.BorderLayout.SOUTH, new SwingMovieController(graph));

		final javax.swing.JButton toFile = new javax.swing.JButton("set capture file");

		toFile.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(java.awt.event.ActionEvent e) {

				if (graph.getState() == DSCapture.PREVIEW) {

					/* capture to a Windows Media file using the default profile */

					graph.setCaptureFile("captureTest.asf", DSFilterInfo.doNotRender(), DSFilterInfo.doNotRender(), true);

					toFile.setText("set preview");

					/* start recording right away. Outcomment to control this from GUI */

					graph.record();

				} else {

					graph.setPreview();

					toFile.setText("set capture file");

				}

			}

		});

		f.add(java.awt.BorderLayout.NORTH, toFile);

		f.pack();

		f.setVisible(true);

		javax.swing.JFrame jf = new javax.swing.JFrame("Device control");

		jf.setLayout(new java.awt.GridLayout(0,1));

		if (graph.getActiveVideoDevice() != null && graph.getActiveVideoDevice().getControls() != null) {

			for (int i = CaptureDeviceControls.BRIGHTNESS; i < CaptureDeviceControls.LT_FINDFACE; i++) try { jf.add(graph.getActiveVideoDevice().getControls().getController(i, 0, true)); }catch (Exception ex){}

		}

		if (graph.getActiveAudioDevice() != null) for (int i = CaptureDeviceControls.MASTER_VOL; i < CaptureDeviceControls.TREBLE; i++) try { jf.add(graph.getActiveAudioDevice().getControls().getController(i, 0, true)); }catch (Exception ex){}

		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);

	}

	public void propertyChange(java.beans.PropertyChangeEvent pe) {

		switch(DSJUtils.getEventType(pe)) {

		}

	}

	public static void main(String[] args){

		new SimpleCapture().createGraph();

	}


}
J

Metaleiro:
[color=darkblue] O DSI é muito bom, não conhecia, obrigado !

Na verdade o que eu preciso é algo que também não estou encontrando nele, queria editar a imagem de exibição da Webcam em tempo de captura.

Segue o código para captura com WebCan no DSI - Java
[/color]

“DSJ” - DirectShow Java

Para filtrar a imagem em tempo real precisa implementar “filtros” para a jmf. Funcionam como codecs(é um).
Se eu fosse você escrevia filtros em c++ com a api do directx/directshow, e posteriormente carregava-os com a dsj.

Se for utilizar jmf, aqui vai um tutorial, mas o desempenho é literalmente podre(Para filtrar em tempo real):

http://snipplr.com/view/1583/java--jmf-simple-filter/

Para dsj tem que ser c++ mesmo.
http://www.humatic.de/htools/dsj/xgr.php

Criado 4 de outubro de 2010
Ultima resposta 5 de out. de 2010
Respostas 3
Participantes 2