springboot整合javacv实现打开本地摄像头直播

        
            org.bytedeco
            javacv-platform
            1.4.4
        
RecordPush
package com.vip.file.service;

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.presets.opencv_objdetect;
import org.bytedeco.javacv.*;

import javax.swing.*;

public class RecordPush {

    /**
     * 推流器
     * @param outputPath 接收路径
     * @param v_rs       帧率
     * @throws Exception
     * @throws org.bytedeco.javacv.FrameRecorder.Exception
     * @throws InterruptedException
     */
    public void getRecordPush(String outputPath, int v_rs) throws Exception, org.bytedeco.javacv.FrameRecorder.Exception, InterruptedException {

        Loader.load(opencv_objdetect.class);

        //创建采集器
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  //本地摄像头默认为0

        //开启采集器
        try {
            grabber.start();
        } catch (Exception e) {
            try {
                grabber.restart();  //一次重启尝试
            } catch (Exception e2) {
                throw e;
            }
        }

        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();  //转换器
        Frame grabframe = grabber.grab();  //获取一帧
        opencv_core.IplImage grabbedImage = null;
        if (grabframe != null) {
            grabbedImage = converter.convert(grabframe); //将这一帧转换为IplImage
        }

        //创建录制器
        FrameRecorder recorder;
        recorder = FrameRecorder.createDefault(outputPath, 1280, 720);   //输出路径,画面高,画面宽
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);  //设置编码格式
        recorder.setFormat("flv");
        recorder.setFrameRate(v_rs);
        recorder.setGopSize(v_rs);

        //开启录制器
        try {
            recorder.start();
        } catch (java.lang.Exception e) {
            System.out.println("recorder开启失败");
            System.out.println(recorder);
            try {
                if (recorder != null) {  //尝试重启录制器
                    recorder.stop();
                    recorder.start();
                }
            } catch (java.lang.Exception e1) {
                e.printStackTrace();
            }
        }

        //直播效果展示窗口
        CanvasFrame frame = new CanvasFrame("直播效果", CanvasFrame.getDefaultGamma() / grabber.getGamma());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setAlwaysOnTop(true);

        //推流
        while (frame.isVisible() && (grabframe = grabber.grab()) != null) {
            frame.showImage(grabframe);   //展示直播效果
            grabbedImage = converter.convert(grabframe);
            Frame rotatedFrame = converter.convert(grabbedImage);

            if (rotatedFrame != null) {
                recorder.record(rotatedFrame);
            }

            Thread.sleep(50);  //50毫秒/帧
        }
    }
}

启动类

@SpringBootApplication
public class FileApplication {

    public static void main(String[] args) {
//        SpringApplication.run(FileApplication.class, args);
        SpringApplicationBuilder builder = new SpringApplicationBuilder(FileApplication.class);
        builder.headless(false).run(args);
        System.out.println("---------------启动成功---------------");

        //设置rtmp服务器推流地址
        String outputPath = "rtmp://192.168.188.146:1935/live/stream";
        RecordPush recordPush = new RecordPush();
        try {
            recordPush.getRecordPush(outputPath, 25);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

springboot整合javacv实现打开本地摄像头直播_第1张图片

 首先你要有一个rtmp流媒体服务器,如何搭建。Windows基于Nginx搭建RTMP流媒体服务器(附带所有组件下载地址及验证方法)_windows搭建流媒体服务器_stalin_的博客-CSDN博客
 

你可能感兴趣的:(spring,boot,java,数学建模)