1
This commit is contained in:
+13
-4
@@ -19,10 +19,19 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.opencv</groupId>
|
||||
<artifactId>opencv</artifactId>
|
||||
<version>470</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/main/resources/opencv-470.jar</systemPath>
|
||||
<artifactId>opencv-480</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<!--JAVA使用javacv实现图片合成短视频,相关JAR包-->
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>javacv</artifactId>
|
||||
<version>1.5.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
<artifactId>javacv-platform</artifactId>
|
||||
<version>1.5.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,49 +1,50 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfByte;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
|
||||
public class DemoApplicationTests {
|
||||
/**
|
||||
* @return
|
||||
* @Description
|
||||
* @Param
|
||||
* @Author zhangsan
|
||||
* @Date 2020.09.05 9:43
|
||||
**/
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 解决awt报错问题
|
||||
// 加载动态库
|
||||
URL url = ClassLoader.getSystemResource("opencv_java470.dll");
|
||||
System.load(url.getPath());
|
||||
// 读取图像
|
||||
// Mat image = Imgcodecs.imread("D:\\1.jpg");
|
||||
File file = new File("D:/1.jpg");
|
||||
InputStream fos = new FileInputStream(file);
|
||||
Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR);
|
||||
if (image.empty()) {
|
||||
throw new Exception("image is empty");
|
||||
}
|
||||
HighGui.imshow("Original Image", image);
|
||||
|
||||
// 创建输出单通道图像
|
||||
Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
|
||||
// 进行图像色彩空间转换
|
||||
Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);
|
||||
|
||||
HighGui.imshow("Processed Image", grayImage);
|
||||
Imgcodecs.imwrite("D:/hello.jpg", grayImage);
|
||||
HighGui.waitKey();
|
||||
}
|
||||
}
|
||||
//package com.wyl.javacv;
|
||||
//
|
||||
//
|
||||
//import org.opencv.core.CvType;
|
||||
//import org.opencv.core.Mat;
|
||||
//import org.opencv.core.MatOfByte;
|
||||
//import org.opencv.highgui.HighGui;
|
||||
//import org.opencv.imgcodecs.Imgcodecs;
|
||||
//import org.opencv.imgproc.Imgproc;
|
||||
//import org.opencv.videoio.VideoWriter;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.FileInputStream;
|
||||
//import java.io.InputStream;
|
||||
//import java.net.URL;
|
||||
//
|
||||
//
|
||||
//public class DemoApplicationTests {
|
||||
// /**
|
||||
// * @return
|
||||
// * @Description
|
||||
// * @Param
|
||||
// * @Author zhangsan
|
||||
// * @Date 2020.09.05 9:43
|
||||
// **/
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// // 解决awt报错问题
|
||||
// // 加载动态库
|
||||
// URL url = ClassLoader.getSystemResource("opencv_java480.dll");
|
||||
// System.load(url.getPath());
|
||||
// // 读取图像
|
||||
//// Mat image = Imgcodecs.imread("D:\\1.jpg");
|
||||
// File file = new File("C:\\Users\\95981\\Desktop\\Mattingfailure.jpg");
|
||||
// InputStream fos = new FileInputStream(file);
|
||||
// Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR);
|
||||
// if (image.empty()) {
|
||||
// throw new Exception("image is empty");
|
||||
// }
|
||||
// HighGui.imshow("Original Image", image);
|
||||
//
|
||||
// // 创建输出单通道图像
|
||||
// Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
|
||||
// // 进行图像色彩空间转换
|
||||
// Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY);
|
||||
//
|
||||
// HighGui.imshow("Processed Image", grayImage);
|
||||
// Imgcodecs.imwrite("D:/hello.jpg", grayImage);
|
||||
// HighGui.waitKey();
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfByte;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.videoio.VideoWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
public class ImageToVideoConverter {
|
||||
public static void main(String[] args) throws IOException {
|
||||
URL url = ClassLoader.getSystemResource("opencv_java480.dll");
|
||||
System.load(url.getPath());
|
||||
String imageFolderPath = "C:\\Users\\95981\\Desktop\\picture"; // 你的图片文件夹路径
|
||||
String outputVideoPath = "C:\\Users\\95981\\Desktop\\vidoe\\video14.mp4"; // 输出视频文件路径
|
||||
|
||||
try {
|
||||
Size frameSize = new Size(910, 910); // 设置视频帧的大小(根据你的图片大小进行调整)
|
||||
int fps = 15; // 视频帧率
|
||||
int x = VideoWriter.fourcc('D', 'I', 'V', 'X');
|
||||
VideoWriter videoWriter = new VideoWriter(outputVideoPath, x, fps, frameSize, true);
|
||||
|
||||
// 获取图片文件列表
|
||||
File imageFolder = new File(imageFolderPath);
|
||||
File[] imageFiles = imageFolder.listFiles();
|
||||
|
||||
if (imageFiles != null) {
|
||||
for (File imageFile : imageFiles) {
|
||||
if (imageFile.isFile()) {
|
||||
// Mat image = Imgcodecs.imread(imageFile.getAbsolutePath());
|
||||
InputStream fos = new FileInputStream(imageFile);
|
||||
Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR);
|
||||
if (!image.empty()) {
|
||||
// 调整图片大小,以适应视频帧大小(可选,如果图片与视频帧大小相同,可省略)
|
||||
// Imgproc.resize(image, image, frameSize);
|
||||
|
||||
videoWriter.write(image);
|
||||
image.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
videoWriter.release(); // 释放VideoWriter资源
|
||||
System.out.println("Video created successfully!");
|
||||
} else {
|
||||
System.out.println("No image files found in the folder.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
File file = new File(outputVideoPath);
|
||||
file.deleteOnExit();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.wyl.javacv;
|
||||
|
||||
import org.bytedeco.ffmpeg.global.avcodec;
|
||||
import org.bytedeco.ffmpeg.global.avutil;
|
||||
import org.bytedeco.javacv.FFmpegFrameRecorder;
|
||||
import org.bytedeco.javacv.FrameRecorder;
|
||||
import org.bytedeco.javacv.Java2DFrameConverter;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaCvTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
//合成的MP4 存放的地址路径 这里的路径并不会自动创建,需要手动提前创建好,否则会报错:Could not open 'null'
|
||||
String mp4SavePath = "C:\\Users\\95981\\Desktop\\vidoe\\img2.mp4";
|
||||
//图片存放的地址路径
|
||||
String img = "C:\\Users\\95981\\Desktop\\picture";
|
||||
int width = 910;
|
||||
int height = 910;
|
||||
//读取所有图片
|
||||
File file = new File(img);
|
||||
File[] files = file.listFiles();
|
||||
Map<Integer, File> imgMap = new HashMap<Integer, File>();
|
||||
int num = 0;
|
||||
for (File imgFile : files) {
|
||||
imgMap.put(num, imgFile);
|
||||
num++;
|
||||
}
|
||||
createMp4(mp4SavePath, imgMap, width, height);
|
||||
}
|
||||
|
||||
private static void createMp4(String mp4SavePath, Map<Integer, File> imgMap, int width, int height) throws FrameRecorder.Exception {
|
||||
//视频宽高最好是按照常见的视频的宽高 16:9 或者 9:16
|
||||
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mp4SavePath, width, height);
|
||||
int bitrate = 1000000;
|
||||
//设置视频编码层模式 import org.bytedeco.ffmpeg.global.avcodec;可能需要手动复制添加
|
||||
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
|
||||
//设置视频为25帧每秒
|
||||
recorder.setFrameRate(15);
|
||||
//设置视频图像数据格式 import org.bytedeco.ffmpeg.global.avutil;可能需要手动复制添加
|
||||
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
|
||||
recorder.setVideoBitrate(bitrate);
|
||||
|
||||
recorder.setFormat("mp4");
|
||||
try {
|
||||
recorder.start();
|
||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
//录制一个22秒的视频,22秒为自定义的一个视频时间长度,图片少则在22秒内,多则到22秒停止
|
||||
|
||||
imgMap.entrySet().stream().forEach(e -> {
|
||||
BufferedImage read = null;
|
||||
try {
|
||||
read = ImageIO.read(e.getValue());
|
||||
} catch (IOException ioException) {
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
try {
|
||||
recorder.record(converter.getFrame(read));
|
||||
} catch (FrameRecorder.Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//最后一定要结束并释放资源
|
||||
recorder.stop();
|
||||
recorder.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user