Saturday, December 18, 2010

Capture photo / image from Web Cam / USB Camera using Java

In this post we will show how to use a USB camera to capture a picture from inside a Java application..
We will use the library LTI-CIVIL which is part of FMJ (Freedom for Media in Java)



Let's go through the needed steps:

1.Download LTI-CIVIL:

Which is a Java library for capturing images from a video source such as a USB camera. It provides a simple API and does not depend on or use JMF!
The advantage behind this library that it doesn't depend on JMF (which is almost very old now)
The project URL: http://lti-civil.org/
Download URL: http://sourceforge.net/projects/lti-civil/files/

Extract the zip/tar file and you will find 2 things we will use:
a) lti-civil-no_s_w_t.jar
b) native folder contain the native libraries according to the operating system , pick the one for the system you are using.


2.Create the Java App:

Add to the class-path of the project the jar file and the native library folder.
To Run the project as jar file , you can use the JVM parameter:

-Djava.library.path=".....\native\win32-x86" to add the library to the class path of the jar file.


3.Create a class file:

Name it like TestWebCam implements CaptureObserver

4. Define the following instance variables:

JButton start = null;
JButton shot = null;
JButton stop = null;
CaptureStream captureStream = null;
boolean takeShot=false;

5.In the constructor:

Add the following code to initialize the capturing device:

public TestWebCam() {
CaptureSystemFactory factory = DefaultCaptureSystemFactorySingleton.instance();
CaptureSystem system;
try {
system = factory.createCaptureSystem();
system.init();
List list = system.getCaptureDeviceInfoList();
int i = 0;
if (i < list.size()) {
CaptureDeviceInfo info = (CaptureDeviceInfo) list.get(i);
System.out.println((new StringBuilder()).append("Device ID ").append(i).append(": ").append(info.getDeviceID()).toString());
System.out.println((new StringBuilder()).append("Description ").append(i).append(": ").append(info.getDescription()).toString());
captureStream = system.openCaptureDeviceStream(info.getDeviceID());
captureStream.setObserver(TestWebCam.this);
}
} catch (CaptureException ex) {
ex.printStackTrace();
}
//UI work of the program
JFrame frame = new JFrame();
frame.setSize(7000, 800);
JPanel panel = new JPanel();
frame.setContentPane(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
start = new JButton("Start");
stop = new JButton("Stop");
shot = new JButton("Shot");
panel.add(start);
panel.add(stop);
panel.add(shot);
panel.revalidate();
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
captureStream.start();
} catch (CaptureException ex) {
ex.printStackTrace();
}
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
captureStream.stop();
} catch (CaptureException ex) {
ex.printStackTrace();
}
}
});
shot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
takeShot=true;
}
});
}

6.Add the following 2 methods mandated by the implemented interface CaptureObserver:

public void onNewImage(CaptureStream stream, Image image) {
if(!takeShot) return;
takeShot=false;
System.out.println("New Image Captured");
byte bytes[] = null;
try {
if (image == null) {
bytes = null;
return;
}
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(os);
jpeg.encode(AWTImageConverter.toBufferedImage(image));
os.close();
bytes = os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
bytes = null;
} catch (Throwable t) {
t.printStackTrace();
bytes = null;
}
if (bytes == null) {
return;
}
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
File file = new File("/img" + Calendar.getInstance().getTimeInMillis() + ".jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
BufferedImage myImage = ImageIO.read(file);
shot.setText("");
shot.setIcon(new ImageIcon(myImage));
shot.revalidate();
} catch (IOException ex) {
ex.printStackTrace();
}
}

public void onError(CaptureStream stream, CaptureException ce) {
System.out.println("Error!");
}

7.Optionally, to Covert image to Gray Scale:

In case you need to convert it into Gray scale (for image processing purposes you can replace the following line with this block:

Old Line:
shot.setIcon(new ImageIcon(myImage));

New Block:

//Convert myImage to gray scale
BufferedImage imageGray = new BufferedImage(myImage.getWidth(), myImage.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = imageGray.getGraphics();
g.drawImage(myImage, 0, 0, null);
g.dispose();
shot.setIcon(new ImageIcon(imageGray));


8.Add main method to this class to run it:

public static void main(String args[])
throws Exception {
TestWebCam test = new TestWebCam();
}

9.Connect the USB Camera and run the application...

It should show 3 buttons : Start , Stop and Shot..
Click on start , then on shot, it will get you the current web camera image, move the web camera and take another shot by click on the picture to update it.


**Possible Issues:

1.Native library is not in the classpath.
2.Wrong native library is used.
3.USB Camera is not correct connected or installed.
4.Another program is running connected to the camera resource (you may be running your program 2 times)

You can use the CaptureStream parameter as well, but i think if you need to capture a streaming web cam , it would better to use images with intervals.


*** UPDATE: based on a lot of requests asking about using it to capture videos, here is the new post discussing the topic in details:

WebCam Video Capture in Java

Hope this help more to address this issue in particular.

*** UPDATE|: based on number of requests to access the web camera from JSP file , here is a link to a post discussing the how to in details:

Accessing Webcam Within JSP


*** UPDATE: if you want sample code , please download my other open source project Interactive4J, it has example for video and images.

204 comments:

  1. heei thanks for this tutorial.
    But I have a problem with the folowing line:

    JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(os);

    my eclipse says: "JPEGImageEncoder cannot be resolved to a type"

    do you have a solution for this problem?
    thanks and greetings

    Pundidas

    ReplyDelete
  2. Ensure you are using Java 5 and Sun JDK.
    Or replace it with the code used in post http://osama-oransa.blogspot.com/2010/12/using-gmail-to-send-emails-with.html to send jpg in email, it highlight how to construct JPG.

    ReplyDelete
    Replies
    1. import com.sun.image.codec.jpeg.*;
      and add rt.jar to your project from Jre's lib folder


      Harry :)

      Delete
  3. thanks for answering.
    it works with Java 5 but im normally using Java 6, so I'll try to build it in Java 6. The issue is to change the JPEGImageEncoder thing, due to it dosn't exists in Java 6 I think.

    ReplyDelete
  4. Hi,

    First of all thank you very much for providing such a typical solution.

    I am using fedora-core-12 64bit and set the path like

    CLASSPATH=/home/guest/Downloads/webcam/lti-civil-20070920-1721/lti-civil-no_s_w_t.jar:$CLASSPATH

    LD_LIBRARY_PATH=/home/guest/Downloads/webcam/lti-civil-20070920-1721/native/linux-x86:/home/guest/Downloads/webcam/lti-civil-20070920-1721/native/linux-amd64:$LD_LIBRARY_PATH

    but when i am running this sample i got an error message like :

    Exception in thread "main" java.lang.NoClassDefFoundError: TestWebCam
    Caused by: java.lang.ClassNotFoundException: TestWebCam
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Could not find the main class: TestWebCam. Program will exit.

    ReplyDelete
  5. This means the class loader couldn't load the class due to exception in the constructor , You may need to identify the actual exception first to fix.

    ReplyDelete
  6. Add to the class-path of the project the jar file and the native library folder.
    To Run the project as jar file , you can use the JVM parameter:

    -Djava.library.path=".....\native\win32-x86" to add the library to the class path of the jar file.

    i never understand this clearly.
    i try these and add jar to project but i got following error:
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at TestWebCam.(TestWebCam.java:44)
    at TestWebCam.main(TestWebCam.java:178)
    i cant fix this
    please help me.
    thanks for reading this.

    ReplyDelete
  7. This means the library is not loaded , if you run using command line you need to add the -D option , if you are running it from inside IDE , go to run options/configurations and add this -D option to Runtime , Java options/arguments.

    ReplyDelete
  8. Referring to the problem tuhin is facing, maybe I know how to add it in Eclipse IDE?

    ReplyDelete
  9. It is easily in Eclipse:

    From Eclipse Run --> Open Run Dialog (or Run configuration) --> Arguments tab --> VM Arguments and place -D.....

    ReplyDelete
  10. Getting following error on running the aforementioned program on Ubuntu.

    Is this some webcam driver issues ? Please help.

    Device ID 0: /dev/video0
    Description 0: /dev/video0
    *** glibc detected *** /usr/lib/jvm/java-6-openjdk/bin/java: free(): invalid pointer: 0xb4d2dcb0 ***
    ======= Backtrace: =========
    /lib/libc.so.6(+0x6c501)[0x224501]
    /lib/libc.so.6(+0x6dd70)[0x225d70]
    /lib/libc.so.6(cfree+0x6d)[0x228e5d]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x30885c)[0x12a185c]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x21a2ec)[0x11b32ec]
    /home/local/PERSISTENT/surbhi_gupta/Downloads/lti-civil/native/linux-x86/libcivil.so(_ZN7JNIEnv_18ReleaseStringCharsEP8_jstringPKt+0x1f)[0x409a5f]
    /home/local/PERSISTENT/surbhi_gupta/Downloads/lti-civil/native/linux-x86/libcivil.so(Java_com_lti_civil_impl_jni_NativeCaptureSystem_openCaptureDeviceStream+0x12e)[0x408c74]
    [0xb572b0dd]
    [0xb5724463]
    [0xb5723e21]
    [0xb5721366]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x209932)[0x11a2932]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x30a509)[0x12a3509]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x20886f)[0x11a186f]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x213d0c)[0x11acd0c]
    /usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm.so(+0x222d6d)[0x11bbd6d]
    /usr/lib/jvm/java-6-openjdk/bin/java(JavaMain+0xd45)[0x804aef5]
    /lib/libpthread.so.0(+0x5cc9)[0x1a3cc9]
    /lib/libc.so.6(clone+0x5e)[0x2886ae]
    ======= Memory map: ========
    00110000-00132000 r-xp 00000000 08:07 27350 /usr/lib/jvm/java-6-openjdk/jre/lib/i386/libjava.so
    00132000-00133000 r--p 00021000 08:07 27350 /usr/lib/jvm/java-6-openjdk/jre/lib/i386/libjava.so
    ....
    ....
    ....
    opened v4l2 device
    Found UVC Camera (046d:0994) card with uvcvideo v4l2 driver
    discover_inputs()
    Found sources: 1
    0 - Camera 1 (2)
    unknown or unsupported format: 1196444237

    ReplyDelete
  11. For me it seems driver issue as the service already discovered but the format is not supported , can you check the supported formats?

    ReplyDelete
  12. Thanks for the reply.

    The program worked for me on windows. Can I capture the webcam images through command line without the swing GUI. I tried but its asking for Quick time. Can you please suggest some alternative ?

    ReplyDelete
    Replies
    1. Hi Surbahi,can you please guide me How the Image capyure worked in case of you. I also followed the same steps described in blog.but gettinf exception.code is able to detect device

      DSCaptureException::FailWithException: hr=0x800705aa.
      com.lti.civil.CaptureException: pMediaControl->Run() failed: 0
      at com.lti.civil.impl.jni.NativeCaptureStream.nativeStart(Native Method)
      at com.lti.civil.impl.jni.NativeCaptureStream.start(NativeCaptureStream.java:52)
      at com.ample.TestWebCam$1.actionPerformed(TestWebCam.java:74)



      help will be appricated.
      my mail Id is amar_it05@yahoo.com

      plz provide me steps I need it urgently

      Delete
  13. Yea , you can if you refer to other open source projects you can find a way :

    http://osama-oransa.blogspot.com/2011/01/hidden-parent-eye.html

    http://osama-oransa.blogspot.com/2010/12/java-home-monitor-application.html

    Simply remove the JFrame :) or setVisible(false) for it.

    ReplyDelete
  14. thanks for posting this code but i have a question:
    what do u mean by: implements CaptureObserver ? the class TestWebCam is abstract?

    can u post a full code plz
    plz help, i have been searching for days of ways to capture webcam image from java code

    ReplyDelete
  15. When you make it implement this interface you must add these 2 methods to the class: public void onNewImage(CaptureStream stream, Image image) & public void onError(CaptureStream stream, CaptureException ce)...

    For a working example plz have a look on the other open source projects in my blog: http://osama-oransa.blogspot.com/2010/12/java-home-monitor-application.html

    ReplyDelete
  16. hi.. I'am new with JMF.. i need to capture image using a laptop webcam. i used JMF it running but it cannot save image i capture which i need most. can you help me?? hopeyou can reply.. god bless

    ReplyDelete
  17. This post is talking about FJM not JMF , you can follow the steps easily or use Interactive4J open source code to do this.

    ReplyDelete
  18. public class SwingCapture extends Panel implements ActionListener
    {
    public static Player player = null;
    public CaptureDeviceInfo di = null;
    public MediaLocator ml = null;
    public JButton capture = null;
    public Buffer buf = null;
    public Image img = null;
    public VideoFormat vf = null;
    public BufferToImage btoi = null;
    public ImagePanel imgpanel = null;

    public SwingCapture()
    {
    setLayout(new BorderLayout());
    setSize(320,550);

    imgpanel = new ImagePanel();
    capture = new JButton("Capture");
    capture.addActionListener(this);

    String str1 = "vfw:Logitech USB Video Camera:0";
    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();

    try
    {
    player = Manager.createRealizedPlayer(ml);
    player.start();
    Component comp;

    if ((comp = player.getVisualComponent()) != null)
    {
    add(comp,BorderLayout.NORTH);
    }
    add(capture,BorderLayout.CENTER);
    add(imgpanel,BorderLayout.SOUTH);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }



    public static void main(String[] args)
    {
    Frame f = new Frame("ImageCapture");
    SwingCapture cf = new SwingCapture();

    f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    playerclose();
    System.exit(0);}});

    f.add("Center",cf);
    f.pack();
    f.setSize(new Dimension(320,550));
    f.setVisible(true);
    }


    public static void playerclose()
    {
    player.close();
    player.deallocate();
    }


    public void actionPerformed(ActionEvent e)
    {
    JComponent c = (JComponent) e.getSource();

    if (c == capture)
    {
    // Grab a frame
    FrameGrabbingControl fgc = (FrameGrabbingControl)
    player.getControl("javax.media.control.FrameGrabbingControl");
    buf = fgc.grabFrame();

    // Convert it to an image
    btoi = new BufferToImage((VideoFormat)buf.getFormat());
    img = btoi.createImage(buf);

    // show the image
    imgpanel.setImage(img);

    // save image
    saveJPG(img,"c:\\test.jpg");
    }
    }

    class ImagePanel extends Panel
    {
    public Image myimg = null;

    public ImagePanel()
    {
    setLayout(null);
    setSize(320,240);
    }

    public void setImage(Image img)
    {
    this.myimg = img;
    repaint();
    }

    public void paint(Graphics g)
    {
    if (myimg != null)
    {
    g.drawImage(myimg, 0, 0, this);
    }
    }
    }


    public static void saveJPG(Image img, String s)
    {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);

    FileOutputStream out = null;
    try
    {
    out = new FileOutputStream(s);
    }
    catch (java.io.FileNotFoundException io)
    {
    System.out.println("File Not Found");
    }

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(0.5f,false);
    encoder.setJPEGEncodeParam(param);

    try
    {
    encoder.encode(bi);
    out.close();
    }
    catch (java.io.IOException io)
    {
    System.out.println("IOException");
    }
    }

    }

    can you help me about this code the erros is

    Exception in thread "main" java.lang.NullPointerException
    at SwingCapture.(SwingCapture.java:39)
    at SwingCapture.main(SwingCapture.java:65)
    Java Result: 1

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. I am not sure where is the exception line number as you didn't number the post, so most probably you are missing to add the native library to the Java class-path, so try to add this as well..

    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";

    ReplyDelete
  21. MR.OSAMA ORASA is it possible that you can program in j2me about browsing mobile inbox message??? if it is possible can you give me a runnable code for it.. thank you very much in advance :))

    ReplyDelete
  22. You can't do this in J2ME but you can use Android for that.

    ReplyDelete
  23. mr. osama can you hepl me. I'am doing work with computer vision my platform is J2SE in netbeans with JDK 1.6 with JMF. it possible for that?? my project is capture image and save it using Java.

    ReplyDelete
  24. If you follow the post steps you can do this easily.

    ReplyDelete
  25. good day, we tried following yours steps like downloading and adding library and a classpath, we seem have a problem configuring the class path since we are using netbeans 7.0 and using jdk 6.0..should we use a higher jdk.. by the way,when we create the program with the codes above, there are so many errors..we can't solve this problem can you help us? specialy on creating class path.. we are very new to this..thank you so much in advance :)

    ReplyDelete
  26. it should work fine in netbeans 7.0 and jdk 6.0, do you get compilation errors ? if yes then you didn't add all the jars to class path correctly, if you mean runtime error then you need to load the native lib during running it by using -D

    ReplyDelete
  27. good day mr. Osama.i used JMF in java what is the problem about this line: Player player = Manager.createRealizedPlayer(deviceInfo.getLocator()); here the error says:
    Exception in thread "main" java.lang.NullPointerException
    at Shot.FrameGrab.main(FrameGrab.java:19)

    i hope you can reply..

    ReplyDelete
  28. Add System.out.println(""+deviceInfo.getLocator());
    Just above this line and see if null pointer occur ..
    The device info should be initialized by something like (before that line)
    String value= "vfw:Microsoft WDM Image Capture (Win32):0";
    DeviceInfo deviceInfo = CaptureDeviceManager.getDevice(value);

    ReplyDelete
  29. hey Osama,

    Thanks for the url of ITI-CIVIL in ur blog ....it helped me pretty much..in solving my problem of displaying live video & then capturing the image Thanq..

    with regards,
    Amala

    ReplyDelete
  30. mr. osama can you hepl me. i dont know about cannot find symbol class capturestream.. iam newbie in java.. thx..

    ReplyDelete
  31. Add the jars to the class path as mentioned above and the compilation error will be resolved.

    ReplyDelete
  32. mr osama can u help me, im confuse for adding import class requirement for this project.. there's such error like cannot find symbol class ByteArrayInputStream and others.. sory, iam newbie in java..

    ReplyDelete
  33. This means you are not configuring the Java JDK itself correctly, this class should be imported like :
    import java.io.ByteArrayInputStream;

    Plz check your classpath and add the needed jars and JDK, you can google on how to do this on the IDE you are using.

    ReplyDelete
  34. Hello Sir
    I am used your code in netbeans
    how can used jar file & liabrary file pls tell me

    ReplyDelete
  35. In NetBeans it is easily to add jars to classpath and in run add the JVM param using -D as specified in the steps.

    ReplyDelete
  36. Hello Sir Can You Pls Send
    lti-civil.zip file on my mail id because some proble of dowloading of your link
    pls sir
    my email id is : nikunjrathod77@gmail.com

    ReplyDelete
  37. hello sir i have this error, im using netbeans..

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at aa.TestWebCam$1.actionPerformed(TestWebCam.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)

    ReplyDelete
  38. This means you have no USB connected to the machine.

    ReplyDelete
  39. mr osama, i can running the program.. can I see the image area that will captured by the webcam? and can i capture continuously from webcam within the specified time? thanks in advance

    ReplyDelete
  40. Yea, you can of-course , you check another open source project Interactive4J in this blog contain example of how to make video/images from web cam in easy way , you can take the source code and use it directly.

    ReplyDelete
  41. Hello Sir
    http://sourceforge.net/projects/lti-civil/files/
    this link is not working properly so pls send me

    LTI-CIVIL.zip file
    my email id is
    nikunjrathod77@gmail.com

    ReplyDelete
  42. It is working fine with me , please try to connect using different internet connection.

    Send failed due to security error as the zip contain some executable files, so please try to download it.

    ReplyDelete
  43. thank you sir
    i can download .zip file
    sir i am very new in java so how can set the
    jar file and native liabrary
    i was using jdk1.6 and the java class path is
    c:/j2sdk1.4.2/bin
    so pls tell me how can set .jar file and native file i was using window

    ReplyDelete
  44. There is only 1 advice try to use Google to do what you want :) , how did you get my blog ? by search , so you need to do the same for each issue you face , here is a link for this : http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html

    ReplyDelete
  45. Hello OSAMA, thx for this nice POST....i am trying ur Code but i get an exception,
    u allready answer that we hav to add an argument to eclipse, but can u explain wich argument i have to write in VM argument window? Thx.....

    Exception is:
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at TestWebCam.(TestWebCam.java:48)
    at TestWebCam.main(TestWebCam.java:163)

    ReplyDelete
  46. Add this argument:
    -Djava.library.path=".....\native\win32-x86"

    ..... = parent folders of this Civil library.

    ReplyDelete
  47. i want to access this applet in jsp page How can I?

    ReplyDelete
    Replies
    1. There is another post describing that for using web cam in JSP file.

      Delete
  48. Using the applet in JSP page is the same as using it in any normal html.
    But You can't use this in applet because of the Native Library.

    ReplyDelete
  49. why is this?

    error: package com.sun.image.codec.jpeg does not exist
    import com.sun.image.codec.jpeg.JPEGCodec;

    isnt this package part of the API??
    im through JDK 7.

    ReplyDelete
    Replies
    1. No , it is not , you can replace it with your own code. or use old JDK.

      Delete
    2. it might be removed as it is Sun file and could be removed at any JDK release.

      Delete
  50. From the Java Docs 1.5 you can see: "This class is a factory for implementations of the JPEG Image Decoder/Encoder.

    Note that the classes in the com.sun.image.codec.jpeg package are not part of the core Java APIs. They are a part of Sun's JDK and JRE distributions. Although other licensees may choose to distribute these classes, developers cannot depend on their availability in non-Sun implementations. We expect that equivalent functionality will eventually be available in a core API or standard extension."

    ReplyDelete
  51. thanks for the reply..hmm..i got it... sooo..what I shal do?
    any alternatives? pls.

    ReplyDelete
  52. Hi,
    I have tried to create all classes in "com.sun.image.codec.jpeg" and refer to those classes without refering to the particular package.
    Will it work out??
    while im trying to implement this strategy.
    I have got strucked at following classes,
    JPEGEncodeParam and JPEGQTable.

    ReplyDelete
  53. You may include the JAR file from the JRE or get them from Sun JRE as a source code and include them in your project if this is includes a small set of classes, or you can search for any other option available on the internet to provide the same function of encoding.

    ReplyDelete
  54. ok..thank you for your time. ill try ma best. regards.

    ReplyDelete
  55. hello i m beginner java user how we set class path in netbeans

    ReplyDelete
  56. I advice you to search on every thing using Google and you will find the answer...

    http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html

    ReplyDelete
  57. can i use this library in applet?

    ReplyDelete
  58. No , you can't >>> you can use web start instead...

    ReplyDelete
    Replies
    1. Hi Osama,

      Could you explain me please how to load this application as a java web start application?

      Thanks in advance.

      Delete
    2. Use this tutorial :
      http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/

      Delete
  59. hello sir i have this error, im using netbeans..

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at aa.TestWebCam$1.actionPerformed(TestWebCam.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    August 4, 2011 8:00 AM
    whereas my webcam is connected to machine
    please suggest some advice

    ReplyDelete
  60. This means USB camera is not connected or not recognized , can you try it with another program that uses it.

    ReplyDelete
  61. hi sir, when i click start button i am able to see video streaming.. could you help me please, what is the problem

    ReplyDelete
  62. You can click on the image to change it , or create a thread that do the same to refresh the image every x time , may be 1 second according to the smooth appearance you want to have... the less the time the more the smooth you get.

    ReplyDelete
  63. Hi sir gud mor i followed ur steps even i dnt ve any prob in compiling but when i click the start button my web cam is not opening can u provide me the details !!!
    i am facing dis prob in Eclipse tool

    ReplyDelete
  64. Any exception is thrown ? can you test your web cam using another application as well ? is the java class path is set correctly and dynamic library ? can you try running the jar from the command line ?

    ReplyDelete
  65. My web cam is running properly pls can u send me the steps to run through command prompt...
    Thanks for ur previous reply!!

    ReplyDelete
  66. Use java -jar your_jar_name -Djava.library.path=".....\native\win32-x86"

    -D will load the native library...

    ReplyDelete
  67. HI SIR,Can u say how to Save the image in a gray format , i followed all ur steps every thing s working fine but the problem arises when saving the image it s being saved only in coloured format even though i apply gray scale conversion as u specified!!!looking for ur reply!!

    ReplyDelete
  68. After converting you need to save the image : so save myImage here :
    //Convert myImage to gray scale
    BufferedImage imageGray = new BufferedImage(myImage.getWidth(), myImage.getHeight(),
    BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = imageGray.getGraphics();
    g.drawImage(myImage, 0, 0, null);
    ***** Save the image here ******
    g.dispose();
    shot.setIcon(new ImageIcon(imageGray));

    ReplyDelete
  69. This solution run over windows 7 of 64 bits

    ReplyDelete
  70. how to run it am not able to get it plzz help me.explain in detail plz

    ReplyDelete
  71. Sorry , what was your issue , explain so i can help.

    ReplyDelete
  72. Please help me .How to display live video from webcam on my Frame. .and from that live video i want to take a picture.

    ReplyDelete
  73. Follow the steps and create a thread to capture the image in a rate (the fast the better may be 1 image per seconds or 12 per seconds)..display the images will give you the feeling of the video and you can store the image you want.

    ReplyDelete
  74. Hi sir,Please help me.I want to display a live video from my webcam on my jsp page using jmf (As you said that we cannot use fmj because of native library).

    ReplyDelete
  75. I think the only way is to use plug-in for the browser , and i can see many sites using Flash Player for that as it can capture from webcam.

    ReplyDelete
  76. Hi sir,i get following error during run time..please help me to overcome it..please..
    Exception in thread "main" com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: /home/liveuser/lti-civil/native/linux-x86/libcivil.so: libstdc++.so.5: cannot open shared object file: No such file or directory
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at Cam.main(Cam.java:26)
    Caused by: java.lang.UnsatisfiedLinkError: /home/liveuser/lti-civil/native/linux-x86/libcivil.so: libstdc++.so.5: cannot open shared object file: No such file or directory
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1807)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1732)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 1 more

    ReplyDelete
    Replies
    1. I can see the following exception : java.lang.UnsatisfiedLinkError means the native library can't be linked , so check the path : /home/liveuser/lti-civil/native/linux-x86/libcivil.so
      Check the privileges of this user to access it , and check the Linux/OS version you are using..

      Delete
  77. In Fedora14 linux i run a program that captures image from usb Webcam gives error like
    [liveuser@localhost bin]$ /home/liveuser/lti-civil/bin/java -classpath we.jar -Djava.library.path="native/linux-x86" com.lti.civil.test.Initial
    Device ID 0: /dev/video0
    Description 0: /dev/video0
    opened v4l2 device
    Found USB2.0 PC CAMERA card with uvcvideo v4l2 driver
    discover_inputs()
    Found sources: 1
    0 - Camera 1 (2)
    unknown or unsupported format: 1448695129
    Exception in thread "main" com.lti.civil.CaptureException: unknown or unsupported format: 1448695129
    at com.lti.civil.impl.jni.NativeCaptureSystem.openCaptureDeviceStream(Native Method)
    at com.lti.civil.test.Initial.main(initial.java:46)

    Sir,kindly help..why this is happening and how can i resove it??

    ReplyDelete
    Replies
    1. The camera is detected but it can't open the device to capture image , are you able to use it from other programs with no issues ?

      Delete
  78. yes sir. i can capture image by cheese program in fedora 14 but through java code using lti-civil..the upper mentioned runtime error is occuring..
    Sir please help,what should i do?

    ReplyDelete
  79. how to make jar file tht run on all computers

    ReplyDelete
    Replies
    1. Create 1 jar file and create .bat or .sh files for each system that refer to the native libraries.

      Delete
  80. I was Trying to run above program in NETBEANS 7 but i got error please help me..
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
    required: CaptureSystemFactory
    found: com.lti.civil.impl.jni.NativeCaptureSystemFactory
    at DefaultCaptureSystemFactorySingleton.instance(DefaultCaptureSystemFactorySingleton.java:24)
    at TestWebCam.(TestWebCam.java:25)
    at TestWebCam.main(TestWebCam.java:87)

    ReplyDelete
    Replies
    1. This seems conflicts in your class-path check the jars (remove unnecessary) and retry...

      Delete
  81. Hi,
    I am using java1.6 for running the prog given by u....i am getting 4 warning in cmd.........

    com.sun.image.codec.jpeg.* is Sun proprietary API nad may be removed in future release

    Sir can u plz suggest me the solution for that.......nd also tried to install jdk1.5 but my processor is not compatible with it.......... plz rply as soon as possible.....i need it!!!
    thanks

    ReplyDelete
    Replies
    1. Ignore these warnings or use suppress warnings.

      Thanks,
      Osama

      Delete
  82. I have the following exceptions and it doest not show anything an clicking START button nd i am using jdk1.6:
    Exception :
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java
    .library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem
    (NativeCaptureSystemFactory.java:24)
    at TestWebCam.(TestWebCam.java:45)
    at TestWebCam.main(TestWebCam.java:141)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem
    (NativeCaptureSystemFactory.java:21)
    ... 2 more

    Plz rply fast...........

    ReplyDelete
    Replies
    1. Please specify the native library path correctly refer to point#2.

      Delete
  83. Sir i have given
    classpath
    variable value = D:\new webcam\lti-civil-20070920-1721\native\win32-x86;D:\new webcam\lti-civil-20070920-1721\lti-civil-no_s_w_t.jar;

    is anything extra to be done???

    ReplyDelete
  84. no, you should configure the native library as well , 1st parameter in your class-path is native library ... so it should be passed as parameter to the JVM: -D........
    This is a JVM parameter and need to be set from Run properties in Netbeans or eclipse or from the command line if you run your project from the command line.

    ReplyDelete
  85. Thanks a lot sir,
    The prog run smoothly in netbeans but i still could run it in command line............and can u give some idea of video capturing also to be embedded with this program only!!!!!!!!!!
    I shall be highly thankful to you............

    ReplyDelete
    Replies
    1. For video capturing , simply create a thread to capture the images in speed x images per seconds and put it in the same place it will looks like a video exactly.

      The other option , you can download my Interactive4J open source and get the needed code to do the same.

      Delete
  86. Sir,
    Can u plz tell me exactly where to use thread i.e for which func in prog u have given above for vedio..actually i have tried alot but it is not working properly!!!!!!!!!
    i shall be thankful to u.........
    and thanks for info u have given me!!!!!!!!

    ReplyDelete
    Replies
    1. Please check my open source code , it do all what you need.
      Thanks..

      Delete
  87. i am using netbeans ide and i added a lib directory to libraries consisting of lti-civil-no_s_w_t.jar and swt.jar and i am using w7 32 bit so i added the dl folders accoring to it on the lib

    but when i try to run the file


    om.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at googlecv.TestWebCam.(TestWebCam.java:54)
    at googlecv.TestWebCam.main(TestWebCam.java:160)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 2 more


    this error s showing can u please tell me how to resoulve this

    ReplyDelete
  88. hi i am using netbeans to run the above code

    i am using windows7 os 32 bit

    i added a lib folder containing lti-civil-no_s_w_t.jar and swt.jar and the .dll files needed for windows 32 bit

    but when i try to run the above code i am getting this error . can u please tell me how to fix it

    om.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at googlecv.TestWebCam.(TestWebCam.java:54)
    at googlecv.TestWebCam.main(TestWebCam.java:160)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 2 more

    ReplyDelete
    Replies
    1. Please add the jvm parameter -D ...... with the correct library.

      Delete
  89. Sir, thanks for previous info nd i am able to run vedio and image capturing smoothly ...........
    can sir plz help me out in making face detection and face comparison application.....actully i have done face detection but could save the image and compare with other saved image...............
    thanks in advance.......

    plz help me out!!!!!!!!!!!!

    ReplyDelete
    Replies
    1. This need statisitcal analysis, so you need to dig more on statistical analysis.

      Delete
  90. Hi thanks for the code help. I want to capture image automatically with the webcam. The user should not click to capture the image. I had used JTwain.jar and Asprise.dll but its a trial version. Can u plz help i am in very much need of it.
    Thanks

    ReplyDelete
    Replies
    1. You can check the code in Interactive4J for that or Home monitor application..

      Delete
  91. I want to capture image from my web application through applet,can i use this library in my applet code.

    Thanks in advance.......

    Please help me out

    ReplyDelete
  92. Thanks for your valuable post,
    I got this application executed, but I found that it doesn't show the video preview of the webcam so that we can take the exact snapshot of the video(image).
    Could you please help to resolve this issue.

    Thanks & Regards,
    Rasheed

    ReplyDelete
    Replies
    1. You can show the video by using a thread to capture the images in the speed you need e.g. 14 frame per second, you may use this in my open source project Interactive4J.

      Delete
    2. Thank you so much for your reply.
      I tried your suggestion to display the video preview of the webcam, but I couldn't get the exact location to put the code, I request you to provide the code to get the webcam video.
      Thank you very much.

      Regards,
      Rasheed

      Delete
    3. Hi Rasheed,
      Here is a new post talking about how to capture video in your application..

      http://osama-oransa.blogspot.com/2012/04/webcam-video-capture-in-java.html

      Thanks,
      Osama

      Delete
  93. I've opened the url above, but I couldn't get the exact location to put the code, I request you to provide the code to get the webcam video.
    Thank you very much. tory

    ReplyDelete
    Replies
    1. Can you download interactive4j , you will find all the details ... the post is explaining it.

      Delete
    2. thanks, now I can capture the picture with the camera privew. But can you help me to set the image size? tahnks

      Delete
  94. dude thx 4 sharing! but i've got little problem here. it's on an exception when i press start button

    Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: com.lti.civil.impl.jni.NativeCaptureStream.start()V
    at com.lti.civil.impl.jni.NativeCaptureStream.start(Native Method)
    at ticketing.WebCam$1.actionPerformed(WebCam.java:56)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

    ReplyDelete
    Replies
    1. The native library is not linked correctly, please refer to the post again in the -D parameter .. or see previous replies for the same issue.

      Delete
  95. Sir I want to use webcame code in jsp can u help me......?

    ReplyDelete
  96. Hello,
    I did all steps that you described at the beginning, first I got:
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)

    After setting PATH=C:\Users\mike\Desktop\lti-civil-20070920-1721\native\win32-x86

    I get the following error:
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: C:\Users\mike\Desktop\lti-civil-20070920-1721\native\win32-x86\civil.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

    Maybe you can help me out?

    Thanks!

    ReplyDelete
    Replies
    1. As in the exception , you are linking 32 bit library in 64 bit system, so either you use a specified library for 64 (if they released one for it) or you need to work around it by run it as 32 (i don't know how) or install a virtual machine for 32 bit operating system.

      Delete
  97. Sir, firstly i salute you for your valuable post and more knowledge in java programming, today i get yours "lti-civil-20070920-1721" file and i also run file in netbeans 6.8 and set all class path but still it gives error :
    Exception in thread "main" com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24) at com.lti.civil.test.CaptureSystemTest.main(CaptureSystemTest.java:32)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 1 more
    Java Result: 1

    please help me i use it newly and i need it.

    ReplyDelete
  98. Sir, firstly i saluate you for your valuable post and best knowledge in java programming, today i get your project and also run in netbeans 6.8 and also set all classpath but it still gives error :
    "Exception in thread "main" com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at com.lti.civil.test.CaptureSystemTest.main(CaptureSystemTest.java:32)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 1 more
    Java Result: 1"

    Sir please help me i need it

    ReplyDelete
    Replies
    1. You need to link the native library using the java parameter -D......location of the library
      Please refer to the post or other comments....

      Delete
  99. can u mail me a simple code for capturing an image and saving it in a folder just when i run the program and using JMF and java inbuilt classes only??

    i nee it for my college project and cant find a solution since last 6 days...please help me out

    ReplyDelete
  100. Any of my open source project contain the code ready: home monitor or interactive4j is better.

    ReplyDelete
  101. sir! is there any difference instead of using a webcam i will use a digital camera? if there is, do you have a code for that? thanks in advance..

    ReplyDelete
  102. sir! i have a question:

    how can i show the preview of the image that i want to capture before capturing it? and on what part of the program i should modify in order to solve this problem!..

    i'm a newbie in java.. i am developing a java app that includes capturing images using a webcam or digital camera..
    hoping for your quick response.. i need it badly..

    ReplyDelete
    Replies
    1. please refer to Interactive4J open source project to do this.

      Delete
  103. Hi, I am using netbeans 7 and jdk1.7.0_04. I have tried the code given in "Capture photo / image from Web Cam / USB Camera using Java". But I am getting error at method info.getDeviceID() and also it is giving error at class defination, saying make class TestWebCam abstract. Please help me.

    ReplyDelete
    Replies
    1. Search for similar exception in other comments and the reply to these exceptions, otherwise post the exception.

      Delete
  104. Hi Thanks for your valuable post.This standalone application works fine.But when i integrate same code from applet to jsp i am getting errors like this

    Exception: java.lang.NoClassDefFoundError: com/lti/civil/CaptureObserver
    java.lang.NoClassDefFoundError: com/lti/civil/CaptureObserver
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    plz help me.

    ReplyDelete
    Replies
    1. There is another post to address how to use web cam in JSP file in m blog : http://osama-oransa.blogspot.co.uk/2012/06/accessing-webcam-within-jsp.html

      Delete
  105. For me its perfectly working. can some one help me to get webcam settings or property panel.

    ReplyDelete
  106. For me its working perfectly. someone can help me out to get webcam setting or property panel by using this library.

    ReplyDelete
  107. hello, btw i want to make a project that is almost the same as this one, but i hardly understand your explanation above...i am still a newbie on java and this is a fundamental project for me to understand more about it...btw i am using netbeans 7 and i use netbeans UI designer to design the main interface of my application...do you have at least the working source code from your explanation above? thx for replying

    ReplyDelete
    Replies
    1. if u dont understand this post , go to my open source Interactive4j.

      Delete
  108. on your link :
    http://osama-oransa.blogspot.com/2012/04/webcam-video-capture-in-java.html

    you say this :
    "You class must implements : CaptureListener , this interface has 1 method inside it :
    public void setCaptureFile(File filename);"

    what do you mean by this?
    i have already made a surface class for that, but do i have to re-define it again in main class?

    ReplyDelete
  109. sir,,

    i have run these codes,but when it capture, it indicate only the black screen.
    but there was no error and also show in my programme and it save in correct position in my laptop...
    can you tell why it indicate only black screen when i captured photo..

    ReplyDelete
    Replies
    1. What do you mean by black ? also does it save the correct image or it is also black one ?

      Delete
    2. sir ,
      it save as black color photo.

      Delete
    3. remove step 7 that convert it from color to gray scale.

      Delete
  110. 1.com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    2.Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    How Can i Overcome This Exception

    ReplyDelete
  111. I have created this application in netbeans7.1 & jdk1.7.

    When i want to run this application Following Error Occur How can i solve this...

    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at javaapplication4.TestWebCam.(TestWebCam.java:37)
    at javaapplication4.TestWebCam.main(TestWebCam.java:149)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)

    Plz, Help me..

    ReplyDelete
    Replies
    1. add jvn parameter -d as specified above.

      Delete
    2. iKndly Tell me how can i add the jvn parameters Kindly Help me

      Delete
    3. In the project properties in netbeans you will find run properties where there is an option for JVM parameters.

      Delete
  112. hello friend, I can´t make it work my "jar" when I run I do not see anything and tried with the command line like this java -jar c: \ CapturaFram.jar -Djava.library.path = "c: \ win32 -x86 "
    but mark "error in the main, not civil in java library path"

    help me please

    ReplyDelete
    Replies
    1. This means library is not linked corre
      ctly is your windows 32 bit ? is the library in this path?

      Delete
  113. Hi Osama Oransa!
    Do u know how to setting webcam resolution using lti-civil?

    ReplyDelete
    Replies
    1. I didn't do that before but What kind of settings?
      Some can be developed by yourself as captured frame per second.

      Delete
  114. I used the following code and the error shown to me is

    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at com.pac.TestWebCam.(TestWebCam.java:62)
    at com.pac.TestWebCam.main(TestWebCam.java:203)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)


    i have imported the jar and the native folder ...

    I am using windows 7 and jdk1.7 and netbeans 7.2 verson

    Thanks in advance

    ReplyDelete
  115. I am getting the error

    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path

    what is the problem

    Thanks in advance

    ReplyDelete
    Replies
    1. Native library is not correctly located/defined.

      Delete
  116. hello sir!

    on what particular class can i set the frame rate in order for me to view the exact movement i want to capture.. coz i'm making a project that would capture images.. but the problem is that the preview is in slow motion.. hoping for your immediate response.. thank you in advance..

    ReplyDelete
    Replies
    1. You can control this by decreasing your loop, post your code so i can assist you.
      You may use my open source Interactive4J directly to capture video.

      Delete
  117. is it possible to adjust the frame rate for the preview? i'm using your lti-civil-no_s_w_t jar file.. coz the frame rate is too slow, i want it to be more faster.. tnx.

    ReplyDelete
    Replies
    1. You can control this by decreasing your loop, post your code so i can assist you.

      Delete
  118. how can i change screen resolution

    ReplyDelete
  119. Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: com.lti.civil.impl.jni.NativeCaptureStream.start()V
    at com.lti.civil.impl.jni.NativeCaptureStream.start(Native Method)

    How can i fix this problem, please?

    ReplyDelete
  120. Sir Can u plz help me.
    Everything is working except when i click on START button the WEBCAM is initialized and started but I can't see the actual streaming. After clicking on SHOT the image is displayed properly, but initially after START nothing is visible in the frame.

    Can u plz help.
    I want the (Camera)Live streaming to be visible after clicking on Start. Rest all is fine.
    Plz can u modify or help me with ur same code.

    ReplyDelete
  121. Dear Osama Oransa,
    I am developing web application for capturing images from webcam. I have some issues with JMF. it doesn't recognize usb webcam. so i am planning to switch over to lti.civil. So i tried the code which you have given in the same post. its working fine. then i converted the same in to applet its not working. my plan is to embed the applet in jsp. its giving noclassdeffcoundErro com/lti/civil/captureException. I signed applet using keytool. Is it the problem with accessing native using tomcat5.5 please help me solving the problem . Thank you

    ReplyDelete
  122. Please look at the other post describing how to do that for JSP files.

    ReplyDelete
  123. Im new to this. i used this code nd im getting following error

    ava.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - onNewImage(com.lti.civil.CaptureStream,com.lti.civil.Image) is already defined in webcam.TestWebCam
    at webcam.TestWebCam.(TestWebCam.java:107)
    Exception in thread "main" Java Result: 1

    ReplyDelete
  124. hey...I am working on a project where i need to use webcam.they above code is working perfectly but can i see the Pre-stream of webcam what i am going to captr on cilckevent of button. Please Reply soon.

    ReplyDelete
    Replies
    1. Can you please check interactive4J project , this is done in the demo ...
      Thanks..

      Delete
  125. Its seems this post is too old!!!
    but am getting this error,iam using netbeans 7.3


    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at testwebcam.TestWebCam.(TestWebCam.java:51)
    at testwebcam.TestWebCam.main(TestWebCam.java:154)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
    at java.lang.Runtime.loadLibrary0(Runtime.java:845)
    at java.lang.System.loadLibrary(System.java:1084)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 2 more

    ReplyDelete
    Replies
    1. If you looked at previous comments you will find the answer.
      -You didn't put the native library in Java classpath so it can load it , please use -D as described above:
      -Djava.library.path=".....\native\win32-x86" to add the library to the class path of the jar file.

      You can add it in netbeans in the run JVM argument

      Delete
  126. I have added the jar file in my project .Also copied the native lib folder and pasted into my project application folder .

    Do i need to set class path explicity.

    I run all other projects with the class path already set

    ReplyDelete
    Replies
    1. Please follow the steps and add -D option during execution so the native library cab be loaded :)

      Delete
  127. Hi can this work on windows 8 64 bit?

    ReplyDelete
  128. hello friends, to not have problems with JPEG library, I leave the following code, which will allow them to create the .jar

    only change the method onNewImage

    @Override
    public void onNewImage(CaptureStream stream, Image image) {
    if(!takeShot) return;
    takeShot=false;
    System.out.println("New Image Captured");
    byte[] bytes = null;
    try {
    if (image == null) {
    bytes = null;
    return;
    }
    //File file = new File("fotos/img" +Calendar.getInstance().getTimeInMillis() +".jpg");
    file=new File("fotos/imgpaciente.jpg");
    final BufferedImage bufferedImage = AWTImageConverter.toBufferedImage(image);
    ImageIO.write(bufferedImage, "JPG", file);
    img.setText("");
    img.setIcon(new ImageIcon(bufferedImage));
    img.revalidate();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    ReplyDelete
  129. hello friends and thank you osama, to not have problems with JPEG library, I leave the following code, which will allow them to create the .jar

    only change the method onNewImage

    @Override
    public void onNewImage(CaptureStream stream, Image image) {
    if(!takeShot) return;
    takeShot=false;
    System.out.println("New Image Captured");
    byte[] bytes = null;
    try {
    if (image == null) {
    bytes = null;
    return;
    }
    //File file = new File("fotos/img" +Calendar.getInstance().getTimeInMillis() +".jpg");
    file=new File("fotos/imgpaciente.jpg");
    final BufferedImage bufferedImage = AWTImageConverter.toBufferedImage(image);
    ImageIO.write(bufferedImage, "JPG", file);
    img.setText("");
    img.setIcon(new ImageIcon(bufferedImage));
    img.revalidate();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    ReplyDelete
  130. Hello sir,
    i try your code i got some error, as shown in below:

    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at TestWebCam.(TestWebCam.java:106)
    at TestWebCam.main(TestWebCam.java:166)
    Caused by: java.lang.UnsatisfiedLinkError: no civil in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 2 more

    ReplyDelete
    Replies
    1. This is because the native library is not configured correctly, please fix this by using the library location as in your computer...

      Delete
  131. ex = (com.lti.civil.CaptureException) com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: /home/ketangspl/Downloads/lti-civil-20070920-1721/native/linux-x86/libcivil.so: libstdc++.so.5: cannot open shared object file: No such file or directory

    I am facing such problems... Can you please guide me what shall be the problem

    ReplyDelete
    Replies
    1. The library seems to be missing or you do not have the grants to access it in that location:
      /home/ketangspl/Downloads/lti-civil-20070920-1721/native/linux-x86/libcivil.so

      Check the path and library file name.

      Delete
  132. Hi Osama, i have this error and i am using windows8,Netbeans

    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: G:\vinismon\NetbeansSampleProgram\TestWebCam\civil.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:24)
    at testwebcam.TestWebCam.(TestWebCam.java:41)
    at testwebcam.TestWebCam.main(TestWebCam.java:139)
    Caused by: java.lang.UnsatisfiedLinkError: G:\vinismon\NetbeansSampleProgram\TestWebCam\civil.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at com.lti.civil.impl.jni.NativeCaptureSystemFactory.createCaptureSystem(NativeCaptureSystemFactory.java:21)
    ... 2 more
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type:
    at testwebcam.TestWebCam.(TestWebCam.java:62)
    at testwebcam.TestWebCam.main(TestWebCam.java:139)
    Picked up _JAVA_OPTIONS: -Xmx512M
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)

    ReplyDelete
    Replies
    1. You need to use the correct native library for 64 bit not the one for 32 bit.

      Delete
  133. hello sir i want to capture the face displayed on webcam and save the image to a folder on my computer can you help me 

    ReplyDelete
    Replies
    1. Follow the steps and save the file, don't see any issue in what you want.

      Delete
  134. Hi sir can you clearly explain how to fix the library?
    com.lti.civil.CaptureException: java.lang.UnsatisfiedLinkError: no civil in java.library.path

    ReplyDelete
    Replies
    1. Please back to the steps and ensure the native library is in right location and referenced by -D parameter correctly.

      Delete
    2. Plz explain me step by step, i dont get it

      Delete
    3. Can you refer to step 1 and 2 above ...

      Delete
    4. Does the win32 *86 works for a 64 bits?
      if yes, its this part that i dont get :
      Add to the class-path of the project the jar file and the native library folder.
      To Run the project as jar file , you can use the JVM parameter:

      -Djava.library.path=".....\native\win32-x86" to add the library to the class path of the jar file.

      Delete
  135. if i want to use webcam what should i do in this code ???

    ReplyDelete
    Replies
    1. if the devise is recognized, just connect in the correct one you need.
      The code already loop over existing devices and print them.

      Delete
  136. I have error at this statement if(!takeShot) return;

    ReplyDelete
  137. HI, I have executed the code and it runs fine, thanks. But i kindly request that you do direct me to where i could get documentation about this LTI-CIVIL library iv'e searched online but cant seem to get any useful documentation about it.

    ReplyDelete