Create dynamic image using Java
This post is the first lesson in java 2D series intended to teach you how to use Java 2D to create dynamic images.
A Brief Idea |
The Java 2D™ API provides two-dimensional graphics, text, and imaging capabilities for Java™ programs through extensions to the Abstract Windowing Toolkit (AWT). Read More
The Java 2D API provides following capabilities:
- A uniform rendering model for display devices and printers
- A wide range of geometric primitives, such as curves, rectangles, and ellipses, as well as a mechanism for rendering virtually any geometric shape
- Mechanisms for performing hit detection on shapes, text, and images
- A model that provides control over how overlapping objects are rendered
- Enhanced color support that facilitates color management
- Support for printing complex documents
- Control of the quality of the rendering through the use of rendering hints
Here I am going to write web based application which will create an image and render it to the web browser.
The dynamic image will be created by buffer image in the memory and then will be displayed on web browser.The buffer image doesn't have limitation of the format of the image, it may be stored in any format.
Here We Go |
This is my dynamic image creator class namely DynamicImageCreato.java
package com.sarf.dynamic.image;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
public class DynamicImageCreator {
public BufferedImage createImage()
{
int width = 160;
int height = 100;
int imageType = BufferedImage.TYPE_4BYTE_ABGR;
BufferedImage imageBuffer = new BufferedImage(width,height,imageType);
Graphics2D graphics = imageBuffer.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
// true means to repeat pattern
GradientPaint gradientBG = new GradientPaint(0, 0, Color.red, 175,
175, Color.yellow,true);
graphics.setPaint(gradientBG);
graphics.fillRect(0, 0, imageBuffer.getWidth(), imageBuffer.getHeight());
graphics.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
GradientPaint gradientText = new GradientPaint(0, 0, Color.BLUE,
100, 20, Color.CYAN,true);
graphics.setPaint(gradientText);
graphics.rotate(0.25);
graphics.drawString("Sarf 2D",55,40);
// File file = new File("D:\\img.png");
// ImageIO.write(imageBuffer, "png", file);
return imageBuffer;
}
}
You can use this class to create an image and store it to disk. You have to use below lines for writing image into file.
File file = new File("D:\\img.png");
ImageIO.write(imageBuffer, "png", file);
Now I am going to give you a servlet which will use above class to create dynamic image and send the image to web browser.
package com.sarf.dynamic.image.controller;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sarf.dynamic.image.DynamicImageCreator;
public class DynamicImageController extends HttpServlet {
private static final long serialVersionUID = 1L;
public DynamicImageController() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DynamicImageCreator obj = new DynamicImageCreator();
BufferedImage image = obj.createImage();
OutputStream out = response.getOutputStream();
response.setContentType("image/png");
ImageIO.write(image,"png",out);
out.flush();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Configuration |
Here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>DynaImage</display-name>
<servlet>
<description></description>
<display-name>DynamicImageController</display-name>
<servlet-name>DynamicImageController</servlet-name>
<servlet-class>
com.sarf.dynamic.image.controller.DynamicImageController
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DynamicImageController</servlet-name>
<url-pattern>/DynamicImageController</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Here is my index.html content.
<html>
<head>
<title>Export</title>
</head>
<body>
<img id="img1"
src = "./DynamicImageController"
width="160" height="100" alt="Sarf Here" >
</img>
</body>
</html>
Output |
<< Home