java 水印字体 斜_斜水印,重复水印,文字

转自:http://layicr.i.sohu.com/blog/view/180340898.htm

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.Shape;

import java.awt.font.GlyphVector;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.IIOException;

import javax.imageio.ImageIO;

/**

* 斜水印,重复水印,文字

* @param pressText  文字

* @param targetImg  目标图片

* @param fontName 字体名称

* @param colorStr 字体颜色字符串,格式如:#29944f

* @param fontSize  字体大小

* @param alpha 透明度(0.1-0.9)

* @param carelessness true为字体实心,false为字体空心

* @return

*/

public

static BufferedImage pressText(String pressText, String targetImg,

String fontName, String colorStr, int fontSize, float alpha,boolean

carelessness) {

try {

File file = new File(targetImg);

Image src = ImageIO.read(file);

//图片宽度

int width = src.getWidth(null);

//图片高度

int height = src.getHeight(null);

BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

Graphics2D g2d  = image.createGraphics();

//绘原图

g2d.drawImage(src, 0, 0, width, height, null);

//比例

g2d.scale(1, 1);

g2d.addRenderingHints(new RenderingHints(

RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON));

g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

//颜色

Color color = Color.decode(colorStr);

//字体

Font font = new Font(fontName, Font.PLAIN, fontSize);

GlyphVector fontGV = font.createGlyphVector(g2d.getFontRenderContext(),

pressText);

Rectangle size = fontGV

.getPixelBounds(g2d.getFontRenderContext(), 0, 0);

Shape textShape = fontGV.getOutline();

double textWidth = size.getWidth();

double textHeight = size.getHeight();

AffineTransform rotate45 = AffineTransform

.getRotateInstance(Math.PI / 4d);

Shape rotatedText = rotate45.createTransformedShape(textShape);

// use a gradient that repeats 4 times

g2d.setPaint(new GradientPaint(0, 0, color,

image.getWidth() / 2, image.getHeight() / 2,color));

//透明度

g2d.setStroke(new BasicStroke(alpha));

// step in y direction is calc'ed using pythagoras + 5 pixel padding

double yStep = Math.sqrt(textWidth * textWidth / 2) + 5;

// step over image rendering watermark text

for (double x = -textHeight * 3; x < image.getWidth(); x += (textHeight * 3)) {

double y = -yStep;

for (; y < image.getHeight(); y += yStep) {

g2d.draw(rotatedText);

if(carelessness)//字体实心

{

g2d.fill(rotatedText);

}

g2d.translate(0, yStep);

}

g2d.translate(textHeight * 3, -(y + yStep));

}

g2d.dispose();

return image;

} catch(IIOException e){

logger.warn(e);

} catch (Exception e) {

logger.warn(e);

}

return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值