All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.imagecreator.impl.AbstractTextToImage Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.imagecreator.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import org.tinygroup.imagecreator.TextToImage;

//TODO 增加新的单元测试代码,把参数初始化部分的情况也测试到
/**
 * 文本转换图像抽象类,实现了除输出之外的所有功能,输出由子类实现outputImage方法
 */
public abstract class AbstractTextToImage implements TextToImage {
	private static final int MAX_PRIMARY_COLOR = 256;
	// 画图参数
	private ConvertParameter convertParameter = null;

	public ConvertParameter getImageParameter() {
		return convertParameter;
	}

	public void setImageParameter(ConvertParameter convertParameter) {
		this.convertParameter = convertParameter;
	}

	// TODO 添加干扰线粗细控制
	/**
	 * 画干扰线
	 */
	private void drawDisturbedLine(Graphics graph) {
		for (int i = 0; i < convertParameter.getDisturbedLineNum(); i++) {
			// 线条的颜色
			graph.setColor(RandomUtil.getColor(null, MAX_PRIMARY_COLOR));
			// 线条两端坐标
			int x1 = RandomUtil.getInt(convertParameter.getWidth());
			int y1 = RandomUtil.getInt(convertParameter.getHeight());

			int x2 = RandomUtil.getInt(convertParameter.getWidth());
			int y2 = RandomUtil.getInt(convertParameter.getHeight());

			// 画线
			graph.drawLine(x1, y1, x2, y2);
		}
	}

	/**
	 * 产生内容到Out对象
	 *
	 * @param text
	 *            要输出的文件
	 * @param out
	 *            要输出的地方
	 * @throws IOException
	 * @throws NoParameterException
	 */
	public void generateImage(String text, T out) throws IOException {
		// 如果没有参数,则初始化一个默认的
		if (convertParameter == null) {
			newImageParameter();
		}

		// 返回图像
		BufferedImage image = getImage();

		// 获取画布
		Graphics2D g = initImage(image);

		// 画干扰线
		drawDisturbedLine(g);

		// 画字符串
		draw(convertParameter, g, text);

		// 执行
		g.dispose();

		// 输出图片结果
		outputImage(image, out);
	}

	// TODO 添加文字颜色与背景颜色相近时重新取颜色
	/**
	 * 画文字
	 *
	 * @param convertParameter
	 * @param g
	 * @param text
	 */
	void draw(ConvertParameter convertParameter, Graphics2D g, String text) {
		// 文字旋转
		Font font = new Font(convertParameter.getFontName(), Font.PLAIN
				| Font.BOLD, convertParameter.getFontSize());
		g.setFont(font);

		FontMetrics fm = g.getFontMetrics(font);
		int fontHeight = fm.getHeight(); // 字符的高度

		int offsetLeft = 0;
		int rowIndex = 1;
		for (int i = 0; i < text.length(); i++) {
			initDrawStyle(convertParameter, g);
			char c = text.charAt(i);
			int charWidth = fm.charWidth(c); // 字符的宽度

			// 自动换行
			boolean isReturn = Character.isISOControl(c);
			boolean isOutWidth = offsetLeft >= (convertParameter.getWidth() - charWidth);
			if (isReturn || isOutWidth) {
				rowIndex++;
				offsetLeft = 0;
			}

			g.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);
			offsetLeft += charWidth;
		}
	}

	/**
	 * 初始化绘图格式
	 *
	 * @param convertParameter
	 * @param g
	 */
	private void initDrawStyle(ConvertParameter convertParameter, Graphics2D g) {
		// TODO 这里的旋转要改成高度位置不要变化
		g.rotate(
				Math.toRadians(RandomUtil.getDouble(
						convertParameter.getRadian(),
						convertParameter.getRadianRange())),
				convertParameter.getRotateX(), convertParameter.getRotateY());
		g.scale(convertParameter.getScale(), convertParameter.getScale());

		g.setColor(RandomUtil.getColor(convertParameter.getFontColor(),
				convertParameter.getColorRange()));

	}

	/**
	 * 初始化图像
	 *
	 * @param image
	 * @return
	 */
	private Graphics2D initImage(BufferedImage image) {
		Graphics2D g = (Graphics2D) image.getGraphics();

		Image bgImage = convertParameter.getBgImage();
		if (bgImage == null) {
			// 画长方形
			g.setColor(convertParameter.getBgColor());
			g.fillRect(0, 0, convertParameter.getWidth(),
					convertParameter.getHeight());
		} else {
			g.drawImage(bgImage, 0, 0, image.getWidth(), image.getHeight(), 0,
					0, bgImage.getWidth(null), bgImage.getWidth(null), null);
		}
		// 外框
		g.setColor(convertParameter.getRectColor());
		g.drawRect(0, 0, convertParameter.getWidth() - 1,
				convertParameter.getHeight() - 1);
		return g;
	}

	/**
	 * 设置参数是否存在
	 *
	 * @throws NoParameterException
	 */
	private void newImageParameter() {
		convertParameter = new ConvertParameter();
		// 设置初始值

		// 文字旋转
		convertParameter.setRotateX(convertParameter.getWidth() / 2);
		convertParameter.setRotateY(convertParameter.getHeight() / 2);
	}

	/**
	 * 返回图像
	 *
	 * @return
	 */
	private BufferedImage getImage() {
		BufferedImage image = new BufferedImage(convertParameter.getWidth(),
				convertParameter.getHeight(), BufferedImage.TYPE_INT_RGB);

		if (convertParameter.getRectColor() == null) {
			convertParameter.setRectColor(Color.BLACK);
		}
		if (convertParameter.getBgColor() == null) {
			convertParameter.setBgColor(Color.WHITE);
		}
		return image;
	}

	/**
	 * 输出图像,要由子类实现
	 *
	 * @param image
	 * @param out
	 * @throws IOException
	 */
	protected abstract void outputImage(BufferedImage image, T out)
			throws IOException;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy