Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for Scaling an image into specified width and height
Previous Home Next

In this Generic Servlet program, Scaled an image into specified width and height and then, a RGBImageFilter (image filter) is used to convert image property like color. Image must be encored into gif format before sending response to client. Used this jar file into program for encore image into GIF format.

Acme.JPM.Encoders.GifEncoder (here)\

Servlet 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">
  <servlet>
   <servlet-name>ScaledImageServlet</servlet-name>
   <servlet-class>r4r.GenericServlet.ScaledImageServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>ScaledImageServlet</servlet-name>
     <url-pattern>/ScaledImageServlet</url-pattern>
  </servlet-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-GenericServlet</title>
 </head>
 <body>
  <form>
   <h1>Access GenericServlet by servlet's Name!</h1>
   <a href="ScaledImageServlet">ScaledImageServlet</a>
  </form>
 </body>
</html>

Servlet Program

/*
 * Save as a ScaledImageServlet.java
 */
package r4r.GenericServlet;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import Acme.JPM.Encoders.GifEncoder;
/**
 *
 * @author R4R
 */
public class ScaledImageServlet extends GenericServlet {
 Frame frame = new Frame(); //new frame
 @Override
 public void service(ServletRequest req,
	  ServletResponse res) throws ServletException,IOException
 {
  res.setContentType("image/gif"); // response send to client browser
  ServletOutputStream out = res.getOutputStream();
  // Get resource full path
  String imagePath = getServletContext().getRealPath("javalogo1.gif");
  try {
//check resource availability
if (imagePath == null || imagePath.startsWith("image")) {
out.println(HttpServletResponse.SC_NOT_ACCEPTABLE + 
	 "Reading content type must be \"image(jpg/gif)/*\"");
}
//Creates an image with the specified image resource.
Image image = Toolkit.getDefaultToolkit().createImage(imagePath);
//Track image status 
MediaTracker mt = new MediaTracker(frame); //frame acts as ImageObserver
mt.addImage(image, 0);
mt.waitForAll(); //Load all image

//Get image property
int heigth = image.getHeight(frame);
int width = image.getWidth(frame);
//Make sure reading Image is valid
if (heigth <= 0 || width <= 0) {
 out.println(HttpServletResponse.SC_NO_CONTENT + "Not a valid image");
}

//Scaled property is used to set the specified width and height of image object
Image scaledImage = image.getScaledInstance
	      (width / 2, heigth / 2, Image.SCALE_SMOOTH);

//Applying image filter
ImageFilter colorFilter = new 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;
  /*
*  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
*/
  int V = (int) (0.615 * Red - 0.515 * Green - 0.100 * Blue);

  return ((0xff << 24) | (V << 16) | (V << 8) | V);
 }
};
//Construct a Off-screen Image and match this image from imagefilter object
Image img = frame.createImage
	(new FilteredImageSource(scaledImage.getSource(), colorFilter));
//Encode the off-screen image into GIF format
GifEncoder encoder = new GifEncoder(img, out);
encoder.encode();
  } catch (InterruptedException ex) {
Logger.getLogger(ScaledImageServlet.class.getName()).log(Level.SEVERE, 
 "InterruptedException generate while reading image:", ex);
  } finally {
//Free resource
frame.dispose();
out.flush();
out.close();
  }
 }
}
Output of Program
Previous Home Next