代码实例
// 读取原图片信息
//得到文件
File file = new File("d:\\1.png");
//文件转化为图片
Image srcImg = ImageIO.read(file);
//获取图片的宽
int srcImgWidth = srcImg.getWidth(null);
//获取图片的高
int srcImgHeight = srcImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
FontRenderContext frc = g2.getFontRenderContext();
TextLayout tl = new TextLayout("你好", new Font("黑体", Font.BOLD, 25), frc);
//文字所在位置
Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(690,390));
g2.setStroke(new BasicStroke(3.0f));
//边框颜色
g2.setColor(new Color(178, 113, 23));
g2.draw(sha);
//字体颜色
g2.setColor(Color.WHITE);
g2.fill(sha);
g2.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bufImg, "png", imOut);
InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
OutputStream outStream = new FileOutputStream("d:\\3.png");
IOUtils.copy(inputStream, outStream);
inputStream.close();
outStream.close();
评论 (0)