NOTE: Updated code with configuration and example is available here:
--
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(); } }
public static void main(String[] args) { GrabberShow gs = new GrabberShow(); Thread th = new Thread(gs); th.start(); } }
With me, it doesn't work with the VideoInputFrameGrabber but only with the OpenCVFrameGrabber
ReplyDeleteAnyway it worked. That's great news.
ReplyDeletePls help me with following error.Ive configured C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\;
Deletepath 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.
Make sure javaCV version matches openCV version. JavaCV 2.4.4 works with OpenCV 2.4.4 not 2.4.0
DeleteI'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();?
ReplyDelete0 is for default camera. try using 0 instead of 1.
DeleteFrameGrabber grabber = new VideoInputFrameGrabber(0);
Hi,
DeleteI 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.
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"
ReplyDeletewhats wrong?
thanks
Configure environment paths correctly..
Deletesorry 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
ReplyDeletethanks
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 :
Deletehttp://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/opencv-javacv-eclipse-project.html
Hello GT.
ReplyDeletei 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)
-
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
Deleteit needs OPENCV in C:// folder :))
ReplyDeletewhile running the following code:
ReplyDelete---------------------------------------------------------------------------------
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..
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.
DeleteThe correct path should be :
" C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\; " ......
If you have installed opencv @ " C:\opencv\ "
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?
ReplyDeleteThanks in advance for your help!
Can you please list the list of your articles about JavaCV? It would be greatly appreciated. Thank you.
ReplyDeleteyou can see them from here : http://ganeshtiwaridotcomdotnp.blogspot.com/search/label/OpenCV-JavaCV
DeleteI was trying to run the above code following all the given instructions, found an error as the following:
ReplyDeleteException 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.
I am also getting the same , GT could you help me . I installed opencv in specific folder and also set C:\Users\I067754>path
DeletePATH=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
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:
ReplyDelete<!-- 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>
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.
ReplyDeleteSo 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?
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,
ReplyDeleteI 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.
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, ]
Delete, 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.
It gives me an error on the below line...
ReplyDelete"img = grabber.grab();"
Give me solution....
What's the error?
DeleteHello,
ReplyDeleteI 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
Hi,
ReplyDeleteI 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
heyy Ganesh, how can i record a video using javacv.??
ReplyDeleteI'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!
ReplyDeleteHowever, 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?
To display a live image on a panel:
Deletejava.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);
Hi,
ReplyDeleteI 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.
Hello Ganesh i compiled ur code but i'm getting error please help me.....
ReplyDeleteClass "grabbershow.GrabberShow" does not have a main method.
how can i capture the image?? its only showing me the live video, i want to capture the image, how can i do it??
ReplyDeletein the above code, the following line is capturing the image ...
DeletecvSaveImage((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.
***** VIDEOINPUT LIBRARY - 0.1995 - TFW07 *****
ReplyDeleteSETUP: 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.
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
ReplyDeleteException 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.
can live webcam image be shown in a JPanel instead of a CanvasFrame ???
ReplyDelete