cc985a8364307055a9508d1d04124c6feb536528.svn-base 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package cn.com.goldenwater.dcproj.utils;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.geom.RoundRectangle2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.StringReader;
  10. import java.util.Arrays;
  11. import java.util.Random;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. public class AvatarHelper {
  15. /**
  16. * @throws IOException
  17. * @throws
  18. **/
  19. public static void main(String[] args) throws IOException {
  20. String tep = "李海超";
  21. StringReader reader = new StringReader(tep);
  22. //150×50的图片
  23. BufferedImage bufImage = new BufferedImage(150, 50, BufferedImage.TYPE_INT_RGB);
  24. Graphics2D g = bufImage.createGraphics();
  25. //设定背景颜色
  26. g.setColor(new Color(0xDCDCDC));
  27. g.fillRect(0, 0, 150, 50);
  28. //画边框
  29. g.setColor(Color.black);
  30. g.drawRect(0, 0, 150 - 1, 50 - 1);
  31. //设置输出字体
  32. g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
  33. //字体的平滑处理
  34. g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  35. try {
  36. int i = 30; //行高
  37. int num = 0;
  38. //每行显示的字体数量
  39. char[] fun = new char[31];
  40. int charnum = 0;
  41. while ((charnum = reader.read()) != -1) {
  42. fun[num++] = (char) charnum;
  43. if (fun[30] != 0) {
  44. System.out.println(new String(fun));
  45. g.drawString(new String(fun), 20, i); //20和i值是图片上的x,y坐标
  46. i = i + 30; //换行,通过增加行高实现
  47. num = 0;
  48. Arrays.fill(fun, '\0');
  49. }
  50. }
  51. g.drawString(new String(fun), 20, i);
  52. FileOutputStream fos = new FileOutputStream("d:/img/bar22.jpeg");
  53. g.dispose();
  54. ImageIO.write(bufImage, "JPEG", fos);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }