java播放rtsp视频(连接网络摄像头)和截取视频图片

依赖

<dependency>
	<groupId>org.bytedeco</groupId>
	<artifactId>javacv-platform</artifactId>
	<version>1.4.4</version>
</dependency>

简单代码

代码是从网络上搬运的,测试过可以使用。

public class Test {
	public static void main(String[] args) throws IOException {
		String file = "rtsp://admin:[email protected]/1";
		FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(file);
		grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
		grabber.setImageWidth(960);
		grabber.setImageHeight(540);
		System.out.println("grabber start");
		grabber.start();
		
//1.播放视频
/* 
		CanvasFrame canvasFrame = new CanvasFrame("摄像机");
		canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		canvasFrame.setAlwaysOnTop(true);
		OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
		while (true){
			Frame frame = grabber.grabImage();
			opencv_core.Mat mat = converter.convertToMat(frame);
			canvasFrame.showImage(frame);
		}
 */
		

//2.帧截图
/*
		File outPut = new File("E:\\aa.jpg");
		while (true){
				Frame frame = grabber.grabImage();
				if (frame != null) {
					ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
					grabber.stop();
					grabber.release();
					System.out.println("图片已保存");
					break;
				}
		
		}

 */
		

	}

	/**
	 * 创建BufferedImage对象
	 */
	public static BufferedImage FrameToBufferedImage(Frame frame) {
		Java2DFrameConverter converter = new Java2DFrameConverter();
		BufferedImage bufferedImage = converter.getBufferedImage(frame);
//		bufferedImage=rotateClockwise90(bufferedImage);
		return bufferedImage;
	}
	/**
	 * 处理图片,将图片旋转90度。
	 */
	public static BufferedImage rotateClockwise90(BufferedImage bi) {
		int width = bi.getWidth();
		int height = bi.getHeight();
		BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
		for (int i = 0; i < width; i++)
			for (int j = 0; j < height; j++)
				bufferedImage.setRGB(j, i, bi.getRGB(i, j));
		return bufferedImage;
	}
}

你可能感兴趣的:(java,java,rtsp)