javacv——讀取攝像頭的圖像、截取視頻的畫面

javacv開發包是用於支持java多媒體開發的一套開發包,能夠適用於本地多媒體(音視頻)調用以及音視頻,圖片等文件後期操做(圖片修改,音視頻解碼剪輯等等功能)。java

  這些須要引入的包。音視頻處理使用ffmpeg,圖像處理使用opencv,攝像頭抓取使用opencvcanvas

一、讀取攝像頭的圖像dom

public class JavavcCameraTest{
    public static void main(String[] args) throws Exception, InterruptedException{
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();   //開始獲取攝像頭數據
        CanvasFrame canvas = new CanvasFrame("攝像頭");//新建一個窗口
        canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.setAlwaysOnTop(true);
        while(true){
            if(!canvas.isDisplayable()){//窗口是否關閉
                grabber.stop();//中止抓取
                System.exit(2);//退出
                break;
            }    
            canvas.showImage(grabber.grab());//獲取攝像頭圖像並放到窗口上顯示, 這裏的Frame frame=grabber.grab(); frame是一幀視頻圖像
            Thread.sleep(200);//50毫秒刷新一次圖像
        }
    }
}

效果:spa

 

二、截取攝像頭都畫面並保存下來code

public class JavavcCameraTest
{
    static OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();

    public static void main(String[] args) throws Exception, InterruptedException
    {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();   //開始獲取攝像頭數據
        CanvasFrame canvas = new CanvasFrame("攝像頭");//新建一個窗口
        canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.setAlwaysOnTop(true);
        int ex = 0;
        while(true)
        {
            if(!canvas.isDisplayable())
            {//窗口是否關閉
                grabber.stop();//中止抓取
                System.exit(2);//退出
                break;
            }       
            canvas.showImage(grabber.grab());//獲取攝像頭圖像並放到窗口上顯示, 這裏的Frame frame=grabber.grab(); frame是一幀視頻圖像
            opencv_core.Mat mat = converter.convertToMat(grabber.grabFrame());
            ex++;
            opencv_imgcodecs.imwrite("d:\\img\\" + ex + ".png", mat);
            Thread.sleep(200);//50毫秒刷新一次圖像
        }
    }
}

grabber.grab()和grabber.grabFrame()均可以得到當時攝像頭的圖像,但都是Frame類型的圖像,想要保存須要先調用penCVFrameConverter.ToIplImage的convertToMat轉換爲mat類型,而後調用opencv_imgcodecs.imwrite()方法進行保存。視頻

效果:blog

 三、截取視頻的畫面圖片

public abstract class FrameGrabberKit {

    public static void main(String[] args) throws Exception {
        String inputFilePth = "C:\\Users\\Administrator\\Desktop\\a.mp4";
        randomGrabberFFmpegImage(inputFilePth, 5);
    }

    public static void randomGrabberFFmpegImage(String filePath, int randomSize)
            throws Exception {
        FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        ff.start();

        int ffLength = ff.getLengthInFrames();
        List<Integer> randomGrab = random(ffLength, randomSize);
        int maxRandomGrab = randomGrab.get(randomGrab.size() - 1);
        Frame f;
        int i = 0;
        while (i < ffLength) {
            f = ff.grabImage();
            if (randomGrab.contains(i)) {
                doExecuteFrame(f, i);
            }
            if (i >= maxRandomGrab) {
                break;
            }
            i++;
        }
        ff.stop();
    }

    public static void doExecuteFrame(Frame f, int index) {
        if (null == f || null == f.image) {
            return;
        }
        OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
        String imageMat = "png";
        String FileName = "D:\\img";
        opencv_core.Mat mat = converter.convertToMat(f);
        opencv_imgcodecs.imwrite("d:\\img\\" + index + "sls.png", mat);//存儲圖像
    }

    public static List<Integer> random(int baseNum, int length) {
        List<Integer> list = new ArrayList<>(length);
        while (list.size() < length) {
            Integer next = (int) (Math.random() * baseNum);
            if (list.contains(next)) {
                continue;
            }
            list.add(next);
        }
        Collections.sort(list);
        return list;
    }
}

inputFilePth是視頻的路徑。這裏隨機挑了5個畫面的圖像存儲下來。開發

效果:get

相關文章
相關標籤/搜索