java生成横竖字图片

前段时间看到空间上很流行一种两看的图片,也就是可以正着看,也可以侧着看,自己尝试着使用程序生成一下

java生成横竖字图片


整体上就是将将字体进行缩放,然后组合在一块。整体操作如下:

  • 生成两张写着相应信息的图片
  • 对图片进行缩放
  • 将图片进行叠加    

下面上代码:
图片操作类ImageUtil:

package honest.imageio;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图片操作类
 * 
 * @author 李跃东
 * 
 */
public class ImageUtil {

	private BufferedImage image;
	private int width; // 图片宽度
	private int height; // 图片高度

	public ImageUtil(int width, int height) {

		this.width = width;
		this.height = height;
		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	}

	/**
	 * 创建一个含有指定颜色字符串的图片
	 * 
	 * @param message
	 *            字符串
	 * @param fontSize
	 *            字体大小
	 * @param color
	 *            字体颜色
	 * @return 图片
	 */
	public BufferedImage drawString(String message, int fontSize, Color color) {
		Graphics g = image.getGraphics();
		g.setColor(color);
		Font f = new Font("宋体", Font.BOLD, fontSize);
		g.setFont(f);
		int len = message.length();
		g.drawString(message, (width - fontSize * len) / 2,
				(height + (int) (fontSize / 1.5)) / 2);
		g.dispose();
		return image;
	}

	/**
	 * 缩放图片
	 * 
	 * @param scaleW
	 *            水平缩放比例
	 * @param scaleY
	 *            垂直缩放比例
	 * @return
	 */
	public BufferedImage scale(double scaleW, double scaleH) {
		width = (int) (width * scaleW);
		height = (int) (height * scaleH);

		BufferedImage newImage = new BufferedImage(width, height,
				image.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		g.dispose();
		image = newImage;
		return image;
	}

	/**
	 * 旋转90度旋转
	 * 
	 * @return 对应图片
	 */
	public BufferedImage rotate() {
		BufferedImage dest = new BufferedImage(height, width,
				BufferedImage.TYPE_INT_ARGB);
		for (int i = 0; i < width; i++)
			for (int j = 0; j < height; j++) {
				dest.setRGB(height - j - 1, i, image.getRGB(i, j));
			}
		image = dest;
		return image;
	}

	/**
	 * 合并两个图像
	 * 
	 * @param anotherImage
	 *            另一张图片
	 * @return 合并后的图片,如果两张图片尺寸不一致,则返回null
	 */
	public BufferedImage mergeImage(BufferedImage anotherImage) {

		int w = anotherImage.getWidth();
		int h = anotherImage.getHeight();
		if (w != width || h != height) {
			return null;
		}

		for (int i = 0; i < w; i++) {
			for (int j = 0; j < h; j++) {
				int rgb1 = image.getRGB(i, j);
				int rgb2 = anotherImage.getRGB(i, j);

				Color color1 = new Color(rgb1);
				Color color2 = new Color(rgb2);

				// 如果该位置两张图片均没有字体经过,则跳过
				// 如果跳过,则最后将会是黑色背景
				if (color1.getRed() + color1.getGreen() + color1.getBlue()
						+ color2.getRed() + color2.getGreen()
						+ color2.getBlue() == 0) {
					continue;
				}

				Color color = new Color(
						(color1.getRed() + color2.getRed()) / 2,
						(color1.getGreen() + color2.getGreen()) / 2,
						(color1.getBlue() + color2.getBlue()) / 2);
				image.setRGB(i, j, color.getRGB());
			}
		}
		return image;
	}

	/**
	 * 保存图片int rgb1 = image.getRGB(i, j); int rgb2 = anotherImage.getRGB(i, j);
	 * rgb2 = rgb1 & rgb2; image.setRGB(height - i, j, rgb2);
	 * 
	 * @param filePath
	 *            图片路径
	 */
	public void save(String filePath) {
		try {
			ImageIO.write(image, "png", new File(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 得到对应的图片
	 * 
	 * @return
	 */
	public BufferedImage getImage() {
		return image;
	}
}


接着就是生成对应的图片测试类GenerateImage

package honest.imageio;

import java.awt.Color;

/**
 * 生成图片
 * 
 * @author Honest
 * 
 */
public class GenerateImage {
	public static void main(String[] args) {
		String outPath = "/home/honest/Desktop/out.png";
		ImageUtil imageUtil1 = new ImageUtil(2000, 250);
		imageUtil1.drawString("这只是测试一下程序", 200, Color.BLUE);
		imageUtil1.scale(0.125, 1);
		
		ImageUtil imageUtil2 = new ImageUtil(2000, 250);
		imageUtil2.drawString("这只是另外一下程序", 200, Color.RED);
		imageUtil2.scale(0.125, 1);
		imageUtil2.rotate();
		
		imageUtil2.mergeImage(imageUtil1.getImage());
		imageUtil2.save(outPath);
	}
}

代码中注释已经写的相对比较清晰了,欢迎留言


java生成横竖字图片

上一篇:推荐几款COM组件开发的入门书籍


下一篇:用Java对文件进行重命名