Java Code : Capture Image from webcam using JavaCV

Java Code for capturing image from webcam- uses JavaCV (java wrapper for OpenCV) library
Working CODE:
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage; import static com.googlecode.javacv.cpp.opencv_highgui.*; public class CaptureImage { private static void captureFrame() { // 0-default camera, 1 - next...so on final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); try { grabber.start(); IplImage img = grabber.grab(); if (img != null) { cvSaveImage("capture.jpg", img); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { captureFrame(); } }

39 comments:

  1. Halo,

    Your article is very good, Thanks

    I have some questions for you :

    1) How the list program if I'll use the external webcam ?

    2) I've tried your program (above) but not run, what's wrong...?

    Thanks for your attention

    ReplyDelete
    Replies
    1. As i mentioned,
      grabber = new OpenCVFrameGrabber(0);
      ->> 0 is for default camera(laptop's webcam) and 1 is for the next and so on... Try using 1 for external camera.

      For the second question, please post the error message? So that I can check what's going on.

      Delete
  2. I'm also getting an error. I'm trying to use a camera attached through USB port ( I used OpenCVFrameGrabber(1);). Here's the error I'm getting:


    Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Katherine\AppData\Local\Temp\jniopencv_core3346750596212666156.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:352)
    at com.googlecode.javacpp.Loader.load(Loader.java:285)
    at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:127)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:305)
    at com.googlecode.javacv.cpp.opencv_imgproc.(opencv_imgproc.java:96)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:305)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:91)
    at com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:99)
    at CaptureImage.captureFrame(CaptureImage.java:11)
    at CaptureImage.main(CaptureImage.java:21)

    ReplyDelete
    Replies
    1. @Katherine, i think you have problem with OpenCV library configuration.
      Did you correctly followed the configuration as i wrote in this post : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html ? and Added the following to System Path ??
      3) Configure path: Add the following environment variables in system path
      64-bit version of the JDK:
      C:\opencv\build\common\tbb\intel64\vc10\;C:\opencv\build\x64\vc10\bin\;
      32-bit version of the JDK:
      C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\;

      Delete
  3. superb article Mr.Ganesh Tiwari
    My team and myself are working on a project called Emergency Vehicle recognition system where we writing code in java using eclipse
    but we are confused on what framework or software to use to take the video directly into the code we write and it should execute

    that is our system should take the live stresming video from the webcam and use it within the java code online

    do u have any idea plz help
    thank u for ur attention.

    ReplyDelete
  4. hello Mr.ganesh we are do a project were we need to take in real time video from the environment or system and were that video is converted to images and i can use blob analysis on it to detect the object can you help us out here

    video is taken thru webcam

    and does opencv used within the code to get image inside the codes or application we are designing plz help we are newbie to java and to this stream

    regards
    satish.s

    ReplyDelete
  5. Hello Mr Ganesh , could you please help me , how to load a video instead of webcam and break the video into frames to do processing?

    ReplyDelete
    Replies
    1. I haven't done anything like you said yet ! But this will help you a lot

      Delete
    2. Hi Divium !

      FFMPEG is the way to go for your requirement. You set FFMPEG up on your system and then call the right commands from inside your code. For Java, you can use ProcessBuilder to call external commands.

      If you have any issues, please do drop me an email at,
      kamran.software.engineer@gmail.com

      Thanks.

      Delete
  6. I get empty black picture, can you tell me what is the possble reason?

    ReplyDelete
    Replies
    1. This might be due to your camera is taking time initialize. Try to run this code instead - http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/javacv-capture-save-flip-show-live.html

      Delete
    2. hi Ganesh, I'm getting a black picture too.
      Has a way to hold on a second to webcam get fully ready to capture?

      Delete
  7. Which Jar File should i used for above program please help me?

    ReplyDelete
    Replies
    1. Follow the steps on http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html to configure the project.

      Delete
  8. Hello Ganesh , do you know how to use the function blobTrackerAuto ? i really need complete my final year project and i'm at a loss what to do next. Although we have the blobtracker sample in openCV , i'm not able to do the conversion..have you this funcion or know how to use it ? please can you help me?

    ReplyDelete
  9. I don't get a picture, but the following output:

    ***** VIDEOINPUT LIBRARY - 0.1995 - TFW07 *****

    SETUP: Setting up device 0
    SETUP: USB2.0 UVC WebCam
    SETUP: Couldn't find preview pin using SmartTee
    SETUP: Capture callback set
    SETUP: Device is setup and ready to capture.

    ReplyDelete
    Replies
    1. I too am getting same thing... But its not capturing any image...

      Delete
  10. hi ganesh
    I am getting following eerror in any code relating to javacv...I have tried thers also but no luck.

    Exception in thread "main" java.lang.NoClassDefFoundError: com/googlecode/javacpp/Pointer
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
    at test.TestGrabber.main(TestGrabber.java:10)
    Caused by: java.lang.ClassNotFoundException: com.googlecode.javacpp.Pointer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
    ... 15 more

    ReplyDelete
    Replies
    1. It looks like you have missed some steps in configuring javacv project. Follow the steps http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html correctly.

      Delete
  11. Hi Ganesh,
    This is pretty neat code but I'm getting the following error:


    at java.lang.Class.forName(Class.java:247)
    at com.googlecode.javacpp.Loader.load(Loader.java:335)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:91)
    at com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:171)
    at CaptureImage.captureFrame(CaptureImage.java:11)
    at CaptureImage.main(CaptureImage.java:21)

    Can you please point to possible solution

    ReplyDelete
    Replies
    1. Can you please post what exception you get. Please provided full stacktrace .

      Delete
    2. Sorry about that :(


      Exception in thread "main" java.lang.UnsatisfiedLinkError: /private/var/folders/39/_2n7sjxn5tl4kmxq6_j_nvx00000gn/T/libjniopencv_core3258241347711174746.dylib: Library not loaded: lib/libopencv_core.2.4.dylib Referenced from: /private/var/folders/39/_2n7sjxn5tl4kmxq6_j_nvx00000gn/T/libjniopencv_core3258241347711174746.dylib Reason: image not found
      at java.lang.ClassLoader$NativeLibrary.load(Native Method)
      at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1827)
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1703)
      at java.lang.Runtime.load0(Runtime.java:770)
      at java.lang.System.load(System.java:1020)
      at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:403)
      at com.googlecode.javacpp.Loader.load(Loader.java:342)
      at com.googlecode.javacpp.Loader.load(Loader.java:316)
      at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:131)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:247)
      at com.googlecode.javacpp.Loader.load(Loader.java:335)
      at com.googlecode.javacv.cpp.opencv_imgproc.(opencv_imgproc.java:96)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:247)
      at com.googlecode.javacpp.Loader.load(Loader.java:335)
      at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:91)
      at com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:171)
      at CaptureImage.captureFrame(CaptureImage.java:11)
      at CaptureImage.main(CaptureImage.java:21)

      Delete
    3. at com.googlecode.javacv.cpp.opencv_imgproc.(opencv_imgproc.java:96)
      at java.lang.Class.forName0(Native Method)

      ---> You have the javaCv jar in classpath.



      "java.lang.UnsatisfiedLinkError"
      ---> JVM could not locate the native libraries [ libopencv_core.2.4.dylib ...] in system path. i.e., the required library files could not be linked into your program. This is most frequently caused by the native library files not being in the place where t.

      I don't use OSX and don't have any idea of how you configured javacv there.
      But it looks like you didn't provide the path of opencv properly.

      May be this will help you : http://code.google.com/p/javacv/issues/detail?id=125

      Delete
  12. I forgot to mention that Im using OSX Lion if it is relevant.
    Cheers

    ReplyDelete
  13. Showing errors like these..Please give me a solution.

    Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\gopal\AppData\Local\Temp\javacpp7955905460040\jniopencv_core.dll: %1 is not a valid Win32 application
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1928)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1825)
    at java.lang.Runtime.load0(Runtime.java:792)
    at java.lang.System.load(System.java:1059)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:418)
    at com.googlecode.javacpp.Loader.load(Loader.java:368)
    at com.googlecode.javacpp.Loader.load(Loader.java:315)
    at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.googlecode.javacpp.Loader.load(Loader.java:334)
    at com.googlecode.javacv.cpp.opencv_imgproc.(opencv_imgproc.java:96)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.googlecode.javacpp.Loader.load(Loader.java:334)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:91)
    at com.googlecode.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:170)
    at javaapplication16.JavaApplication16.captureframe(JavaApplication16.java:24)
    at javaapplication16.JavaApplication16.main(JavaApplication16.java:38)
    Java Result: 1

    ReplyDelete
  14. Hi i executed you code its building successfull but when i made it run the process will be continuing that its running till i stop the process, When i stop the process it ll display as buid successfuly but i am not getting the image of a image dialouge box.

    ReplyDelete
  15. I run the program, but it just run countinously, no output of the program...
    how it can be like that, there was no error from my program...

    ReplyDelete
  16. hello ganesh..!!
    i am using your code for image grabbing but i am getting an error..!!

    com.googlecode.javacv.FrameGrabber$Exception: videoInput.getPixels() Error: Could not get pixels.
    at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:163)
    at opencv.ImageGrabber.run(ImageGrabber.java:31)
    at java.lang.Thread.run(Unknown Source)

    also i would like to bring to your notice that it worked the first time and showed me the video..
    but in my second attempt its not working
    also i tried your above code to capture and save image and its working fine ..!!
    where am i going wrong..please help..!

    ReplyDelete
    Replies
    1. I encountered the same problem. Have you solved it? Thank in advance.

      Delete
  17. Hi ganesh thank's for this solution It work's great thank's a lot
    I am getting problem in setting camera resolution and which size the image it will take after saving it.Please help me out. I setting camera resolution by setting
    grabber.setImageHeight(value)
    grabber.setImageWidth(value)
    But this is not saving image in accurate values i give
    Thank in advance

    ReplyDelete
  18. how can we make a focal point images over camera screen juzt as we seen in real camer...is it possible?this should be seen when the pop up screen comes...it shoulbe seen on that screen

    ReplyDelete
  19. Thank you so much... ;)

    ReplyDelete
  20. run:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/googlecode/javacpp/Pointer
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Caused by: java.lang.ClassNotFoundException: com.googlecode.javacpp.Pointer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    ... 11 more
    Could not find the main class: webcam.CaptureImage. Program will exit.
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    I got this error ganesh.. and this will also while runing

    bad operand types for binary operator '!='

    ReplyDelete
  21. hello, please i wanna do the same thing but using android device camera instead of webcmer of the laptop via blutooth or wifi .please help i need indication. thanks a lt

    ReplyDelete
  22. Hello Mr.Ganesh Tiwari

    I need ur help!! am 5th year computer engineering student and am working on like video calling system with many clients so is it possible to use opencv and javacv for this kind of project in order to stream the incoming videos?

    ReplyDelete
  23. I have tried your code, a blank window stating "web cam" appears but my webcam is not powering on and there is no error in console either.

    ReplyDelete
    Replies
    1. are you using correct camera index?

      final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

      If you have multiple web cam then, try changing 0 to 1 or 2 and so on

      Delete
  24. I think your code should be modified as follows since sometimes there's a delay in camera initialisation and an exception is thrown:

    import com.googlecode.javacv.FrameGrabber;
    import com.googlecode.javacv.OpenCVFrameGrabber;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;

    import static com.googlecode.javacv.cpp.opencv_highgui.*;

    public class CaptureImage {
    private static void captureFrame() {
    // 0-default camera, 1 - next...so on
    final FrameGrabber grabber = new OpenCVFrameGrabber(0);

    boolean done = false;

    while (!done) {
    try {
    grabber.start();
    IplImage img = null;
    do {
    img = grabber.grab();
    } while (img == null);
    if (img != null) {
    cvSaveImage("capture.jpg", img);
    System.out.println("A frame was captured successfully!");
    done = true;
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args) {
    captureFrame();
    }
    }

    ReplyDelete

Your Comment and Question will help to make this blog better...