zoukankan      html  css  js  c++  java
  • 树莓派简单摄像头录像并保存视频文件

    一、简介

      本文讲使用OpenCV,不使用FFMPEG的方法进行保存视频。

    二、代码

      1、引用

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="OpenCvSharp3-AnyCPU" version="3.4.1.20180830" targetFramework="net47" />
    </packages>

      2、代码

    using OpenCvSharp;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace OpenCVDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var cameraIndex = 0;
                if (args != null && args.Length > 0)
                {
                    int.TryParse(args[0], out cameraIndex);
                }
                Console.WriteLine("Begin At Cam Index " + cameraIndex + " ...");
                var path = Path.Combine(AppContext.BaseDirectory, "imgs");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                int count = 0;
                var videoSize = new Size(640, 480);
                var timePoint = new Point(0, 15);
                VideoWriter videoWriter = new VideoWriter(Path.Combine(path, "Video.mp4"), FourCC.XVID, 30,
                       videoSize, true);
                using (var camera = VideoCapture.FromCamera(CaptureDevice.Any))
                {
                    //VideoWriter videoWriter = new VideoWriter(Path.Combine(path, "Video.mp4"), FourCC.Prompt, 30,
                    //new Size(camera.Get(CaptureProperty.FrameWidth), camera.Get(CaptureProperty.FrameHeight)));
                    while (true)
                    {
                        var mat = camera.RetrieveMat();
                        if (mat.Empty()) { Thread.Sleep(100); continue; }
                        if (++count > 30 * 30) break;
                        Console.WriteLine("save .. " + count);
                        //mat.PutText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new Point(10, 10), HersheyFonts.HersheyPlain, 20, Scalar.Yellow, 2);
                        Cv2.PutText(mat, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " GTM+8", timePoint, HersheyFonts.HersheyPlain, 1, Scalar.Red, 1);
                        //OpenCvSharp.Cv2.ImShow("myWin", mat);
                        //if (Cv2.WaitKey(40) == 13)
                        //{
                        //    break;
                        //}
                        Thread.Sleep(50);
                        var newmat = mat.Resize(videoSize);
                        videoWriter.Write(newmat);
                        mat.Dispose();
                        newmat.Dispose();
                    }
                    videoWriter.Dispose();
                    //GC.Collect();
                }
                Console.WriteLine("Finish.");
            }
        }
    }
  • 相关阅读:
    sql server 2005中的DDL触发器
    SQL中两台服务器间使用连接服务器(ZT)
    [学习摘录]读和写,关于cache和buffer
    [摘录备忘]Shell基础
    [问题解决]bash: ifconfig: command not found
    [收藏学习]openstack脚本安装经验借鉴
    [常用收藏]电脑常用的密码破解法及其备忘录
    齐唐创业日记:毕业七年从打工到创办网站(转)
    Oracle中的to_date()函数
    上海五年的奋斗历程 从月薪3500到700万(转)
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/9690950.html
Copyright © 2011-2022 走看看