博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java绘制带姓的圆
阅读量:4635 次
发布时间:2019-06-09

本文共 2770 字,大约阅读时间需要 9 分钟。

public class ImageGenerator {	private static final Color[] colors = new Color[] { new Color(129, 198, 132), new Color(149, 117, 204), new Color(255, 187, 68),			new Color(41, 181, 245), new Color(254, 138, 128), new Color(240, 98, 146) };	/**	 * 生成图片 int width = 100; int height = 100;	 * 	 * @param name	 * @param width	 * @param height	 * @throws IOException	 */	public static InputStream generateImg(char ch) throws IOException {		return generateImg(ch, 80, 80, 40);	}	/**	 * 	 * @param ch	 * @param width	 * @param height	 * @throws IOException	 */	public static InputStream generateImg(char ch, int width, int height, int fontSize) throws IOException {		BufferedImage bufferedImage = generateBufferedImage(ch, width, height, fontSize);		ByteArrayOutputStream bs = new ByteArrayOutputStream();		ImageIO.write(bufferedImage, "png", bs);		InputStream is = new ByteArrayInputStream(bs.toByteArray());		return is;	}	/**	 * 	 * @param ch	 * @param width	 * @param height	 * @throws IOException	 */	public static BufferedImage generateBufferedImage(char ch, int width, int height, int fontSize) throws IOException {		Font font = new Font("黑体", Font.PLAIN, fontSize);		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);		Graphics2D g2 = (Graphics2D) bufferedImage.createGraphics();		// 增加下面代码使得背景透明		bufferedImage = g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);		g2.dispose();		g2 = bufferedImage.createGraphics();				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);				// g2.setBackground(getRandomColor());		// g2.clearRect(0, 0, width, height);		g2.setFont(font);		g2.drawOval(0, 0, width, height);		g2.setPaint(getRandomColor());		g2.fillOval(0, 0, width, height);				g2.setPaint(Color.white);		FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);		int w = fm.charWidth(ch);		int h = fm.getAscent();		g2.drawString((ch + "").toUpperCase(), (width - w) / 2, h + (height - h) / 2 - (fm.getHeight() - h) / 2);		g2.dispose();		return bufferedImage;	}	public static Color getRandomColor() {		Random r = new Random();		return colors[r.nextInt(colors.length)];	}	public static void main(String[] args) {		File imgFile = new File("D:/1111111111111111.png");		File imgFile2 = new File("D:/11111111111111cdscsdcsdcsdcsdc11.png");		try {			IOUtils.copy(ImageGenerator.generateImg('何'), new FileOutputStream(imgFile));			ImageIO.write(ImageGenerator.generateBufferedImage('A', 100, 100, 50), "png", imgFile2);		} catch (IOException e) {			e.printStackTrace();		}		System.out.println("生成完成");	}}

  

转载于:https://www.cnblogs.com/heml/p/6633249.html

你可能感兴趣的文章
Educational Codeforces Round 39 B Weird Subtraction Process
查看>>
使用邮件规则,将收到的邮件进行分类
查看>>
ASP.NET文件的下载
查看>>
桶排序和计数排序
查看>>
使用邻接矩阵构造图
查看>>
virtual hust 2013.6.20 数论基础题目 D - Just the Facts
查看>>
eShopOnContainers 知多少[8]:Ordering microservice
查看>>
bzoj 1040: [ZJOI2008]骑士 树形dp
查看>>
hdu 1423
查看>>
hdu 4720
查看>>
友元关系
查看>>
IO缓冲区
查看>>
一道SQL统计试题
查看>>
OO第二次博客作业
查看>>
mysql 批量更新数据类型
查看>>
HDU 1273 漫步森林
查看>>
Bootstrap4.x 新增
查看>>
太久没来了,好尴尬呀
查看>>
django 链接地址匹配流程
查看>>
图片和文件上传的两款插件
查看>>