c # link cameras, camera video storage

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
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            chushihua();
       
        }
        Camera = new new CameraCapture CameraCapture ();
        CameraPTZ new new CameraPTZ PTZ = ();
        VideoFileWriter new new VideoFileWriter Video = ();
        DispatcherTimer new new DispatcherTimer = Timer ();
        Private void chushihua ()
        {
            camera.OnFrameChanged + = Camera_OnFrameChanged; // receiving camera images frame, open the camera is triggered every frame received byte of data.
            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:[email protected]/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();
        }
    }
}

Guess you like

Origin www.cnblogs.com/yufei66/p/11323011.html