首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向图像中添加水印

向图像中添加水印
EN

Stack Overflow用户
提问于 2016-08-25 08:39:45
回答 1查看 2.8K关注 0票数 4

我试图在我的网站上添加水印到图片上。我希望它的工作方式是显示水印时,图像被下载或显示在其他网站。在我的网站上,我想显示它没有水印。如果水印被放置在图片的上,那就太棒了。这是否可以通过使用图片、元数据或http头来完成上述操作?或者这是不可能的,我应该通过添加一个页脚上传和隐藏它吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-10 04:28:00

您可以引用以下链接:

向图像添加文本水印

代码语言:javascript
运行
复制
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);
    }
}

向图像中添加图像水印

代码语言:javascript
运行
复制
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);
    }
}

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39140494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档