com.pureinfo.srm.common.ImageHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.common.ImageHelper.java

Source

package com.pureinfo.srm.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.AttributedString;
import java.util.Random;

import org.jfree.chart.encoders.EncoderUtil;
import org.jfree.chart.encoders.ImageFormat;

import com.pureinfo.force.exception.PureException;

/**
 * PureInfo JavaMail
 * @(#)Test.java   1.0 Nov 1, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

/**
 * <P>
 * Created on Nov 1, 2005 11:34:39 AM <BR>
 * Last modified on Nov 1, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Nov 1, 2005
 * @since JavaMail 1.0
 */
public class ImageHelper {
    private static Random random = new Random(System.currentTimeMillis());

    public static void main(String[] args) throws IOException, FileNotFoundException, PureException {
        drawImage("1234", new FileOutputStream("test.png"));
    }

    public static void drawImage(String _sString, OutputStream _os) throws PureException {
        int nWidth = 200;
        int nHeight = 50;
        String sText = _sString;

        BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();

        for (int i = 0; i < sText.length(); i++) {
            draw(String.valueOf(sText.charAt(i)), g2, i * nWidth / sText.length(), 0,
                    (i + 1) * nWidth / sText.length(), nHeight);
        }

        g2.dispose();
        try {
            EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
        } catch (Exception ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        }
    }

    private static void draw(String _str, Graphics2D _g2, int _nX0, int _nY0, int _nX1, int _nY1) {
        Font font = new Font(getFontName(), Font.BOLD, getFontSize());
        AttributedString str = new AttributedString(_str);
        str.addAttribute(TextAttribute.FONT, font);
        str.addAttribute(TextAttribute.FOREGROUND, getColor());
        _g2.drawString(str.getIterator(), getNumber(_nX0, _nX1), (_nY0 + _nY1) * 2 / 3);
    }

    private static int getNumber(int _nX0, int _nX1) {
        int number = _nX0 + (Math.abs(random.nextInt()) % (_nX1 - _nX0)) * 3 / 5;
        return number > 170 ? 170 : number;
    }

    private static String getFontName() {
        String[] names = new String[] { "Comic Sans MS", "Arial Narrow", "Times New Roman", "Courier New" };
        return names[Math.abs(random.nextInt()) % names.length];
    }

    private static int getFontSize() {
        int nSize = Math.abs(random.nextInt()) % 10 * 3 + 25;
        return nSize > 45 ? 45 : (nSize < 30 ? 30 : nSize);
    }

    //    private static int getFontStyle() {
    //        int[] names = new int[] { Font.PLAIN, Font.BOLD, Font.ITALIC };
    //        return names[Math.abs(random.nextInt()) % names.length];
    //    }

    private static Color getColor() {
        Color[] names = new Color[] { Color.WHITE, Color.RED, Color.GREEN, Color.BLUE, Color.CYAN,
                Color.DARK_GRAY };
        return names[Math.abs(random.nextInt()) % names.length];
    }

    public static void drawRectangle(Paint _color, Point _point, OutputStream _os) throws PureException {
        int nWidth = _point.x;
        int nHeight = _point.y;

        BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setPaint(_color);
        g2.fillRect(0, 0, nWidth, nHeight);

        g2.dispose();
        try {
            EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
        } catch (Exception ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        }
    }
}