Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    JAVA: stream /dev/video in JFrame

    I didn't know where else to post this.
    I have a web cam (AIPTek PEN CAM) which is loaded and is /dev/video0 on my FC4 system. My goal is to be able to stream the live video in a JFrame and/or a jsp page. This has never been discussed in any of the Java books I have read.

    1) has anyone done this before and if so can you please help me?
    2) any recommendations on which webcams work best for linux?

    Thanks for your time everyone.


  2. #2
    Linux Newbie DeoXMAN's Avatar
    Join Date
    Jul 2005
    Azerbaijan, Germany
    is there library for videos? try searching in API documentation.
    Karabakh - I will be back

  3. #3
    Linux User
    Join Date
    Aug 2005

    Usually, using V4L for interfacing with webcams, there is no need for video codecs.

    Opening the file /dev/video0 and mapping it to memory (or reading it sequentially), the result is a RGB/Gray striped image (each line pixels follow the previous ones).

    So, each time you open that file (/dev/video0), you can get a new uncompressed video frame, wich you can display.

    Here, in this forums, i've answered about V4L interface, how to get frames for V4L devices. Look for them.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (

  4. $spacer_open
  5. #4
    when I do; cat /dev/video0 > /tmp/test.mpg or test.avi I am unable to play the video using xine or mplayer. I have v4l installed and even though you are saying no need for codecs I have every possible codec i can find installed. can you give me an example of how i can open /dev/video0 and map it to memory?

    FYI: Using camorama i am able to see live images from the web cam.

    I appreciate your response but I don't think it brings me any closer to knowing how to stream /dev/video0 into a JFrame.
    I'm new to this forum so i'll try to look for your answers to v4l but i'd appreciate it if you could send me the links.

    Thanks a lot.


  6. #5
    Linux User
    Join Date
    Aug 2005
    What you read from /dev/video0 is not a MPEG file (neither an AVI, MOV...): simply is not a video. It is a raw image.

    for example, this script extract 1000 frames from your webcam:

    for (i = 0; i < 1000; i++) ; do
      cat /dev/video0 > image_raw_$i.raw
      sleep 0.20
    all image_raw*.raw files can be used to encode a video (for example using mplayer/mencoder).

    For example, if you're webcam output 8 bit grayscale images 400x400, each .raw file contains 160000 (400 x 400) bytes, wich each byte describe one pixel (in grayscale). Each .raw file rappresents a frame.
    In the previous situation, you can instantiate a BufferedImage from a byte[ ] array, wich is the data red from /dev/video0. Once you got the BufferedImage, you can display it on a JFrame.

    Of course, this change when you webcam output 8-16-24 RGB images... but the concept it's the same. (i.e.: 24 bit RGB raw images are composed by N pixels, wich each pixel is composed by 3 bytes R G B, wich describe the Red Green and Blue component).

    Obviously you should find a way to setup your webcam using ioctl. This task is easy using C language, but about Java I can't help anymore. You should read v4l.h header, reproduce constants in Java manner and execute the appropriate ioctls.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (

  7. #6
    Thanks a lot burnit.
    I'm at work right now but i'll def. try your suggestions when I get home.

  8. #7
    Try this code.

     * To change this template, choose Tools | Templates
     * and open the template in the editor.
    import com.piousbox.ImageUtils;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
     * @author ae1
    public class ScratchImage extends Frame implements Runnable {
        Thread animation;
        int offset = 0;
        BufferedImage image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
        byte[] bytes = new byte[640 * 480 * 3];
        InputStream is = null;
        public ScratchImage() {
            setSize(300, 300);
            animation = new Thread(this);
            File file = new File("/dev/video0");
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ScratchImage.class.getName()).log(Level.SEVERE, null, ex);
            this.addWindowListener(new WindowAdapter() {
                public void windowClosing(
                        WindowEvent windowEvent) {
        public static void main(String[] args) {
            new ScratchImage();
        public void run() {
            while (true) {
                try {
                    int numread =;
                    offset += numread;
                    InputStream in = new ByteArrayInputStream(bytes);
                    image =;
                } catch (IOException ex) {
                    Logger.getLogger(ScratchImage.class.getName()).log(Level.SEVERE, null, ex);
        public void update(Graphics g) {
            g.drawImage(image, 0, 0, this);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts