| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package cn.com.goldenwater.dcproj.utils;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.StringReader;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class AvatarHelper {
- /**
- * @throws IOException
- * @throws
- **/
- public static void main(String[] args) throws IOException {
- String tep = "李海超";
- StringReader reader = new StringReader(tep);
- //150×50的图片
- BufferedImage bufImage = new BufferedImage(150, 50, BufferedImage.TYPE_INT_RGB);
- Graphics2D g = bufImage.createGraphics();
- //设定背景颜色
- g.setColor(new Color(0xDCDCDC));
- g.fillRect(0, 0, 150, 50);
- //画边框
- g.setColor(Color.black);
- g.drawRect(0, 0, 150 - 1, 50 - 1);
- //设置输出字体
- g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
- //字体的平滑处理
- g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- try {
- int i = 30; //行高
- int num = 0;
- //每行显示的字体数量
- char[] fun = new char[31];
- int charnum = 0;
- while ((charnum = reader.read()) != -1) {
- fun[num++] = (char) charnum;
- if (fun[30] != 0) {
- System.out.println(new String(fun));
- g.drawString(new String(fun), 20, i); //20和i值是图片上的x,y坐标
- i = i + 30; //换行,通过增加行高实现
- num = 0;
- Arrays.fill(fun, '\0');
- }
- }
- g.drawString(new String(fun), 20, i);
- FileOutputStream fos = new FileOutputStream("d:/img/bar22.jpeg");
- g.dispose();
- ImageIO.write(bufImage, "JPEG", fos);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|