java图片加水印

java基础 专栏收录该内容
53 篇文章 0 订阅

 

 

参考链接:http://www.codejava.net/java-se/graphics/adding-a-watermark-over-an-image-programmatically-using-java

 

一: 添加文字水印:

 

/**
 * Embeds a textual watermark over a source image to produce
 * a watermarked one.
 * @param text The text to be embedded as watermark.
 * @param sourceImageFile The source image file.
 * @param destImageFile The output image file.
 */
static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
    try {
        BufferedImage sourceImage = ImageIO.read(sourceImageFile);
        Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
 
        // initializes necessary graphic properties
        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
        g2d.setComposite(alphaChannel);
        g2d.setColor(Color.BLUE);
        g2d.setFont(new Font("Arial", Font.BOLD, 64));
        FontMetrics fontMetrics = g2d.getFontMetrics();
        Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
 
        // calculates the coordinate where the String is painted
        int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
        int centerY = sourceImage.getHeight() / 2;
 
        // paints the textual watermark
        g2d.drawString(text, centerX, centerY);
 
        ImageIO.write(sourceImage, "png", destImageFile);
        g2d.dispose();
 
        System.out.println("The tex watermark is added to the image.");
 
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

 

二:加图片水印:

 

/**
 * Embeds an image watermark over a source image to produce
 * a watermarked one.
 * @param watermarkImageFile The image file used as the watermark.
 * @param sourceImageFile The source image file.
 * @param destImageFile The output image file.
 */
static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) {
    try {
        BufferedImage sourceImage = ImageIO.read(sourceImageFile);
        BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);
 
        // initializes necessary graphic properties
        Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
        g2d.setComposite(alphaChannel);
 
        // calculates the coordinate where the image is painted
        int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
        int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;
 
        // paints the image watermark
        g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);
 
        ImageIO.write(sourceImage, "png", destImageFile);
        g2d.dispose();
 
        System.out.println("The image watermark is added to the image.");
 
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值