| | |
| | | package com.ruoyi.device.hiksdk.util.imageUtil; |
| | | |
| | | import org.bytedeco.ffmpeg.global.avutil; |
| | | 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.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.util.Random; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | /** |
| | | * @ClassName WaterMarkUtil |
| | | * @Description: |
| | |
| | | * @Version 1.0 |
| | | */ |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | |
| | | /** |
| | | * 图片水印工具类 |
| | | * |
| | | * 图片水印工具类 |
| | | */ |
| | | public class waterMarkUtil { |
| | | |
| | | // 水印透明度 |
| | | private static final float alpha = 0.8f; |
| | | private static final float alpha = 0.3f; |
| | | // 水印横向位置 |
| | | private static int positionWidth = 100; |
| | | private static int positionWidth = 500; |
| | | // 水印纵向位置 |
| | | private static int positionHeight = 300; |
| | | private static int positionHeight = 500; |
| | | // 水印文字字体 |
| | | private static final Font font = new Font("宋体", Font.BOLD, 30); |
| | | private static final Font font = new Font("微软雅黑", Font.BOLD, 80); |
| | | // 水印文字颜色 |
| | | private static final Color color = Color.white; |
| | | private static final Color color = Color.blue; |
| | | |
| | | /** |
| | | * 给图片添加水印文字 |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | // public static void main(String[] args) { |
| | | // String srcImgPath = "D:\\testSmile.jpg"; |
| | | // String text = "JCccc"; |
| | | |
| | | public static void markVideo(String text, String srcImgPath, String targetPath) { |
| | | avutil.av_log_set_level(avutil.AV_LOG_ERROR); |
| | | File file = new File(srcImgPath); |
| | | //抓取视频资源 |
| | | FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file); |
| | | // |
| | | Frame frame = null; |
| | | FFmpegFrameRecorder recorder = null; |
| | | // String fileName = null; |
| | | try { |
| | | frameGrabber.start(); |
| | | //Random random = new Random(); |
| | | // fileName = file.getAbsolutePath() + random.nextInt(100)+".mp4"; |
| | | // System.out.println("文件名-->>"+fileName); |
| | | frameGrabber.setFrameRate(25); // 设置每秒处理10帧 |
| | | recorder = new FFmpegFrameRecorder(targetPath, 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.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); |
| | | |
| | | recorder.start(); |
| | | int index = 0; |
| | | while (true) { |
| | | frame = frameGrabber.grabFrame(); |
| | | if (frame == null) { |
| | | System.out.println("视频处理完成"); |
| | | break; |
| | | } |
| | | //判断音频 |
| | | // System.out.println("音频==" + (frame.samples == null) + "视频==" + (frame.image == null)); |
| | | //判断图片帧 |
| | | if (frame.image != null) { |
| | | IplImage iplImage = Java2DFrameUtils.toIplImage(frame); |
| | | BufferedImage buffImg = Java2DFrameUtils.toBufferedImage(iplImage); |
| | | Graphics2D graphics = buffImg.createGraphics(); |
| | | graphics.setColor(color); |
| | | graphics.setFont(font); |
| | | // 设置透明度 |
| | | float alpha = 0.5f; // 设置透明度值,范围为 0.0(完全透明)到 1.0(完全不透明) |
| | | AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); |
| | | graphics.setComposite(alphaComposite); |
| | | graphics.drawString(text, positionWidth, positionHeight); |
| | | graphics.dispose(); |
| | | Frame newFrame = Java2DFrameUtils.toFrame(buffImg); |
| | | recorder.record(newFrame); |
| | | } |
| | | //设置音频 |
| | | // if (frame.samples != null) { |
| | | // recorder.recordSamples(frame.sampleRate, frame.audioChannels, frame.samples); |
| | | // } |
| | | // System.out.println("帧值=" + index); |
| | | index++; |
| | | } |
| | | |
| | | recorder.stop(); |
| | | recorder.release(); |
| | | frameGrabber.stop(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // String srcImgPath = "G:\\data\\pic\\7aad76110e7c478598da6b82d7446246.jpeg"; |
| | | // String text = "synjones"; |
| | | // // 给图片添加水印文字 |
| | | // markImage(text, srcImgPath, "D:\\testSmileWithMark.jpg"); |
| | | // //markImage(text, srcImgPath, "G:\\data\\pic\\微信截图水印.png"); |
| | | // // 给图片添加水印文字,水印文字旋转-45 |
| | | // markImage(text, srcImgPath, "D:\\testSmileWithMarkRotate.jpg", -45); |
| | | // markImage(text, srcImgPath, "G:\\data\\pic\\7aad76110e7c478598da6b82d7446246水印.png", 45); |
| | | // System.out.println("给图片添加水印文字完毕"); |
| | | // } |
| | | |
| | | |
| | | String videoPath="D:\\1.mp4"; |
| | | markVideo("安瑞达科技",videoPath,"D:\\2.mp4"); |
| | | System.out.println("给视频添加水印文字完毕"); |
| | | } |
| | | } |