【发布时间】:2016-06-13 12:08:10
【问题描述】:
我正在尝试在图像和视频上添加水印。对于图像,我得到了如下解决方案
图片水印代码
方法
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 中进行视频水标记
【问题讨论】: