サーブレットで動的に画像を生成

標準APIだけで出来た。出力画像はこちらと同じ。
ライブラリは強力なのに、シンプルに使えるAPIがないのがJavaの不幸なところだナー、と思う。シンタックスの問題じゃなくて。


ところで、Graphicsがawtにあるのはどうなんだろう?いいのかな?

サーブレット

package image_servlet;

import java.awt.Color;
import java.awt.Graphics2D;
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;

public class ImageServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OutputStream out = response.getOutputStream();

        try {
            response.setContentType("image/png");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");

            BufferedImage image = new BufferedImage(280, 170, BufferedImage.TYPE_INT_BGR);
            draw(image);
            ImageIO.write(image, "png", out);
        } finally {
            out.close();
        }
    }

    private void draw(BufferedImage image) {
        Graphics2D graphics = image.createGraphics();

        try {
            graphics.setBackground(Color.WHITE);
            graphics.clearRect(0, 0, 279, 169);

            graphics.setColor(new Color(0, 0, 0));
            graphics.drawRect(0, 0, 279, 169);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 255));
            graphics.drawLine(10, 22, 270, 22);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 128));
            graphics.drawLine(10, 23, 270, 23);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 64));
            graphics.drawLine(10, 24, 270, 24);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 32));
            graphics.drawLine(10, 25, 270, 25);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 16));
            graphics.drawLine(10, 26, 270, 26);

            graphics.setColor(new Color(0x80, 0x80, 0x80));
            graphics.drawRect(20, 40, 100, 100);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 64));
            graphics.drawRect(30, 50, 100, 100);
            graphics.setColor(new Color(0x00, 0x00, 0x00, 32));
            graphics.drawRect(10, 60, 100, 100);
            graphics.setColor(new Color(0xff, 0x00, 0x00, 64));
            graphics.drawLine(45, 75, 95, 125);
            graphics.setColor(new Color(0x00, 0x00, 0xff, 64));
            graphics.drawLine(45, 125, 95, 75);

            graphics.setColor(new Color(0xff, 0x00, 0x00, 64));
            graphics.fillRect(160, 40, 100, 100);
            graphics.setColor(new Color(0x00, 0xff, 0x00, 64));
            graphics.fillRect(150, 50, 100, 100);
            graphics.setColor(new Color(0x00, 0x00, 0xff, 64));
            graphics.fillRect(170, 60, 100, 100);

            graphics.drawImage(image, 0, 0, null);
        } finally {
            graphics.dispose();
        }
    }

}