JavaCV capture-save-flip-show live camera

JavaCV:  Capture/save/flip image and show live image on CanvasFrame from camera

JAVA CODE:
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; import com.googlecode.javacv.CanvasFrame; import com.googlecode.javacv.FrameGrabber; import com.googlecode.javacv.VideoInputFrameGrabber; import com.googlecode.javacv.cpp.opencv_core.IplImage; public class GrabberShow implements Runnable { //final int INTERVAL=1000;///you may use interval IplImage image; CanvasFrame canvas = new CanvasFrame("Web Cam"); public GrabberShow() { canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); } @Override public void run() { FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next camera int i=0; try { grabber.start(); IplImage img; while (true) { img = grabber.grab(); if (img != null) { cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise cvSaveImage((i++)+"-aa.jpg", img); // show image on window canvas.showImage(img); } //Thread.sleep(INTERVAL); } } catch (Exception e) { } }

public static void main(String[] args) { GrabberShow gs = new GrabberShow(); Thread th = new Thread(gs); th.start(); } }

39 comments:

  1. With me, it doesn't work with the VideoInputFrameGrabber but only with the OpenCVFrameGrabber

    ReplyDelete
  2. Anyway it worked. That's great news.

    ReplyDelete
    Replies
    1. Pls help me with following error.Ive configured C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\;
      path but stil no image seen.

      Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: C:\Users\user\AppData\Local\Temp\javacpp5669223630979\jniopencv_core.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.loadLibrary(Loader.java:422)
      at com.googlecode.javacpp.Loader.load(Loader.java:372)
      at com.googlecode.javacpp.Loader.load(Loader.java:319)
      at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:136)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at com.googlecode.javacpp.Loader.load(Loader.java:338)
      at com.googlecode.javacpp.Loader.load(Loader.java:319)
      at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:163)
      at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:163)
      at GrabberShow.run(GrabberShow.java:22)
      at java.lang.Thread.run(Unknown Source)

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

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

      Delete
    2. Make sure javaCV version matches openCV version. JavaCV 2.4.4 works with OpenCV 2.4.4 not 2.4.0

      Delete
  3. I'm getting an empty panel. Is the input for the VideoInputFrameGrabber constructor the USB port number? Is there any way I can find the camera by default using something like VideoInputFrameGrabber.getDefault(); or FrameGrabber.getDefault();?

    ReplyDelete
    Replies
    1. 0 is for default camera. try using 0 instead of 1.

      FrameGrabber grabber = new VideoInputFrameGrabber(0);

      Delete
    2. Hi,
      I used opencv 2.4.0 to run this code.The webcam did get start up as it's light got switched on using the code but a blank window appeared and did not get any image.The console error showed as below,Please help with the error:

      Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: C:\Users\user\AppData\Local\Temp\javacpp417506457821\jniopencv_core.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.loadLibrary(Loader.java:422)
      at com.googlecode.javacpp.Loader.load(Loader.java:372)
      at com.googlecode.javacpp.Loader.load(Loader.java:319)
      at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:136)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at com.googlecode.javacpp.Loader.load(Loader.java:338)
      at com.googlecode.javacpp.Loader.load(Loader.java:319)
      at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:163)
      at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:163)
      at GrabberShow.run(GrabberShow.java:22)
      at java.lang.Thread.run(Unknown Source)

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

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

      Delete
  4. this code with me, in build ok, no problems, but when i am running, view error in netbeans ie "Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: no jnivideoInputLib in java.library.path"

    whats wrong?

    thanks

    ReplyDelete
    Replies
    1. Configure environment paths correctly..

      Delete
  5. sorry i want to ask mister,, what this code to running need JMF (Java Media FrameWork) because, when i am running, output error is no jnivideoInputLib in java.libary.path,, what is jnivideoInputLib in JMF

    thanks

    ReplyDelete
    Replies
    1. I don't think OpenCV/ JavaCV needs JMF to run. The error is due to something else. Be sure that you followed all steps in configuring the project. You can take help from this post :
      http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html

      Delete
  6. Hello GT.
    i got some errors, can you help me about them?

    -

    Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: C:\Users\tayfun\AppData\Local\Temp\jniopencv_core2555852947181398118.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:129)
    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.javacpp.Loader.load(Loader.java:285)
    at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:156)
    at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:155)
    at GrabberShow.run(GrabberShow.java:23)
    at java.lang.Thread.run(Unknown Source)
    -

    ReplyDelete
    Replies
    1. I think, you missed few steps in project configuration ( particularly - environment variable config in System Property) Follow this and check what you have missed :: http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html

      Delete
  7. it needs OPENCV in C:// folder :))

    ReplyDelete
  8. while running the following code:
    ---------------------------------------------------------------------------------
    package com.example;
    import static com.googlecode.javacv.cpp.opencv_core.cvFlip;

    import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; import com.googlecode.javacv.CanvasFrame?; import com.googlecode.javacv.FrameGrabber?; import com.googlecode.javacv.VideoInputFrameGrabber?; import com.googlecode.javacv.cpp.opencv_core.IplImage?; public class GrabberShow? implements Runnable {

    //final int INTERVAL=1000;///you may use interval IplImage? image; CanvasFrame? canvas = new CanvasFrame?("Web Cam"); public GrabberShow?() {
    canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    } @Override public void run() {
    FrameGrabber? grabber = new VideoInputFrameGrabber?(0); // 1 for next camera int i=0; try {
    grabber.start(); IplImage? img; while (true) {
    img = grabber.grab(); if (img != null) {
    cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise cvSaveImage((i++)+"-aa.jpg", img); // show image on window canvas.showImage(img);
    }
    //Thread.sleep(INTERVAL);
    }
    } catch (Exception e) { }
    }
    }
    --------------------------------------------------------------------------------
    I got the following exception:
    Exception in thread "Thread-3" java.lang.UnsatisfiedLinkError?: C:\Users\chotu\AppData?\Local\Temp\jniopencv_core2800598380084371592.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.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(Unknown Source) at com.googlecode.javacpp.Loader.load(Loader.java:335) at com.googlecode.javacpp.Loader.load(Loader.java:316) at com.googlecode.javacv.cpp.opencv_core$CvArr?.(opencv_core.java:158) at com.googlecode.javacv.VideoInputFrameGrabber?.grab(VideoInputFrameGrabber?.java:158) at com.example.GrabberShow?.run(GrabberShow?.java:25) at java.lang.Thread.run(Unknown Source)
    -----------------------------------------------------------------------
    I have also set the environment variables

    PATH=D:\app\chotu\product\11.1.0\db_2\bin;%SystemRoot?%\system32;%SystemRoot?%;%SystemRoot?%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell?\v1.0\;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\OpenCV2.1\bin;C:\opencv;C:\opencv\build\include;C:\opencv\build\include\opencv;C:\opencv\build\x86\vc10\lib;C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10;
    Please help..

    ReplyDelete
    Replies
    1. Can you please tell me the installation directory of opencv? It looks like you have installed opencv at two places. " C:\OpenCV2.1\ " and " C:\opencv ". Why you are doing so? I suggest you to re-install opencv once again and set remove duplicate paths and add new one only.

      The correct path should be :

      " C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\; " ......
      If you have installed opencv @ " C:\opencv\ "

      Delete
  9. Thank you very much for the source sample for capturing images via webcam. But how it is possible to capture a photo with the webcam when it is flipped 90 degrees. For example when I want to capture a person in vertical view? Can I make a live rotation of 90 degrees with JavaCV before capturing a foto?

    Thanks in advance for your help!

    ReplyDelete
  10. Can you please list the list of your articles about JavaCV? It would be greatly appreciated. Thank you.

    ReplyDelete
    Replies
    1. you can see them from here : http://ganeshtiwaridotcomdotnp.blogspot.com/search/label/OpenCV-JavaCV

      Delete
  11. I was trying to run the above code following all the given instructions, found an error as the following:

    Exception in thread "Thread-3" java.lang.UnsatisfiedLinkError: C:\Users\user\AppData\Local\Temp\javacpp13688201200720\jniopencv_core.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.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(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:334)
    at com.googlecode.javacpp.Loader.load(Loader.java:315)
    at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:158)
    at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:158)
    at GrabberShow.run(GrabberShow.java:20)
    at java.lang.Thread.run(Unknown Source)



    Then a blank window appears and the led of my integrated webcam is turned on. If I close the blank window, it gives the following msg.

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

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

    I have installed openCV in c: and have set the correct path as well. Can you please tell me why I am not able to see my video.

    ReplyDelete
    Replies
    1. I am also getting the same , GT could you help me . I installed opencv in specific folder and also set C:\Users\I067754>path
      PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Application Virtualization Client;c:\
      Program Files (x86)\Open Text\View\bin;C:\Program Files\ThinkPad\Bluetooth Software\;C:\Program Files\ThinkPad\Bluetooth Software\syswow64;C:\Program Files (x86)\Perforce;C:\P
      rogram Files (x86)\QuickTime\QTSystem\;C:\opencv\build\common\tbb\intel64\vc10\;C:\opencv\build\x64\vc10\bin\;
      Kindly help

      Delete
  12. For everyone who is here and have problem with java.lang.UnsatisfiedLinkError: no jnivideoInputLib in java.library.path" and using Maven. Your PATH is correct. This is because some JARs has not been added. In your pom.xml add this:

    <!-- main javacv artifact -->
    <dependency>
    <groupId>com.googlecode.javacv</groupId>
    <artifactId>javacv</artifactId>
    <version>0.2</version>
    </dependency>
    <!-- windows x86 natives for bridj -->
    <dependency>
    <groupId>com.googlecode.javacv</groupId>
    <artifactId>javacv</artifactId>
    <classifier>windows-x86</classifier>
    <version>0.2</version>
    </dependency>


    In the same way you can add natives for other systems, just change artifact classifier.

    Repository to use:

    <repository>
    <id>javacv</id>
    <url>http://maven2.javacv.googlecode.com/git</url>
    </repository>

    ReplyDelete
  13. GT - any idea if very slow video when forcing the FrameGrabber to the camera's native 5MP is related more to software or available hardware resources? Ideally I would like to run my stream at something like 1000x700 (but at the correct X:Y ratio) but then capture images at the fullest possible resolution available to the camera.

    So I'm okay with the video stream playing at 1280x1024, but I'd still want to capture a backend image at a higher resolution (I can sort out all that code later). Really, this is my question: if I want my video stream to play as smoothly at 1280x1024 as it does @ 640x480, is that just a hardware limitation (memory, CPU, GPU etc)? Or is there something I should change in terms of the software - buffers, etc?

    ReplyDelete
  14. Hi i executed code even i got the number of frames once after executing 5-6 time. i got the around 200+ odd frames but wen i exeute after that its not getting the frames,

    I m getting only the blank panel bu & there are no errors & its executed only once.Can u suggest abt this & along with this how can i make these frames to be caputed in time limit & in specific folder.

    ReplyDelete
    Replies
    1. I am not sure what is causing the error that you describing [ i got the number of frames once after executing 5-6 time. i got the around 200+ odd frames but wen i exeute after that its not getting the frames, ]
      , but I suspect your camera for this - just a guess. Please try with another one.

      You can use Timer and use file path instead of file name to achieve what you want. Do quick google search for these. It isn't much difficult to do that.

      Delete
  15. It gives me an error on the below line...
    "img = grabber.grab();"
    Give me solution....

    ReplyDelete
  16. Hello,

    I have a USB camera(PlayStaion Eye) which I want to use for capturing images. However, after installing the driver for that camera and changing 0 in "FrameGrabber grabber = new VideoInputFrameGrabber(0);" to 1 or 2 or ... I'm not able to capture from the USB camera. All numbers like 0 captures from default (built-in) camera instead of the USB camera!

    Any idea how can I fix this?
    Omid

    ReplyDelete
  17. Hi,

    I just followed the OpenCV-JavaCV : eclipse project configuration windows 7 directions, downloaded your code, and then tried to run the test file. When it runs, the GUI pops up with a white screen. The light to my integrated camera lights up to say it has turned on, but i still dont get an image. I get an error that starts off as: Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: C:\Users\Anthony2\AppData\Local\Temp\javacpp3986983340763\jniopencv_core.dll: Can't find dependent libraries.

    Any ideas to how I can resolve this issue?
    Anthony

    ReplyDelete
  18. heyy Ganesh, how can i record a video using javacv.??

    ReplyDelete
  19. I'm using JavaCV+OpenCV. This works great for me. I am doing all kinds of processing on every frame before handing the altered frames to the LiveCanvas and am still displaying at 30 fps! Nice!

    However, a LiveCanvas is TOTALLY USELESS for integrating a live image with your own application. A LiveCanvas is a SEPARATE WINDOW! That means you can't click on your window without it disappearing behind that window and it won't move with your application.

    Is there a way to display a live image on a panel or canvas or something which can be integrated with a normal application with typical UI elements?

    ReplyDelete
    Replies
    1. To display a live image on a panel:

      java.awt.Image dbImage;
      ...
      grabber = new OpenCVFrameGrabber(0);
      dbImage = grabber.grab().getBufferedImage();
      ...
      javax.swing.JPanel jPanelVideo = new javax.swing.JPanel();
      Graphics2D g2d = (Graphics2D) jPanelVideo.getGraphics();
      g2d.drawImage(dbImage, 0, 0, this);

      Delete
  20. Hi,
    I used opencv2.4.0 also camera did switched on but blank screen windo appeared and no image,please help with the error below:
    Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: C:\Users\user\AppData\Local\Temp\javacpp417506457821\jniopencv_core.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.loadLibrary(Loader.java:422)
    at com.googlecode.javacpp.Loader.load(Loader.java:372)
    at com.googlecode.javacpp.Loader.load(Loader.java:319)
    at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:136)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:338)
    at com.googlecode.javacpp.Loader.load(Loader.java:319)
    at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:163)
    at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:163)
    at GrabberShow.run(GrabberShow.java:22)
    at java.lang.Thread.run(Unknown Source)

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

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

    ReplyDelete
  21. Hello Ganesh i compiled ur code but i'm getting error please help me.....


    Class "grabbershow.GrabberShow" does not have a main method.

    ReplyDelete
  22. how can i capture the image?? its only showing me the live video, i want to capture the image, how can i do it??

    ReplyDelete
    Replies
    1. in the above code, the following line is capturing the image ...

      cvSaveImage((i++)+"-aa.jpg", img);

      If you need to do this in click action of a button, then you need to call this code from its action listener.

      Delete
  23. ***** VIDEOINPUT LIBRARY - 0.1995 - TFW07 *****

    SETUP: Setting up device 0
    SETUP: FaceTime HD Camera (Built-in)
    SETUP: Couldn't find preview pin using SmartTee
    SETUP: Capture callback set
    SETUP: Device is setup and ready to capture.

    ERROR: SampleCB() - buffer sizes do not match


    This is what it shows in the console on eclipse when I run it. Please help. No video input is seen but the webcam light turns on meaning it starts.

    ReplyDelete
  24. Hello, Ganesh, I try to run the above code. But the web cam starts well, but during executing the code grabber.grab(); throws the Exception as foolows

    Exception in thread "Thread-3" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at com.googlecode.javacpp.Loader.load(Loader.java:553)
    at com.googlecode.javacpp.Loader.load(Loader.java:532)
    at com.googlecode.javacv.cpp.opencv_core$CvArr.(opencv_core.java:156)
    at com.googlecode.javacv.VideoInputFrameGrabber.grab(VideoInputFrameGrabber.java:163)
    at main.java.GrabberShow.run(GrabberShow.java:23)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.IllegalStateException: Can't overwrite cause
    at java.lang.Throwable.initCause(Throwable.java:320)
    at com.googlecode.javacpp.Loader.load(Loader.java:581)
    at com.googlecode.javacpp.Loader.load(Loader.java:532)
    at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:134)
    ... 8 more
    Could you please find me some solution. I m stucked on it.

    ReplyDelete
  25. can live webcam image be shown in a JPanel instead of a CanvasFrame ???

    ReplyDelete

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