java实现图片平铺倾斜水印效果--转载

转载地址--java实现图片平铺倾斜水印效果_java_clh的博客-CSDN博客_java图片平铺

效果图:

public static void main(String[] args) throws IOException {

    File in = new File("图片路径+图片名称");
    File out = new File("图片路径+图片名称");
    addWaterMark(in,out,"水印文字");
}

public static void addWaterMark(File inputFile, File outputFile, String text) throws IOException {
        Image image = ImageIO.read(inputFile);
        int imgWidth = image.getWidth(null);// 获取图片的宽
        int imgHeight = image.getHeight(null);// 获取图片的高

        int angel = 315;//旋转角度
        int xpadding = 40;//每个水印水平间隔
        int ypadding = 40;//每个水印垂直间隔
        int fontSize = 10;

        BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //绘制原图片
        float alpha = 1F;
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        g.setComposite(ac);
        g.drawImage(image, 0, 0, imgWidth, imgHeight, null);
        g.setBackground(Color.BLACK);
        
        //开始绘制水印
        //水印字体
        Font font = new Font("微软雅黑", Font.BOLD, fontSize);
        g.setFont(font);
        FontRenderContext frc = g.getFontRenderContext();
        TextLayout tl = new TextLayout(text, font, frc);
        //水印串宽度
        int stringWidth = g.getFontMetrics(g.getFont()).charsWidth(text.toCharArray(), 0, text.length());

        //旋转水印
        g.rotate(Math.toRadians(angel), (double) imgWidth / 2, (double) imgHeight / 2);
        //水印透明度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
        // 字体色
        g.setColor(Color.RED);
        
        int x = -imgHeight / 2; 
        int y = -imgWidth / 2;
        
        //循环绘制
        while (x < imgWidth + imgHeight / 2) {
            y = -imgWidth / 2;
            while (y < imgHeight + imgWidth / 2) {
                Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
                g.fill(sha);
                
                y += ypadding;
            }
            x += stringWidth + xpadding;
        }
        
        //释放资源
        g.dispose();
        ImageIO.write(bi, "PNG", outputFile);
    }
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值