记一个小程序在后台通过Graphics2D 画图生成海报,并转换为base64返回前端使用到的封装方法 对图片进行操作

1.将图片切割为圆形

/**
 * 将图片切成圆形
 * @param bi1
 * @return
 */
public static BufferedImage gardenImage(BufferedImage bi1) {
	BufferedImage bi2;
	// 读取图片
	bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
			BufferedImage.TYPE_INT_RGB);
	Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
			.getHeight());
	Graphics2D g2 = bi2.createGraphics();
	g2.setBackground(Color.WHITE);
	g2.fill(new Rectangle(bi2.getWidth(), bi2.getHeight()));
	g2.setClip(shape);
	//设置抗锯齿
	g2.drawImage(bi1, 0, 0, null);
	g2.dispose();
	return bi2;
}

2.向图片写入文字

/**
* @param str
 *            生产的图片文字
 * @param width
 *            图片横向位置
 * @param height
 *            图片纵向位置
 * @param fontSize
 *            文字大小
 * @return
 * @throws IOException
 */
public static Graphics2D create(String str, Image image,int width, int height, Graphics2D g2,int fontSize, Color color ){
	BufferedImage bufferedImage = toBufferedImage(image);
	//这里减去25是为了防止字和图重合
	g2.drawImage(image, 0, 0, bufferedImage.getWidth() - 25, bufferedImage.getHeight() - 25, null);
	/* 设置生成图片的文字样式 * */
	Font font = new Font("黑体", Font.BOLD, fontSize);
	g2.setFont(font);
	g2.setPaint(color);

	/* 设置字体在图片中的位置 在这里是居中* */
	FontRenderContext context = g2.getFontRenderContext();
	Rectangle2D bounds = font.getStringBounds(str, context);
	double y = height;
	double ascent = -bounds.getY();
	double baseY = y + ascent;

	/* 防止生成的文字带有锯齿 * */
	g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

	/* 在图片上生成文字 * */
	g2.drawString(str, width, (int) baseY);

	return g2;
}

3.将Image 转为 BufferedImage

/**
 * 将Image 转为 BufferedImage
 * @param image
 * @return
 */
public static BufferedImage toBufferedImage(Image image) {
	if (image instanceof BufferedImage) {
		return (BufferedImage)image;
	}
	image = new ImageIcon(image).getImage();
	BufferedImage bimage = null;
	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	try {
		int transparency = Transparency.OPAQUE;
		GraphicsDevice gs = ge.getDefaultScreenDevice();
		GraphicsConfiguration gc = gs.getDefaultConfiguration();
		bimage = gc.createCompatibleImage(
				image.getWidth(null), image.getHeight(null), transparency);
	} catch (HeadlessException e) {

	}
	if (bimage == null) {
		int type = BufferedImage.TYPE_INT_RGB;
		bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
	}
	Graphics g = bimage.createGraphics();

	g.drawImage(image, 0, 0, null);
	g.dispose();

	return bimage;
}

4.将字节数组转为image对象

/**
 * 将字节数组转换为image对象
 * @param bytes
 * @return
 * @throws Exception
 */
public static Image byteToFile(byte[] bytes) throws Exception {
	ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
	Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
	Image image = null;
	try {
		ImageReader reader = (ImageReader) readers.next();
		Object source = bis;
		ImageInputStream iis = ImageIO.createImageInputStream(source);
		reader.setInput(iis, true);
		ImageReadParam param = reader.getDefaultReadParam();

		image = reader.read(0, param);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		bis.close();
	}
	return image;
}

5.将InputStream流转换为byte数组

/**
 * 将InputStream流转换为byte数组
 * @param in
 * @return
 * @throws IOException
 */
private byte[] getBytes(InputStream in) throws IOException {
	ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
	byte[] buff = new byte[100];
	int rc = 0;
	while ((rc = in.read(buff, 0, 100)) > 0) {
		swapStream.write(buff, 0, rc);
	}
	return swapStream.toByteArray();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值