Archive

Archive for the ‘java’ Category

Java y v4l, acceder a la webcam en linux con video 4 linux

September 20th, 2010 2 comments
java webcam

java webcam

Estoy en un experimento con webcams y java, y bueno, obviamente al no haber laburado nunca con este tema sobre linux se me vinieron todos los problemas juntos.

Para ir resumiendo, el jar de V4L4J (Video for linux for Java) anda perfecto, pero el ejemplo tiene algunos temillas que hay que correjir, es por eso que les dejo el codigo fixeado, asi los que quieran programar algo en Java con webcam sobre linux ya tienen que con empezar.

En breve escribiré un post sobre como dejar funcionando la webcam en ubuntu (va, kernel 2.6.x) que no ha sido nada facil, ya que hay que tocar algunas cosas (mas que nada por gspca).

Algunos datos que les pueden servir.

Normalmente para trabajar con la webcam en Java solo basta con la libreria JMF (Java Media Framework) que nos da soporte a todo el mundo multimedia. El tema es que para poder implementar video, audio o lo que sea se requiere el source. Este source puede ser un archivo o un streaming, en el caso de windows solemos usar v4W (Video for windows), pero en linux es necesario v4l (o similares) y por defecto no hay soporte para ese source.

Lamentablemente hay mas documentación al respecto sobre C++ que Java, pero es 100% aplicable, así que si saben C++ no habrá problemas con leer el source y entender como hacer las cosas o incluso extender las funcionalidades de package.

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import au.edu.jcu.v4l4j.FrameGrabber;
import au.edu.jcu.v4l4j.exceptions.V4L4JException;
import au.edu.jcu.v4l4j.VideoDevice;
import au.edu.jcu.v4l4j.V4L4JConstants;
import au.edu.jcu.v4l4j.ImageFormat;
import au.edu.jcu.v4l4j.ImageFormatList;

public class Exa5 implements Runnable {
	private Thread thisThread;
	private VideoDevice vd;
	 private JLabel l;
     private JFrame f;
     private long start = 0;
     private int n;
     private FrameGrabber fg;
     private Thread captureThread;
     private boolean stop;

	public static void main(String[] argv){

		try{
        String dev = "/dev/video0";
        //int w=640, h=480, std=FrameGrabber.STANDARD_WEBCAM, channel = 0, qty = 60;

        int w=640, h=480, std=V4L4JConstants.STANDARD_WEBCAM, channel = 0, qty = 60;
        new Exa5(dev,w,h,std,channel,qty);
		}catch(Exception e){ System.out.println("Buuu: "+e.toString()); }

	}

	 public Exa5(String dev, int w, int h, int std, int channel, int qty) throws V4L4JException{
		 initFrameGrabber(dev, w, h, std, channel, qty);
        initGUI();
        stop = false;

		thisThread = new Thread(this, "JMotion");
		thisThread.start();
	}

	 private void initGUI(){
	        f = new JFrame();
	        l = new JLabel();
	        f.getContentPane().add(l);
	        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	        //f.addWindowListener(this);
	        f.setVisible(true);
	        f.setSize(fg.getWidth(), fg.getHeight());
	    }

	 private void initFrameGrabber(String dev, int w, int h, int std, int channel, int qty) throws V4L4JException{
	        vd = new VideoDevice(dev);
	        ImageFormatList l = vd.getDeviceInfo().getFormatList();
	        for(ImageFormat f : l.getNativeFormats()) {
	        	  System.out.println("\t"+f.getName()+" - "+f.getIndex());
	        	  System.out.println("\t\tResolutions: "+f.getResolutionInfo());
	        	}

	        fg = vd.getJPEGFrameGrabber(w, h, channel, std, qty);
	        fg.startCapture();
	        System.out.println("Starting capture at "+fg.getWidth()+"x"+fg.getHeight());
	    }

	 public void setImage(byte[] b) {
	        l.setIcon(new ImageIcon(b));
	    }

	public void run(){
		  ByteBuffer bb;
	        byte[] b;
	        try {
	                while(!stop){
	                        bb = fg.getFrame();
	                        b = new byte[bb.limit()];
	                        bb.get(b);
	                        setImage(b);
	                        System.out.println(bb.toString());
	                }
	        } catch (V4L4JException e) {
	                e.printStackTrace();
	                System.out.println("Failed to capture image");
	        }
	}

	   public void windowClosing(WindowEvent e) {
           if(captureThread.isAlive()){
                   stop = true;
                   try {
                           captureThread.join();
                   } catch (InterruptedException e1) {}
           }

           fg.stopCapture();
           vd.releaseFrameGrabber();

           f.dispose();
   }

}
Categories: java, programacion Tags: , , ,

Java 4 trailer

June 25th, 2010 No comments

Excelente video, que digo excelente, una obra de arte.

Java 4 Trailer

Categories: java, para relajarse Tags: , ,