how to view video from camserver in jframe?

42 views
Skip to first unread message

Rusty20

unread,
Jan 2, 2012, 5:31:10 AM1/2/12
to v4l4j
is there a way to view the video stream from camserver to a jframe
instead of in a browser where i enter http://localhost:8080/stream?

vishnu v

unread,
Jan 3, 2012, 1:24:02 AM1/3/12
to v4...@googlegroups.com
Try this

Server Side

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.Image.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;



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


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
//import java.sql.*;
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 java.text.SimpleDateFormat;

public class Capture extends WindowAdapter implements Runnable {
    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;
    ObjectOutputStream ob;
    ServerSocket serv=null;
    Socket socket = null;
    ImageIcon i;
    Robot robot = null;
    int noc=0;
    int flag=0,flagAlert=0;
    static boolean diff=false;
    int PORT=5678;
   

    public Capture(){


        int std=V4L4JConstants.STANDARD_WEBCAM;
        int channel=0;
        int qty=60;

        try{
            initFrameGrabber("/dev/video0", 320, 240, std, channel, qty);
        }catch(Exception E){
            System.out.println("Exception Capture "+E);
        }



    }
    private void initGUI(){

        /*f = new JFrame();
        f.setSize(640,480);
        l = new JLabel();
        f.getContentPane().add(l);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.addWindowListener(this);
        f.setVisible(true);
    */

    }

    private void initFrameGrabber(String dev, int w, int h, int std, int channel, int qty) throws V4L4JException{
        vd = new VideoDevice(dev);
        fg = vd.getJPEGFrameGrabber(w, h, channel, std, qty);
        fg.startCapture();
        System.out.println("Starting capture at "+fg.getWidth()+"x"+fg.getHeight());           
    }

    /**
     * Updates the image shown in the JLabel
     * @param b
     */
    public void setImage(byte[] b) {
        l.setIcon(new ImageIcon(b));
    }

    /**
     * Implements the capture thread: get a frame from the FrameGrabber, and display it
     */
    public void run(){
       

        if(Thread.currentThread().getName().equals("capture")){
       
            initGUI();
            stop = false;
            ByteBuffer bb;
            byte[] b;
            Image prev,curr;
            BufferedImage image;
            flag=0;

            String ts=null;
            while(true){
            try {
                bb = fg.getFrame();
                b = new byte[bb.limit()];
                bb.get(b);
                //setImage(b);
                image=ImageIO.read(new ByteArrayInputStream(b));
                i=new ImageIcon(image);
                prev=Toolkit.getDefaultToolkit().createImage(image.getSource());

                while(!stop){
                    Thread.sleep(1);
                    bb = fg.getFrame();
                    b = new byte[bb.limit()];
                    bb.get(b);
                    //setImage(b);
                    image=ImageIO.read(new ByteArrayInputStream(b));
                    i=new ImageIcon(image);
                    curr=Toolkit.getDefaultToolkit().createImage(image.getSource());
                   
                    if(ob==null){
                        noc=0;
                    }
                    if(noc>0){

                        ob.writeObject(i);
                        ob.reset();
                    }


                    //ImageIO.write(image, "jpg", new File("images"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Failed to capture image");
                noc=0;
            }
        }
        }else{
            try{
            serv = new ServerSocket(PORT);
            }catch(Exception E){
            }
            while(true){
                try{
                    Thread.sleep(100);
                }catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Failed to capture image");
                }
                if(noc==0)
                {
                    try{
                       
                        socket = serv.accept();
                        ob=new ObjectOutputStream(socket.getOutputStream());
                        System.out.println("connected");
                        noc++;
                        System.out.println(noc);

                    }catch(Exception E){
                        System.out.println("Exception Connect "+E);
                    }
                }
            }


        }



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

        fg.stopCapture();
        vd.releaseFrameGrabber();
        f.dispose();           
    }


    public static void main(String[] args) throws V4L4JException, IOException {
        String dev = "/dev/video0";
        int w=320, h=240, std=V4L4JConstants.STANDARD_WEBCAM, channel = 0, qty = 80;
        Capture c=new Capture();
        // c.initGUI();
        Thread connect=new Thread(c,"connect");
       
        Thread cap=new Thread(c,"capture");
        cap.start();
        connect.start();


    }

}




Client Side

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.io.*;

public class Display implements Runnable {
    JLabel lblImage;
    JFrame frame;
    Socket socket;
    ObjectInputStream ois;
    ImageIcon ic,im;
    BufferedReader br;
    Image image;
    JPanel pnl;
    String IP;
    int PORT=5678;
    public Display(){
        frame =new JFrame();
        frame.setSize(320,240);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(2);
        lblImage=new JLabel();
        frame.setVisible(true);
        pnl =new JPanel();
        pnl.setBounds(0,0,320,240);
        frame.getContentPane().add(pnl);
        pnl.setLayout(null);
        frame.add(pnl);
        pnl.add(lblImage);
        lblImage.setBounds(0,0,320,240);

    }
    public void run()
    {
        if(Thread.currentThread().getName().equals("connect"))
        {
            try{
                Thread.sleep(1000);
            }catch(Exception E){
            }

            try{
               
                socket = new Socket(IP,PORT);
                System.out.print("connected\n");
                ois=new ObjectInputStream(socket.getInputStream());
            }catch(Exception E){
                System.out.println("Connection Error......"+E);
            }
        }else{

            try{

                while(true){
                    Thread.sleep(1);
                    if(ois==null){
                        //System.exit(0);
                    }
                    else{
                        im=(ImageIcon)ois.readObject();
                        lblImage.setIcon(im);
                        im=(ImageIcon)ois.readObject();
                        lblImage.setIcon(im);
                    }
                }

            }catch(Exception E){
                System.out.println("error----"+E);
            }
        }


    }
    public static void main(String args[])
    {
       
        Display dis=new Display();
        dis.IP = args[0];
        Thread display=new Thread(dis,"capture");
        display.start();
        Thread connect=new Thread(dis,"connect");
        connect.start();


    }
}



On Mon, Jan 2, 2012 at 4:01 PM, Rusty20 <jonrus...@gmail.com> wrote:
is there a way to view the video stream from camserver to a jframe
instead of in a browser where i enter http://localhost:8080/stream?

--
You received this message because you are subscribed to the Google Groups "v4l4j" group.
To post to this group, send email to v4...@googlegroups.com.
To unsubscribe from this group, send email to v4l4j+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/v4l4j?hl=en.


Rusty20

unread,
Jan 10, 2012, 7:45:34 AM1/10/12
to v4l4j
thanks for the code, but there seems to be a problem...

there is an error on the getFrame() method: "the method getFrame() is
undefined for the type FrameGrabber"

I'm currently using the v0.8.10

thank you :)

Rusty20

unread,
Jan 10, 2012, 10:08:09 AM1/10/12
to v4l4j
thanks for the code but there seems to be a problem,
there is an error on the getFrame() method saying that "the method
getFrame() is undefined for the type FrameGrabber"
i'm using eclipse and v4l4j v0.8.10.
> > instead of in a browser where i enterhttp://localhost:8080/stream?
Reply all
Reply to author
Forward
0 new messages