经典100%纯手工实现视频图片水印处理,附源码
Posted 洛阳泰山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经典100%纯手工实现视频图片水印处理,附源码相关的知识,希望对你有一定的参考价值。
图片水印效果
视频水印效果
视频水印梳理java代码实现
Java spring boot项目或者maven项目的pom文件中添加以下依赖
<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
核心代码,单类实现,idea创建VideoWatermark 类,复制粘贴下面代码,右键点击run运行主方法即可。
主要有图片文字水印和视频水印两个方法,大家可以根据自己的的需求对方法里的参数修改。
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import sun.font.FontDesignMetrics;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class VideoWatermark {
final static String videoFolderPath = "C:/Users/tarzan/Desktop/video/";
final static String videoName = "video.mp4";
final static String imageFolderPath = "C:/Users/tarzan/Desktop/image/";
final static String imageName = "logo.jpg";
public static void main(String[] args) throws Exception {
// videoProcess(videoFolderPath + videoName);
imageProcess(imageFolderPath + imageName);
}
//视频水印
public static void videoProcess(String filePath){
//抓取资源
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(filePath);
Frame frame;
try{
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(videoFolderPath+"new"+videoName,grabber.getImageWidth(),grabber.getImageHeight(),grabber.getAudioChannels());
recorder.setVideoCodec(grabber.getVideoCodec());
recorder.setFormat(grabber.getFormat());
recorder.setFrameRate(grabber.getFrameRate());
recorder.setPixelFormat(0);
recorder.setVideoBitrate(grabber.getVideoBitrate());
recorder.start();
int grabSize = grabber.getLengthInFrames();
for (int i = 0; i < grabSize; i++) {
//处理图像
frame = grabber.grabImage();
if (frame != null && frame.image != null) {
System.out.println("视频共"+grabSize+"帧,正处理第"+(i+1)+"帧图片");
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(frame);
BufferedImage bufferedImage=watermark(bi,i+1);
recorder.record(converter.convert(bufferedImage));
}
//处理音频
frame=grabber.grab();
if(frame != null && frame.samples!=null){
recorder.recordSamples(frame.sampleRate,frame.audioChannels,frame.samples);
}
}
recorder.stop();
recorder.release();
grabber.stop();
}catch (Exception e){
e.printStackTrace();
}
}
//图片水印
public static void imageProcess(String sourceFilePath){
try {
BufferedImage image = ImageIO.read(new File(sourceFilePath));
fontMark(image,"洛阳泰山出品");
String format=sourceFilePath.substring(sourceFilePath.lastIndexOf(".")+1);
ImageIO.write(image,format, new File(imageFolderPath +"new"+ imageName));
System.out.println("图片水印生成");
} catch (IOException e) {
e.printStackTrace();
}
}
//水印处理
public static BufferedImage watermark(BufferedImage bufImg,int num) {
backgroundBar(bufImg);
fontMark(bufImg,"洛阳泰山出品");
creditRoll(bufImg,"洛阳泰山出品",num);
return bufImg;
}
//文字标记
public static BufferedImage fontMark(BufferedImage bufImg,String content) {
Font font = new Font("微软雅黑", Font.BOLD, 32);
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
Graphics2D graphics = bufImg.createGraphics();
graphics.setColor(Color.orange);
graphics.setFont(font);
graphics.drawString(content, 0, metrics.getAscent());
graphics.dispose();
return bufImg;
}
//背景条
public static BufferedImage backgroundBar(BufferedImage bufImg) {
int width = bufImg.getWidth();//图宽
int height = bufImg.getHeight();//图高
Graphics2D graphics = bufImg.createGraphics();
graphics.setColor(Color.BLUE);
graphics.fillRect(0, height-110, width, 110);
graphics.dispose();
return bufImg;
}
//滚动字幕
public static BufferedImage creditRoll(BufferedImage bufImg,String content,int num) {
Font font = new Font("微软雅黑", Font.BOLD, 18);
int width = bufImg.getWidth();//图宽
int height = bufImg.getHeight();//图高
Graphics2D graphics = bufImg.createGraphics();
graphics.setColor(Color.white);
graphics.setFont(font);
graphics.drawString(content, width-(num*5), height-40);
graphics.dispose();
return bufImg;
}
}
控制台运行输出日志
相关文章推荐
《震惊,java仅用30行代码就实现了视频转音频的批量转换》
特别说明:如需转载此篇文章,请先关注本人后,注明作者名称,和文章链接地址即可。
以上是关于经典100%纯手工实现视频图片水印处理,附源码的主要内容,如果未能解决你的问题,请参考以下文章