This is an archive of an inactive wiki and cannot be modified.

ProperResolutionImagesOnJava

This technique gives a possible implementation for the CategoryBpImagesResizing using JSR-188. The idea is to capture client's profile information (CC/PP or, more likely, UAProf) and serve the version of an image with the resolution that best fits the display of the client. The idea of the example shown below is a servlet which returns an (X)HTML document to the client referencing an image. There would be a repository of different versions of the same image and the img element in the (X)HTML document would point to the image which best fits the display of the client. The resolution of the display is known by checking CC/PP or UAProf information, as declared by the client via HTTP GET request. Most of desktop user agents do not provide CC/PP and/or UAProf information so the example should be tested at client side by a mobile device or an emulator.

An improved version of this example might be done with any transcoding java library (there are some free and/or open-source available) in order to dynamically create an image of desired resolution.

Pre-requisites:

Implementation Steps:

Example:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class ImageAdaptation extends javax.servlet.http.HttpServlet implements
    javax.servlet.Servlet
{
 
  // Object of class "ProfileParser" in charge of getting 
  // client's capabilities
  private ProfileParser profileParser;
 
  public ImageAdaptation()
  {
    super();
  }
 
  public void init(ServletConfig config)
  {
    ServletContext context = null;
    
    // Get the context and set a "ProfileParser" object with such information
    context = config.getServletContext();
    profileParser = new ProfileParser(context);
  }
 
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
  {
    // 'String' object used to store the value of attribute 'ScreenSize'
    String bppVal;
    // 'HashMap' object used to get the set of attributes
    HashMap attrMap;
    // Array of 'String' object used to get dimensions in attribute 'ScreenSize'.
    String Dimensions[];
    // 'String' object used to express the image to be loaded.
    String W3CLogoImage;
   
    response.setContentType("text/html");
 
    PrintWriter out = response.getWriter();
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
    out.println("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
    out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
    out.println("<head>");
 
    out.println("<title>Image Adaptation (CC/PP - UAProf)</title>");
    out.println("</head>");
    out.println("<body>");
 
    // Check whether client declares a CC/PP profile
    // and show profile's file name
    if (request.getHeader("profile") == null)
      System.out.println("profile: <NULL>");
    else
      System.out.println("profile: " + request.getHeader("profile"));
 
    // Same for UAProf 
    if (request.getHeader("x-wap-profile") == null)
      System.out.println("x-wap-profile: <NULL>");
    else
      System.out.println("x-wap-profile: " + request.getHeader("x-wap-profile"));
    
    // Get hash table with attributes (capabilities of the client)
    attrMap = profileParser.getProfileAttr(request);
 
    // If the object is null (attributes could not be read for some reason) 
    // then serve a default image (120 pixels wide as for default delivery context)
    if (attrMap == null)
    {
      out.println("<img src=\"images/w3clogo120.png\" alt=\"W3C Logo\" />");
      out.println("<p>NULL PROFILE</p>");
    }
    // If information about client is available...
    else
    {
      // Get attribute "ScreenSize"
      bppVal = (String) attrMap.get("ScreenSize");
 
      // Get each dimension of screen size
      Dimensions = new String[2];
      Dimensions = bppVal.split("x");
 
      // Create file name of the image to be served 
      // (file name expresses width of the image)
      W3CLogoImage = "w3clogo" + Dimensions[0] + ".png";
 
      // Insert the image into (X)HTML document      
      out.println("<img src=\"images/" + W3CLogoImage + "\" alt=\"W3C Logo\" />");
 
      out.println("<p>Profile exists.</p>");
      out.println("<p>ScreenSize: " + bppVal + "</p>");
    }
 
    out.println("</body>");
    out.println("</html>");
  }
 
}

Back to BestPracticesList


CategoryJava CategoryXhtml CategoryContentAdaptation CategoryBpImagesResizing CategoryBpCapabilities

Contributions to this wiki are governed by the W3C policies for Contribution to W3C' wiki on Mobile Web.