How To Capture Webcam Images In Java Swings

webcam

As the regular readers of tricksmode know that I am pursuing engineering in Information Technology and a part time blogger. Some day ago, I had to finish a project in java which involves input from webcam or you can say that we have to capture images from webcam. The task was little bit complicated because me and mine team partners don’t know anything about hardware interaction in java. But with the help of some open source java libraries we finished up our task without facing much problems.
The main problem which we faced during implementing libraries is that, The libraries didn’t came with proper getting started documentation, We also searched google on the same but didn’t got any positive response.
So today we are sharing a java swings program to capture webcam images and some essential libraries to implement that program on your machine.

Things Required:

1) Java installed on your computer.
2) An usb camera or webcam.
3) Net beans installed on your computer.
4) LTI-CIVIL java library.

What is an LTI-CIVIL library?

LTI-CIVIL is an open source java library for capturing images from webcam or usb camera. The best thing about this library is that, it doesn’t depend on JMF (Java Media Framework) and works well on windows, Linux and Mac os.

How to configure/implement LTI-CIVIL library to use it with my project?

If you are using net beans then LTI-CIVIL library are very easy to configurable. We just have to set some jar files and dll files and we are done. Just a minute we are showing it step by step.

Here we start,

How To Capture Webcam Images In Java Swings With LTI-CIVIL Library

1) Install java and net beans if you haven’t installed them yet.

2) Open net beans and create a new java project.

new project netbeans

3) Now right click on your newly created project and create a new java class and name that “TestWebCam” without quotes.

create java class

4) Now copy and paste the following code in TestWebCam.java class.

/*Start copying from here*/
import com.lti.civil.CaptureDeviceInfo;

 

import com.lti.civil.CaptureException;

 

import com.lti.civil.CaptureObserver; 

import com.lti.civil.CaptureStream; 

import com.lti.civil.CaptureSystem; 

import com.lti.civil.CaptureSystemFactory; 

import com.lti.civil.DefaultCaptureSystemFactorySingleton; 

import com.lti.civil.Image; 

import com.lti.civil.awt.AWTImageConverter; 

import com.sun.image.codec.jpeg.JPEGCodec; 

import com.sun.image.codec.jpeg.JPEGImageEncoder; 

import java.awt.event.ActionEvent; 

import java.awt.event.ActionListener; 

import java.awt.image.BufferedImage; 

import java.io.ByteArrayInputStream; 

import java.io.ByteArrayOutputStream; 

import java.io.File; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import java.util.Calendar; 

import java.util.List; 

import javax.imageio.ImageIO; 

import javax.swing.ImageIcon; 

import javax.swing.JButton; 

import javax.swing.JFrame; 

import javax.swing.JPanel; 

import javax.swing.WindowConstants; 

public class TestWebCam implements CaptureObserver { 

JButton start = null; 

JButton shot = null; 

JButton stop = null; 

CaptureStream captureStream = null; 

boolean takeShot=false; 

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; 

} 

}); 

} 

    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 arg0, CaptureException arg1) { 

        throw new UnsupportedOperationException(“Error is coming “); 

       

    } 

public static void main(String args[]) 

throws Exception { 

TestWebCam test = new TestWebCam(); 

}
}
/*Code Ends here*/
5) you will see a lot of errors are coming in code, don’t worry, this is because we didn’t configured LTI-CIVIL libraries yet. To Configure LTI-CIVIL libraries first of all go to this link and download the LTI-CIVIL libraries.

6) Unzip the downloaded folder to desktop.

7) Open the downloaded folder you will see a folder named “native
Open “Native” Folder and Select your operating system. If you are developing a java swings webcam capture program for windows then open win32 folder. If you are using linux then, Open Linux-amd64 or X86 based on your machine configuration. And mac users open macosx universal folder.

native folder in LTI-CIVIL8) Now copy all the contents of the selected folder and paste them your netbeans project folder. For windows users netbeans project folder exists at Documents > NetbeansProjects>Project Name

For windows users, If your project name is “JavaApplication1”, Then go to documents>NetbeansProjects>JavaApplication1 and Paste the copied contents there.

9) Now open Netbeans and right click on your project, Then Go to properties and then select “libraries” from the right panel. Now click on “Compile” tab and then finally click on Add JAR/FOLDER. After that Browse to the LTI-CIVIL folder which we had downloaded earlier. And add these 4 Jar files lti-civil.jar, lti-civil-no_s_w_t.jar , lti-civil-noswt.jar , lti-civil-noutils.jar to your project.

Adding the jar files to a project

10) Once you had done, you will see that all errors are gone, and the java swings program is properly capturing images from webcam.Snapshot of Java Webcam Image Capturing Program using LTI-CIVIL
Java swing program to capture webcam imagesI tried my best to make this article as simple as I can, if you are facing any problem while implementing LTI-CIVIL or this webcam capture program on your computer, Kindly Comment below and give us a chance to help you.

SHARE
He is Founder and Editor-in-Chief of Tricksmode.com He is a 24 year guy from Dehradun, India and writes about Blogging, Technology, Mobiles, Gadgets, Softwares etc. For any feedback and queries, mail us to : 3dartistrawat@gmail.com

LEAVE A REPLY