1

On my Raspberry Pi, I can successfully capture and save images from my Logitech Pro 9000 USB webcam from LXTerminal with the following bash line:

fswebcam -d /dev/video0 /home/pi/image.jpg

I want to write a java program that runs the bash line above because it is the simplest way to capture and save an image. So far, I have:

import java.io.*;

public class GrabNSave {
  public static void main(String[] args) throws IOException {
  Runtime.getRuntime().exec("/bin/bash -c fswebcam -d /dev/video0 /home/pi/image.jpg");
  }
}

It's not working. I get no error messages. Please help!

3 Answers3

2

First, you need to install fswebcam....

sudo apt-get install fswebcam

Then, in your Java program, you need to run the following:

Runtime.getRuntime().exec("fswebcam -d /dev/video0 /home/username/Desktop/test.jpg");

Worked for me, hopefully it does for you! =)

I had this same problem at first, by the way.. =)

Good luck!
Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
0

/bin/bash -c fswebcam -d /dev/video0 /home/pi/image.jpg will not work in bash either, you need to add quotes so that bash receives the command as a single argument:

 /bin/bash -c 'fswebcam -d /dev/video0 /home/pi/image.jpg'

But I'd recommand using a simpler version:

 Runtime.getRuntime().exec("fswebcam -d /dev/video0 /home/pi/image.jpg")

or if you need to change the arguments something among the lines of:

 String[] command = {"fswebcam", "-d", "/dev/video0", "/home/pi/image.jpg"}
 Runtime.getRuntime().exec(command)
Nicolas Cortot
  • 6,591
  • 34
  • 44
  • Thank you for the suggestions. I tried all of the above and it still doesn't work. The program compiles, but when I run it nothing happens. =( – Aí Phi Nguyễn Apr 22 '13 at 05:50
0

If you handle the InputStream you get from the Process it should work: `

            Process process = Runtime.getRuntime().exec("sudo fswebcam -r 320x240 -d /dev/video0 /home/pi/apache-tomcat-7.0.37/webapps/co/cam1.jpg");
            InputStream ips = process.getErrorStream();

            int b = 0;
            while ((b = ips.read()) > 0) {
                // do something 
            }

`