Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for accessing ImageConversionFilter in Servlet
Previous Home Next

In this program, an ImageConversionFilter is used for converting image into another format by converting image pixels.

Steps involve in creating filter class and mapping it into web.xml file. click here

Application directory structure

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <filter>
  <filter-name>ImageConvertFilter</filter-name>
  <filter-class>r4r.Filter.ImageConvertFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>ImageConvertFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <session-config>
  <session-timeout>
     30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
 Document: index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type"
       content="text/html; charset=UTF-8">
  <title>r4r.co.in-index</title>
 </head>
 <body>
  <h1>Image Conversion Filter!</h1>
  <form method="POST">
  <img src="java-logo1.gif" width="651" 
         height="400" alt="java-logo1"/><br/>
<select name="colorFilter">
 <option>Colorless</option>
 <option>Black&White</option>
 <option>DifferentColor</option>
</select>
<input type="submit" value="Applying Filter" />
  </form><br/>
  <a href="ImageFilter.jsp">
      ImageFilter.jsp|Test purpose</a>
 </body>
</html>

ImageFilter.jsp page

<%-- 
 Document: ImageFilter.jsp
--%>
<%@page import="Acme.JPM.Encoders.GifEncoder"%>
<%@page import="java.awt.image.FilteredImageSource"%>
<%@page import="java.awt.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
       content="text/html; charset=UTF-8">
  <title>r4r.co.in-ImageFilter</title>
 </head>
 <body>
  <h1>ImageFilter Test page!</h1>
  <form method="POST">
  <img src="java-logo1.gif" width="651" 
             height="400" alt="Text Image"/><br/>
  <select name="applyingFilter">
 <option>ColorLess</option>
 <option>Black&White</option>
 <option>Red&Yellow</option>
 <option>DifferentColor</option> 
</select>
<input type="submit" value="Applying Filter" /><br/>
<a href="index.jsp">Return to Index.jsp page</a>
  </form>
  <%!  java.awt.Frame frame = new java.awt.Frame();%>
  <%
// get requested image full path
String imageName =
  request.getServletContext().getRealPath("java-logo1.gif");
frame.addNotify();
//create a dynamic image with specified image resource
java.awt.Image image = 
  java.awt.Toolkit.getDefaultToolkit().createImage(imageName);
//Track the media object status 
java.awt.MediaTracker mt = new java.awt.MediaTracker(frame);
mt.addImage(image, 0);
// add image into list which track by MediaTracker
mt.waitForAll(); //wait until all image not load into list
  %>

  <%
// get drop-down field value from ImageFilter.jsp page
String filterType = request.getParameter("applyingFilter");
if (filterType != null) {
 if (filterType.equals("Colorless")) {
  /*create an ImageFilter which modifies 
  the image pixels in according with RGB ColorModel.
  */
  java.awt.image.ImageFilter colorFilter =
	   new java.awt.image.RGBImageFilter() {
/*
 *  More color combination for filter
 * 
 * RGB --> YUV|  YUV --> RGB
 * Y =  0.299*Red+0.587*Green+0.114*Blue | Red= Y+0.000*U+1.140*V
 * U = -0.147*Red-0.289*Green+0.436*Blue | Green = Y-0.396*U-0.581*V
 * V =  0.615*Red-0.515*Green-0.100*Blue | Blue  = Y+2.029*U+0.000*V
 */
@Override
public int filterRGB(int x, int y, int rgb) {
 // Get (Red Green Blue) color intensity value
 int Red = (rgb & 0x00ff0000) >> 16;
 int Green = (rgb & 0x0000ff00) >> 8;
 int Blue = rgb & 0x000000ff;

 int Luma = (int) (0.299 * Red + 0.587 * Green + 0.114 * Blue);
 //Return image pixels value to ImageFilter
 return ((0xff << 24) | (Luma << 16) | (Luma << 8) | Luma);
}
  };
/*Creates an offScreenImage from ImageProducer
and applying ImageFilter on it.*/
  java.awt.Image offScreenImage =
	     frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream sos = response.getOutputStream();
// Encode offScreenImage into GIF format and send to client browser
  Acme.JPM.Encoders.GifEncoder encoder = 
	   new GifEncoder(offScreenImage, sos);
  encoder.encode();
 } else if (filterType.equals("Black&White")) {
  java.awt.image.ImageFilter colorFilter = 
	         new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
 int Red = (rgb & 0x00ff0000) >> 16;
 int Green = (rgb & 0x0000ff00) >> 8;
 int Blue = rgb & 0x000000ff;

 int U = (int) (-0.147 * Red - 0.289 * Green + 0.436 * Blue);
 return ((0xff << 24) | (U << 16) | (U << 8) | U);
}
  };
 java.awt.Image offScreenImage = 
	frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream sos = response.getOutputStream();
  Acme.JPM.Encoders.GifEncoder encoder = 
	   new GifEncoder(offScreenImage, sos);
  encoder.encode();
 } else if (filterType.equals("Red&Yellow")) {
  java.awt.image.ImageFilter colorFilter = 
	  new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {

int Red = (rgb & 0x00ff0000) >> 16;
int Green = (rgb & 0x0000ff00) >> 8;
int Blue = rgb & 0x000000ff;

int Y = (int) (0.299 * Red + 0.587 * Green + 0.114 * Blue);
int U = (int) (-0.147 * Red - 0.289 * Green + 0.436 * Blue);
int V = (int) (0.615 * Red - 0.515 * Green - 0.100 * Blue);
 return ((0xff << 24) | (Y << 16) | (U << 8) | V);

}
  };
  java.awt.Image offScreenImage = 
	frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream sos = response.getOutputStream();
  Acme.JPM.Encoders.GifEncoder encoder = 
	    new GifEncoder(offScreenImage, sos);
  encoder.encode();
 } else if (filterType.equals("DifferentColor")) {
  java.awt.image.ImageFilter colorFilter = 
	     new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
 return ((rgb & 0xff00ff00)
	    | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
}
  };
  java.awt.Image offScreenImage =
	   frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream sos = response.getOutputStream();
  Acme.JPM.Encoders.GifEncoder encoder = 
	           new GifEncoder(offScreenImage, sos);
  encoder.encode();
 }
 frame.removeNotify(); // free resource
}
  %>
 </body>
</html>

Filter Program

/*
 * Save as a ImageConvertFilter.java
 */
package r4r.Filter;
import java.awt.image.FilteredImageSource;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author R4R
 */
public class ImageConvertFilter implements Filter {

 private static final boolean debug = false;
 private FilterConfig filterConfig = null;
 private java.awt.Frame frame;

 // default constructor
 public ImageConvertFilter() {
 }

 /**
  * Init method for this filter 
  */
 @Override
 public void init(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
  frame = new java.awt.Frame();
  if (filterConfig != null) {
if (debug) {
 log("ImageConvertFilter: Initializing filter");
}
  }
 }

 /**
  * doFilter method for this filter 
  */
 @Override
 public void doFilter(ServletRequest request,
	 ServletResponse response, FilterChain chain)
throws IOException, ServletException {
  if (debug) {
log("ImageConvertFilter:doFilter()");
  }
  // get requested image full path
  String imagePath =((HttpServletRequest) request)
	       .getServletContext().getRealPath("java-logo1.gif");
  log("Image_Path: " + imagePath);
  frame.addNotify();
  //create a dynamic image with specified image resource
  java.awt.Image image = 
	  java.awt.Toolkit.getDefaultToolkit().createImage(imagePath);
  //Track the media object status 
  java.awt.MediaTracker mt = new java.awt.MediaTracker(frame);
  // add image into list which track by MediaTracker
  mt.addImage(image, 0);
  try {
mt.waitForAll();  //wait until all image not add into list
  } catch (InterruptedException ex) {
Logger.getLogger(ImageConvertFilter.class.getName()).log(Level.SEVERE,
  "An InterruptedException generate while loading image", ex);
  }

  if (request instanceof HttpServletRequest) {
// get drop-down field value from index.jsp page
String filterType = request.getParameter("colorFilter");
log("Filter_Type" + filterType);
if (filterType != null) {

 if (filterType.equals("Colorless")) {
  /*create an ImageFilter which modifies the image 
  pixels in according with RGB ColorModel.*/
  java.awt.image.ImageFilter colorFilter = 
	        new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
 // Get (Red Green Blue) color intensity value
 int Red = (rgb & 0x00ff0000) >> 16;
 int Green = (rgb & 0x0000ff00) >> 8;
 int Blue = rgb & 0x000000ff;

 int Luma = (int) (0.299 * Red + 0.587 * Green + 0.114 * Blue);
 //Return image pixels value to ImageFilter
 return ((0xff << 24) | (Luma << 16) | (Luma << 8) | Luma);
}
  };
//Creates an offScreenImage from ImageProducer and applying ImageFilter on it
  java.awt.Image offScreenImage =
	  frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream out = response.getOutputStream();
  // Encode offScreenImage into GIF format and send to client browser
  response.setContentType("image/gif");
  Acme.JPM.Encoders.GifEncoder encoder = 
	  new Acme.JPM.Encoders.GifEncoder(offScreenImage, out);
  encoder.encode();
 } else if (filterType.equals("Black&White")) {
  java.awt.image.ImageFilter colorFilter =
	     new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
 int Red = (rgb & 0x00ff0000) >> 16;
 int Green = (rgb & 0x0000ff00) >> 8;
 int Blue = rgb & 0x000000ff;

 int V = (int) (0.615 * Red - 0.515 * Green - 0.100 * Blue);
 return ((0xff << 24) | (V << 16) | (V << 8) | V);
}
  };
  java.awt.Image offScreenImage = 
	frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream out = response.getOutputStream();
  response.setContentType("image/gif");
  Acme.JPM.Encoders.GifEncoder encoder =
	  new Acme.JPM.Encoders.GifEncoder(offScreenImage, out);
  encoder.encode();
 } else {
  java.awt.image.ImageFilter colorFilter = 
	          new java.awt.image.RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
 return ((rgb & 0xff00ff00) |
	       ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
}
  };
  java.awt.Image offScreenImage =
	   frame.createImage(new FilteredImageSource(image.getSource(),
 colorFilter));
  ServletOutputStream out = response.getOutputStream();
  response.setContentType("image/gif");
  Acme.JPM.Encoders.GifEncoder encoder =
	     new Acme.JPM.Encoders.GifEncoder(offScreenImage, out);
  encoder.encode();
 }
} else {
 chain.doFilter(request, response);
}
  }
 }

 /**
  * Return the filter configuration object for this filter.
  */
 public FilterConfig getFilterConfig() {
  return (this.filterConfig);
 }

 /**
  * Set the filter configuration object for this filter.
  */
 public void setFilterConfig(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
 }

 /**
  * Destroy method for this filter 
  */
 @Override
 public void destroy() {
  filterConfig = null;
  frame.removeNotify();
 }

 /**
  * log method for print msg over server console 
  */
 public void log(String msg) {
  filterConfig.getServletContext().log(msg);
 }
}
Output of Program
Previous Home Next