c#链接摄像头 ,照相录像存储

2021-05-20 02:29

阅读:673

标签:NPU   created   buffer   录制   dispatch   mod   patch   tick   inpu   

using Camera.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace cameraone
{
    ///
    /// MainWindow.xaml 的交互逻辑
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            chushihua();
       
        }
        CameraCapture camera = new CameraCapture();
        CameraPTZ ptz = new CameraPTZ();
        VideoFileWriter video = new VideoFileWriter();
        DispatcherTimer timer = new DispatcherTimer();
        private void chushihua()
        {
            camera.OnFrameChanged += Camera_OnFrameChanged;//接收摄像头图像帧,打开摄像头,会触发接收每一帧字节数据。
            ptz.Port = 80;
            ptz.Host = "172.16.11.12";
            ptz.UserName = "admin";
            ptz.Password = "admin";
            ptz.Args = "/web/cgi-bin/hi3510/ptzctrl.cgi?-step=0&-act=[PTZ]";
        }
        private byte [] source = null;
        private void Camera_OnFrameChanged(object sender, byte[] buffer)
        {
            if(buffer!=null&&buffer.Length>0)
            {
                Application.Current.Dispatcher.Invoke(()=>
                    {
                       if(video.IsOpened)
                        {
                            video.Write(buffer);
                        }
                        source = buffer;
                        img.Source = tobitmap(buffer);
                });
            }
        }
        private BitmapImage tobitmap(byte [] data)  //转换
        {
            BitmapImage bmp = null;
            try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.StreamSource = new MemoryStream(data);
                bmp.EndInit();
                bmp.Freeze();
            }catch
            {
                return null;
            }
            return bmp;
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            camera.Open("rtsp://admin:admin@172.16.11.12/11");
   
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string file = AppDomain.CurrentDomain.BaseDirectory + "imga";
            if(!Directory.Exists(file))
            {
                Directory.CreateDirectory(file);
            }
            DateTime time = DateTime.Now;
            string name = file + "//" + time.Minute + "分" + time.Second + "秒" + ".jpg";
            FileStream fs = new FileStream(name,FileMode.Create);
            fs.Write(source,0,source.Length);
            fs.Close();

        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ptz.TurnRight();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
            timer.Start();
            string file = AppDomain.CurrentDomain.BaseDirectory + "video";
            if(!Directory.Exists(file))
            {
                Directory.CreateDirectory(file);
            }
            DateTime time = DateTime.Now;
            string name = file + "//" + time.Minute + "分" + ".avi";
            video.Open(name,640,480,15,VideoCodes.MPEG4);
           
        }
        int count = 0;
        private void Timer_Tick(object sender, EventArgs e)
        {
            if(count>5)
            {
                video.Close();
                MessageBox.Show("录制结束");
               
            }
            else
            {
                count++;
            }
        }
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            video.Close();
        }
    }
}

c#链接摄像头 ,照相录像存储

标签:NPU   created   buffer   录制   dispatch   mod   patch   tick   inpu   

原文地址:https://www.cnblogs.com/yufei66/p/11323011.html


评论


亲,登录后才可以留言!