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

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();
       
        }
        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();
        }
    }
}

转载于:https://www.cnblogs.com/yufei66/p/11323011.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值