Java实现视频与图片添加水印
1、需要引入javacv依赖
<!-- javacv -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.4</version>
</dependency>
2、代码实现
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameUtils;
import org.bytedeco.opencv.opencv_core.IplImage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
@Slf4j
public class WaterMarkUtils {
private static final Font FONT = new Font("Microsoft Yahei", Font.BOLD, 40);
private static final AlphaComposite COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
private static final int X_MOVE = 300;
private static final int Y_MOVE = 140;
public static void markWithContentByImage(String srcImgPath,String waterMarkContent,String outImgPath) {
try {
Color markContentColor = new Color(180,180,180);
File srcFile = new File(srcImgPath);
File outFile = new File(outImgPath);
BufferedImage srcImg = ImageIO.read(srcFile);
int imgWidth = srcImg.getWidth();
int imgHeight = srcImg.getHeight();
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufImg.createGraphics();
graphics.drawImage(srcImg, 0, 0, imgWidth, imgHeight, null);
createText(markContentColor, waterMarkContent, bufImg, imgWidth, imgHeight, graphics);
try (FileOutputStream fos = new FileOutputStream(outFile);){
ImageIO.write(bufImg, "jpg", fos);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void markWithContentByVideo(String inputPath, String waterMarkContent, String outputPath) {
long l = System.currentTimeMillis();
File file = new File(inputPath);
Frame frame;
try(FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file)){
log.info("文件名-->>" + outputPath);
frameGrabber.start();
try (FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputPath, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels())){
recorder.setFormat("mp4");
recorder.setSampleRate(frameGrabber.getSampleRate());
recorder.setFrameRate(frameGrabber.getFrameRate());
recorder.setTimestamp(frameGrabber.getTimestamp());
recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
recorder.setVideoCodec(frameGrabber.getVideoCodec());
recorder.start();
Color markContentColor = new Color(180,180,180);
while (true){
frame = frameGrabber.grabFrame();
if(frame == null){
log.info("视频处理完成");
break;
}
if(frame.image != null){
IplImage iplImage = Java2DFrameUtils.toIplImage(frame);
BufferedImage bufImg = Java2DFrameUtils.toBufferedImage(iplImage);
int imgWidth = iplImage.width();
int imgHeight = iplImage.height();
Graphics2D graphics = bufImg.createGraphics();
createText(markContentColor, waterMarkContent, bufImg, imgWidth, imgHeight, graphics);
Frame newFrame = Java2DFrameUtils.toFrame(bufImg);
recorder.record(newFrame);
}
if(frame.samples != null){
recorder.recordSamples(frame.sampleRate,frame.audioChannels,frame.samples);
}
}
recorder.stop();
recorder.release();
frameGrabber.stop();
}
}catch (Exception e){
e.printStackTrace();
}
}
private static void createText(Color markContentColor, String waterMarkContent, BufferedImage bufImg, int imgWidth, int imgHeight, Graphics2D graphics) {
graphics.setColor(markContentColor);
graphics.setComposite(COMPOSITE);
graphics.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2,
(double) bufImg.getHeight() / 2);
graphics.setFont(FONT);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int xCoordinate = -imgWidth / 2;
int yCoordinate;
int markWidth = FONT.getSize() * getTextLength(waterMarkContent);
int markHeight = FONT.getSize();
double d = 1.5;
while (xCoordinate < imgWidth * d) {
yCoordinate = -imgHeight / 2;
while (yCoordinate < imgHeight * d) {
graphics.drawString(waterMarkContent, xCoordinate, yCoordinate);
yCoordinate += markHeight + Y_MOVE;
}
xCoordinate += markWidth + X_MOVE;
}
graphics.dispose();
}
public static int getTextLength(String text) {
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if (s.getBytes().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
public static void main(String[] args) {
markWithContentByVideo("C:\\Users\\test.mp4",
"测试水印","C:\\Users\\test1.mp4");
}
}