-6

how to use camera using swing in java with the ubuntu OS please give me some examples and guidelines to use camera through the java.

public static void main(String[] args) {
    CamDemo t = new CamDemo();
    t.getCam();
}

public void getCam() {
    try {

        /* Grab the default web cam */
        MediaLocator ml = new MediaLocator("vfw://0");
        DataSource ds = Manager.createDataSource(ml);
        requestFormatResolution(ds);
        Player p = Manager.createRealizedPlayer(ds);
        p.start();
        Thread.currentThread().sleep(1000);
        JFrame jfrm = new JFrame("Testing Webcam");
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        if (p.getVisualComponent() != null)
            jfrm.getContentPane().add(p.getVisualComponent());
        if (p.getControlPanelComponent() != null)
            jfrm.getContentPane().add(p.getControlPanelComponent(),
                    BorderLayout.SOUTH);
        jfrm.pack();
        jfrm.setLocationRelativeTo(null);
        jfrm.setVisible(true);
        jfrm.setSize(320, 240);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public boolean requestFormatResolution(DataSource ds) {

    if (ds instanceof CaptureDevice) {
        FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
        for (FormatControl fc : fcs) {
            Format[] formats = ((FormatControl) fc).getSupportedFormats();
            for (Format format : formats) {
                if ((format instanceof VideoFormat)
                        && (((VideoFormat) format).getSize().getHeight() <= 240)
                        && (((VideoFormat) format).getSize().getWidth() <= 320)) {
                    ((FormatControl) fc).setFormat(format);
                    return true;
                }
            }
        }
    }
    return false;
}
}

Stacktrace:

javax.media.NoDataSourceException: Cannot find a DataSource for: vfw://0
at javax.media.Manager.createDataSource(Manager.java:1037)
at com.convoy.gpack.pack.CamDemo.getCam(CamDemo.java:32)
at com.convoy.gpack.pack.CamDemo.main(CamDemo.java:16)
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
Gopal00005
  • 2,061
  • 4
  • 35
  • 54
  • what you have tried till now? – maxx777 Aug 07 '14 at 13:36
  • possible duplicate of [Best way to access web camera in Java](http://stackoverflow.com/questions/11487251/best-way-to-access-web-camera-in-java) – DavidPostill Aug 07 '14 at 13:38
  • 2
    why swing when you got javafx – Philipp Sander Aug 07 '14 at 13:40
  • Hello sir I am not getting any example of the camera integration So tell me the one example......Thanks in advance. – Gopal00005 Aug 07 '14 at 13:41
  • 1
    You've been given examples but 'can't use them'. What exactly have you tried, where is your research effort? We're not here to write it for you. Show us what you have tried, the relevant code and the **specific** issues you are having. Read up on [how to write a Short, Self Contained, Correct Example](http://sscce.org/) and have a look at the [help centre](http://stackoverflow.com/help) on here. Then [edit](http://stackoverflow.com/posts/25183968/edit) your question and give all the necessary details. Voting to close as it is off topic for now. I Googled Java Webcam and found TONS of code. – RossC Aug 07 '14 at 13:48
  • Ok Sir I am giving you the applied example.... – Gopal00005 Aug 07 '14 at 13:51
  • unknown protocol: vfw java.net.MalformedURLException: unknown protocol: vfw at java.net.URL.(URL.java:592) at javax.media.MediaLocator.getURL(Unknown Source) at com.convoy.gpack.pack.CamDemo.main(CamDemo.java:19) javax.media.NoDataSourceException at javax.media.Manager.createDataSource(Unknown Source) at com.convoy.gpack.pack.CamDemo.getCam(CamDemo.java:35) at com.convoy.gpack.pack.CamDemo.main(CamDemo.java:19) – Gopal00005 Aug 07 '14 at 14:08
  • This is new stack trace for new error – Gopal00005 Aug 07 '14 at 14:08

2 Answers2

6

Using Webcam Capture project.

Example from author for the API usage:

Webcam buildin = Webcam.getWebcams().get(0); // build-in laptop camera
Webcam usb = Webcam.getWebcams().get(1); // usb camera
BufferedImage image1 = buildin.getImage();
BufferedImage image2 = usb.getImage(); // do with image1 and image2 whatever you want
  • Actually this example is too large to integrate with my project.......So please give me the small and simple to learn example....thanks again.. – Gopal00005 Aug 07 '14 at 13:44
  • And also i am not able to import the project given by the SARXOS Web cam example.... – Gopal00005 Aug 07 '14 at 13:45
  • 5
    @Gopal00005 _Actually this example is too large to integrate with my project_ Really? **Four lines** are too large? – BackSlash Aug 07 '14 at 13:55
  • Oh lord, this is turning into a farce. I think he wants you to do ALL the work for him. Four lines is an insane amount of code (sigh) so you can understand why you should fly over to the OP, hold their hand and only have 3 lines of code! Have a +1 for answering the question and having to deal with one of the laziest posters I've ever seen on here! – RossC Aug 07 '14 at 13:56
  • But sir I don't know how to work with the SARXOS example can you elaborate please how to work with it....I mean step by step...Because I am not able to work with the SARXOS. Sorry for that but I am new to work with Java. – Gopal00005 Aug 07 '14 at 14:01
  • @Gopal00005 Click the link, download the project, import into your project and you only need the four lines of code above. Look into importing into your project and using bits of Github (hint: Donwload Zip) and so on. It's a great learning opportunity and this answer has everything you need already done for you! There's even a link at the top of the Github page with how to get started: http://webcam-capture.sarxos.pl/ Just do that! – RossC Aug 07 '14 at 14:04
  • Ohk thank you very much sir I am trying it now..... – Gopal00005 Aug 07 '14 at 14:10
  • @ROSSC I just have implemented the given example of SARXOS but it also throws exception [main] INFO com.github.sarxos.webcam.Webcam - WebcamDefaultDriver capture driver will be used Exception in thread "main" com.github.sarxos.webcam.WebcamException: ava.util.concurrent.ExecutionException: com.github.sarxos.webcam.WebcamException: Cannot execute – Gopal00005 Aug 08 '14 at 06:37
  • Hey dude @InterruptedException it's great API I ever used thanks again and thanks SARXOS too for providing this simple API for the public use. – Gopal00005 Sep 08 '14 at 10:42
  • Ya welcome! Don't be afraid to try things yourself and make mistakes. That's the Only way to learn... – InterruptedException Nov 02 '14 at 16:55
  • @InterruptedException, the code runs perfectly, but the result is not always same, what you're claiming to be. – Dila Gurung Jan 08 '16 at 06:25
5

The Java Media Framework (jmf) is what you are looking for.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
  • I just need one example to work with the camera can anyone help me ? – Gopal00005 Aug 07 '14 at 13:42
  • no. do some research on your own. show some effort man! – Philipp Sander Aug 07 '14 at 13:49
  • 1
    +1 for this, that's a big help, but I think he wants you to write it for him, or find an example that already does all the work. That's what you get for helping out eh? – RossC Aug 07 '14 at 13:55
  • Actually the SARXOS API is great API and I also tried the jmf but it's better to use SARXOS for the simplicity and reliablity.Thanks a lot buddies... – Gopal00005 Sep 08 '14 at 10:45