Find the answer to your Linux question:
Results 1 to 7 of 7
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    8

    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.

    -Johnny

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

  3. #3
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    Hi.

    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 (http://counter.li.org)

  4. #4
    Just Joined!
    Join Date
    Jun 2006
    Posts
    8
    burnit.
    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.

    -Johnny

  5. #5
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    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:

    Code:
    for (i = 0; i < 1000; i++) ; do
      cat /dev/video0 > image_raw_$i.raw
      sleep 0.20
    done
    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 (http://counter.li.org)

  6. #6
    Just Joined!
    Join Date
    Jun 2006
    Posts
    8
    Thanks a lot burnit.
    I'm at work right now but i'll def. try your suggestions when I get home.

  7. #7
    Just Joined!
    Join Date
    Jul 2010
    Posts
    1
    Try this code.

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.piousbox.video;
    
    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.io.*;
    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() {
            setTitle("ae1");
    
            setVisible(true);
            setSize(300, 300);
    
            animation = new Thread(this);
            animation.start();
    
            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() {
    
                @Override
                public void windowClosing(
                        WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
        }
    
        public static void main(String[] args) {
            new ScratchImage();
        }
    
        public void run() {
            while (true) {
                try {
    
                    int numread = is.read(bytes);
                    offset += numread;
    
                    InputStream in = new ByteArrayInputStream(bytes);
                    in.reset();
                    image = ImageIO.read(in);
    
                    System.out.println(image);
    
                } catch (IOException ex) {
                    Logger.getLogger(ScratchImage.class.getName()).log(Level.SEVERE, null, ex);
                }
                repaint();
            }
        }
    
        @Override
        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
  •