下载好opencv,加载opencv资源
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 这个 一定不要忘记
System.load("opencv_videoio_ffmpeg460_64.dll的文件路径");
}
获取视频流,并在窗口进行预览
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
public class RTSPStream {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 这个 一定不要忘记
System.load("F:\\software\\opencv\\opencv\\build\\bin\\opencv_videoio_ffmpeg460_64.dll");
}
public static void main(String[] args) throws IOException {
VideoCapture capture = new VideoCapture();
// 设置RTSP流地址
String rtspUrl = "rtsp://username:password@ip:port/path";
capture.open(rtspUrl);
Mat mat = new Mat();
String winName = "show";
HighGui.namedWindow(winName);
HighGui.resizeWindow(winName,800,600);
while (capture.read(mat)){
// 重置大小
Mat dst = new Mat();
Imgproc.resize(mat, dst, new Size(800,600));
// 显示
HighGui.imshow(winName, dst);
// waitkey 必须要,否则无法显示
int key = HighGui.waitKey(1);
System.out.println("key="+key);
//esc键退出
if(key == 27){
break;
}
}
HighGui.destroyAllWindows();
capture.release();
}
}
将每一帧数据保存为图片
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
public class RTSPStream {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 这个 一定不要忘记
System.load("F:\\software\\opencv\\opencv\\build\\bin\\opencv_videoio_ffmpeg460_64.dll");
}
public static void main(String[] args) throws IOException {
VideoCapture capture = new VideoCapture();
// 设置RTSP流地址
String rtspUrl = "rtsp://admin:kxyzn3180@192.168.1.64:554/Streaming/Channels/101";
capture.open(rtspUrl);
Mat mat = new Mat();
String winName = "show";
HighGui.namedWindow(winName);
HighGui.resizeWindow(winName,800,600);
while (capture.read(mat)){
// 重置大小
Mat dst = new Mat();
Imgproc.resize(mat, dst, new Size(800,600));
BufferedImage bufferedImage = matToImage(dst);
File file = new File("F:\\file\\111\\a.jpg");
ImageIO.write(bufferedImage,"jpg",file);
// 显示
HighGui.imshow(winName, dst);
// waitkey 必须要,否则无法显示
int key = HighGui.waitKey(1);
System.out.println("key="+key);
//esc键退出
if(key == 27){
break;
}
}
HighGui.destroyAllWindows();
capture.release();
}
private static BufferedImage matToImage(Mat mat) {
// 将Mat转换为BufferedImage
byte[] data = new byte[mat.channels() * mat.cols() * mat.rows()];
mat.get(0, 0, data); // 获取Mat的数据
BufferedImage bufferedImage = null;
if (mat.channels() == 1) {
bufferedImage = new BufferedImage(mat.cols(), mat.rows(), BufferedImage.TYPE_BYTE_GRAY);
} else if (mat.channels() == 3) {
bufferedImage = new BufferedImage(mat.cols(), mat.rows(), BufferedImage.TYPE_3BYTE_BGR);
} else if (mat.channels() == 4) {
bufferedImage = new BufferedImage(mat.cols(), mat.rows(), BufferedImage.TYPE_4BYTE_ABGR);
}
final byte[] targetPixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
System.arraycopy(data, 0, targetPixels, 0, data.length);
return bufferedImage;
}
}