Java/JSP:如何在视频上添加WaterMark [英] Java/JSP: How to add WaterMark on Video

查看:291
本文介绍了Java/JSP:如何在视频上添加WaterMark的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在图像和视频上添加水印.对于图像,我得到的解决方案如下

图像水印代码

方法

static void addWatermarkOnImage(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);
    }
}

方法调用

File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png");
File destImageFile = new File("e:/Test/Watermark/text_watermarked.png");

addTextWatermark("CodeJava", sourceImageFile, destImageFile);

由于没有上面的代码图像,WaterMarking对我来说是完美的.在视频水印的情况下,我在网上尝试了很多示例,但没有任何帮助.因此,请任何人帮助我在java/jsp中进行视频水印

解决方案

在视频(或文本中出现水印)中最常见,最流行的开源工具可能是ffmpeg:

ffmpeg可以在命令行中使用,也可以作为应用程序的一部分以编程方式使用(如果要分发等,请仔细查看许可以确保它满足您的需求).

要以编程方式使用它,可以在包装器中使用命令行工具,也可以直接使用基于它的库.

直接使用库(libavcodec,libavutil,libavformat,libavfilter,libavdevice,libswscale和libswresample-请参见上面链接的关于"页面)应该更有效并且可以进行更大的控制等,但是包装方法仍然很流行一开始可能更容易实现,并且有大量关于命令行语法的在线信息可以实现您可能想做的几乎所有事情,您可以通过这种方法加以利用.

这些库也是基于c的,因此无论如何您都需要一些接口(例如JNI).

围绕ffmpeg的Java包装器的示例是:

有许多使用ffmpeg工具在视频中添加水印的在线示例-此处是一个示例,但值得一提的是查找语法随时间变化的最新广告:

I'm trying to add water mark on image and video. For image i got the solution as below

Image water marking code

Method

static void addWatermarkOnImage(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);
    }
}

Method calling

File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png");
File destImageFile = new File("e:/Test/Watermark/text_watermarked.png");

addTextWatermark("CodeJava", sourceImageFile, destImageFile);

By fallowing above code image WaterMarking is working perfectly for me. In case of video water marking i tried lot of examples in online but nothing was helpful for me. So, kindly any one help me for video water marking in java/jsp

解决方案

The most common and popular open source tool to put a watermark in a video (or text as in another question that just came up) is probably ffmpeg:

ffmpeg can be used in the command line or programatically as part of an application (look at the licensing carefully to make sure it meets your needs if you want to distribute etc).

To use it programatically you can either use the command line tool in a wrapper or use the libraries it is built on directly.

Using the libraries directly (libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale and libswresample - see the about page at the link above) should be more efficient and allow greater control etc, but the wrapper approach is still popular as it can be simpler to implement at first and there is a wealth of online info on command line syntax to achieve pretty much anything you might want to do, which you get to leverage with this approach.

The libraries are also c based so you will need some interface (e.g. JNI) to them anyway.

An example of a Java wrapper around ffmpeg is:

There are many online examples of using the ffmpeg tool to add a watermarking to a video - one example is here but it is worth googling to find the latest ad the syntax does change over time:

这篇关于Java/JSP:如何在视频上添加WaterMark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆