Click here to Skip to main content
13,632,992 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 27 May 2011
Licenced Apache

Capture picture on Webpage with Java Backend

, 27 May 2011
Rate this:
Please Sign up or sign in to vote.
How to capture picture on Webpage with Java Backend

In most of the projects, we try to capture the user’s face on the web page. Till recently, many projects have been implementing this using either ActiveX objects or using Java Plugins or some other media plugins. With the advent of flash and its related technologies, this and its increase in presence over the web flash have become favorite technologies to build such applications, thus making it browser agnostic as compared to ActiveX Objects.

There is a project on the web named JPEGCam which is free and open source and has the capability to do that.

My effort is to create a Java backend to its frontend (default one comes with PHP backend). The following example explains my effort.

I have created a mavenized application to make it run without any extra server deployment. Here is the description.

I’ve created a simple servlet which just captures the input stream, stores data in a folder named uploads, and send the file URL back to client to display.

Here is the code:

package com.linkwithweb.jpegcam;

import java.util.Date;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Ashwin Kumar
 * Servlet implementation class PictureCaptureServlet
public class PictureCaptureServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String fileStoreURL = "";

	 * @see HttpServlet#HttpServlet()
	public PictureCaptureServlet() {
		// TODO Auto-generated constructor stub

	 * @see Servlet#init(ServletConfig)
	public void init(ServletConfig config) throws ServletException {
		fileStoreURL = config.getServletContext().getRealPath("") + "/uploads";
		try {
			File f = new File(fileStoreURL);
			if (!f.exists()) {
		} catch (Exception e) {
			// TODO: handle exception

	 * @see HttpServlet#service(HttpServletRequest request, 
		HttpServletResponse response)
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, 
				IOException {
		try {
			long time = new Date().getTime();

			FileOutputStream fileOutputStream = new FileOutputStream(
					fileStoreURL + "/"+time+".jpg");
			int res;
			while ((res = request.getInputStream().read()) != -1) {
			 * To make sure each url is different and 
			 * not cached added time to it
					"http://localhost:8080/uploads/" + time
							+ ".jpg");

		} catch (Exception e) {
		} finally {


Below is a sample JavaScript which handles the frontend:

<!--<span class="code-comment"> First, include the JPEGCam JavaScript Library --></span>
<script type="text/javascript" src="webcam.js"></script>

<!--<span class="code-comment"> Configure a few settings --></span>
<script language="JavaScript">
    webcam.set_api_url( '/capture' );
    webcam.set_quality( 90 ); // JPEG quality (1 - 100)
    webcam.set_shutter_sound( true ); // play shutter click sound

<!--<span class="code-comment"> Next, write the movie to the page at 320x240 --></span>
<script language="JavaScript">
    document.write( webcam.get_html(320, 240) );
<!--<span class="code-comment"> Code to handle the server response (see test.php) --></span>
<script language="JavaScript">
    webcam.set_hook( 'onComplete', 'my_completion_handler' );

    function take_snapshot() {
        // take snapshot and upload to server
        document.getElementById('upload_results').innerHTML =

    function my_completion_handler(msg) {
        // extract URL out of PHP output
        if (msg.match(/(http\:\/\/\S+)/)) {
            var image_url = RegExp.$1;
            // show JPEG image in page
            document.getElementById('upload_results').innerHTML =
                '<h1>Upload Successful!</h1>' +
                '<h3>JPEG URL: ' + image_url + '</h3>' +
                '<img src="' + image_url + '">';

            // reset camera for another shot
        else alert("Java Error: " + msg);

Now let me paste my pom.xml:

<project xmlns= 


	<name>JPEGCamIntegration Maven Webapp</name>

			<!--<span class="code-comment"> Facilitates downloading source and javadoc in Eclipse --></span>

			<!--<span class="code-comment"> Ensures we are compiling at 1.6 level --></span>

Here is how you run this and test:

mvn jetty:run-exploded

and click this URL:

Code has been checked into the following location:


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Chief Technology Officer Northalley
United States United States
A Technology evangelist with no technical language barriers. A strong believer that Simple Sofware Is Perfect Software. A staunch proponent of software / documentation automation in all domain's. And finally a true diciple of Google Search.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180712.1 | Last Updated 27 May 2011
Article Copyright 2011 by AshwinRayaprolu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid