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:
- A servlet container
- JSR-188 API
- A repository with different resolution versions of the same image
Implementation Steps:
- Have a servlet container running.
- JSR-188 API must be installed and accessible for our application (it includes XML files appearing in sample code).
- Repository of images accessible for our application
Create an application implemented by a servlet with the following code (note that example uses class "ProfileParser", not shown in the code and implemented to make the example shorter and hide the details of reading CC/PP or UAProf information; another example showing how to deal with such information with more detail is available in UseCCPPOrUAProf):
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