Nothing special, just a servlet which takes some text and a few other bits as parameters and outputs the vertical text as an image. I'll just post it here in case it comes in useful, although (as documented) most of the code comes from here with alteration for the text translation, image cropping, custom colors and a few other bits.
The trick to creating images is quite simple, create a BufferedImage, get a Graphics context that allows you to draw to the image. You can copy pictures into the image, write text and shapes, anything else that takes your fancy.
The initial image is created at a predefined size and any drawing outside this area gets lost.
To rotate the text, get a regular Font and define a rotation transformation on it and set it on the Graphics context. I found that the FontMetrics from the rotated text didn't return the correct values for the text so I just transformed from the nonrotated Font. First I tried g2d.rotate(), but that rotated the context without rotating the text, it surprised me.
Cropping was simple and is hardly worth mentioning.
When you're encoding images, ImageIO is your best friend. In theory this code can also output PNG and GIF images, but it wasn't required so I didn't bother trying
A sample request string is: /image/vertical?size=20&bgColor=0xF0E6D5&textColor=0x000000&text=David%20O'efghijk and looks like this. The text was selected to test the font descent (ie the letters that go 'down', like g and j). 
package com.javaranch.davo.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Gets the text passed as a parameter and rotates it 90 degrees so it is written vertically
* @author David O'Meara davo@javaranch.com
* @since 19/07/2007
* @see http://www.codebeach.com/tutorials/watermarking-images-java-servlet.asp
*/
public class VerticalServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
{
String fontName = "Arial";
if (req.getParameter(PARAM_FONT) != null && req.getParameter(PARAM_FONT).length() > 0)
{
fontName = req.getParameter(PARAM_FONT);
}
final String personName = req.getParameter(PARAM_TEXT);
final int fontSize = Integer.parseInt(req.getParameter(PARAM_SIZE));
// Create a rotated font
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(90));
final Font basicFont = new Font(fontName, Font.PLAIN, fontSize);
final Font font = new Font(fontName, Font.PLAIN, fontSize).deriveFont(at);
final Color bgColor = Color.decode(req.getParameter(PARAM_BG_COLOR));
final Color textColor = Color.decode(req.getParameter(PARAM_TEXT_COLOR));
//Create an image BUFFER_HEIGHT x BUFFER_WIDTH, before cropping
BufferedImage bufferedImage = new BufferedImage(BUFFER_HEIGHT, BUFFER_WIDTH, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.setBackground(bgColor);
g2d.clearRect(0, 0, BUFFER_HEIGHT, BUFFER_WIDTH);
// create metrics from the basic font, as the rotated one behaves strangely
g2d.setFont(basicFont);
final FontMetrics fm = g2d.getFontMetrics();
g2d.setFont(font);
g2d.setColor(textColor);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Rectangle2D rect = fm.getStringBounds(personName, g2d);
int drop = fm.getDescent();
int width = Math.min(BUFFER_WIDTH, (int) rect.getWidth() + 2 * BUFFER);
int height = Math.min(BUFFER_HEIGHT, (int) rect.getHeight() + 2 * BUFFER);
g2d.drawString(personName, drop + BUFFER, BUFFER);
BufferedImage croppedImage = bufferedImage.getSubimage(0, 0, height, width);
//Free graphic resources
g2d.dispose();
//Set the mime type of the image
res.setContentType("image/"+IMAGE_TYPE);
//Write the image as a PNG
OutputStream out = res.getOutputStream();
ImageIO.write(croppedImage, IMAGE_TYPE, out);
out.close();
}
private static final String PARAM_TEXT = "text";
private static final String PARAM_SIZE = "size";
private static final String PARAM_FONT = "font";
private static final String PARAM_BG_COLOR = "bgColor";
private static final String PARAM_TEXT_COLOR = "textColor";
private static final int BUFFER = 3;
private static final int BUFFER_WIDTH = 250;
private static final int BUFFER_HEIGHT = 100;
private static final String IMAGE_TYPE = "jpg"; }
|
code generated using the Html2Java plugin for Eclipse
TrackBack to http://radio.javaranch.com/davo/addTrackBack.action?entry=1184942077153