发个小程序希望有人需要(操作摄像头)

271 篇文章 2 订阅
215 篇文章 2 订阅

作者:hhc123

转自:http://bbs.csdn.net/topics/320216425


C# code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
using  System.Runtime.InteropServices;
 
namespace  WindowsFormsApplication24
{
     public  partial  class  Form1 : Form
     {
         public  Form1()
         {
             InitializeComponent();
         }
       
         const  short  WM_CAP = 1024;
         const  int  WM_CAP_DRIVER_CONNECT = WM_CAP + 10;
         const  int  WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11;
         const  int  WM_CAP_EDIT_COPY = WM_CAP + 30;
         const  int  WM_CAP_SET_PREVIEW = WM_CAP + 50;
         const  int  WM_CAP_SET_PREVIEWRATE = WM_CAP + 52;
         const  int  WM_CAP_SET_SCALE = WM_CAP + 53;
         const  int  WS_CHILD = 1073741824;
         const  int  WS_VISIBLE = 268435456;
         const  short  SWP_NOMOVE = 2;
         const  short  SWP_NOSIZE = 1;
         const  short  SWP_NOZORDER = 4;
         const  short  HWND_BOTTOM = 1;
         int  iDevice = 0;
         int  hHwnd;
         [System.Runtime.InteropServices.DllImport( "user32" , EntryPoint =  "SendMessageA" )]
         static  extern  int  SendMessage( int  hwnd,  int  wMsg,  int  wParam, [MarshalAs(UnmanagedType.AsAny)] 
             object  lParam);
         [System.Runtime.InteropServices.DllImport( "user32" , EntryPoint =  "SetWindowPos" )]
         static  extern  int  SetWindowPos( int  hwnd,  int  hWndInsertAfter,  int  x,  int  y,  int  cx,  int  cy,  int  wFlags);
         [System.Runtime.InteropServices.DllImport( "user32" )]
         static  extern  bool  DestroyWindow( int  hndw);
         [System.Runtime.InteropServices.DllImport( "avicap32.dll" )]
         static  extern  int  capCreateCaptureWindowA( string  lpszWindowName,  int  dwStyle,  int  x,  int  y,  int  nWidth,  short  nHeight,  int  hWndParent,  int  nID);
         [System.Runtime.InteropServices.DllImport( "avicap32.dll" )]
         static  extern  bool  capGetDriverDescriptionA( short  wDriver,  string  lpszName,  int  cbName,  string  lpszVer,  int  cbVer);
         private  void  OpenPreviewWindow()
         {
             int  iHeight = 320;
             int  iWidth = 200;
             // 
             //  Open Preview window in picturebox
             // 
             hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0);
             // 
             //  Connect to device
             // 
             if  (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1)
             {
                 // 
                 // Set the preview scale
                 // 
                 SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
                 // 
                 // Set the preview rate in milliseconds
                 // 
                 SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
                 // 
                 // Start previewing the image from the camera
                 // 
                 SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
                 // 
                 //  Resize window to fit in picturebox
                 // 
                 SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, (SWP_NOMOVE | SWP_NOZORDER));
             }
             else
             {
                 // 
                 //  Error connecting to device close window
                 //  
                 DestroyWindow(hHwnd);
             }
         }
         private  void  ClosePreviewWindow()
         {
             // 
             //  Disconnect from device
             // 
             SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
             // 
             //  close window
             // 
             DestroyWindow(hHwnd);
         }
      
 
         private  void  button1_Click( object  sender, EventArgs e)
         {
             iDevice =  int .Parse(device_number_textBox.Text);
             OpenPreviewWindow();
         }
     }
}

picCapture为一个PictureBox
device_number_textBox.Text=0为一个TextBox,值为0


C# code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  TcpClient myclient;
         MemoryStream ms;
         NetworkStream myns;
         BinaryWriter mysw;
         Thread myth;
         TcpListener mytcpl;
         Socket mysocket;
         NetworkStream ns;
 
         private  void  Start_Receiving_Video_Conference()
         {
             try
             {
 
                 // Open The Port
                 mytcpl =  new  TcpListener(5000);
                 mytcpl.Start();                          // Start Listening on That Port
                 mysocket = mytcpl.AcceptSocket();          // Accept Any Request From Client and Start a Session
                 ns =  new  NetworkStream(mysocket);      // Receives The Binary Data From Port
 
                 picture_comming.Image = Image.FromStream(ns);
                 mytcpl.Stop();                              // Close TCP Session
 
                 if  (mysocket.Connected ==  true )              // Looping While Connected to Receive Another Message 
                 {
                     while  ( true )
                     {
                         Start_Receiving_Video_Conference();                  // Back to First Method
                     }
                 }
                 myns.Flush();
 
             }
             catch  (Exception) { button1.Enabled =  true ; myth.Abort(); }
         }
         
         private  void  Start_Sending_Video_Conference( string  remote_IP,  int  port_number)
         {
             try
             {
 
                 ms =  new  MemoryStream(); // Store it in Binary Array as Stream
 
 
                 IDataObject data;
                 Image bmap;
 
                 //  Copy image to clipboard
                 SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);
 
                 //  Get image from clipboard and convert it to a bitmap
                 data = Clipboard.GetDataObject();
 
                 if  (data.GetDataPresent( typeof (System.Drawing.Bitmap)))
                 {
                     bmap = ((Image)(data.GetData( typeof (System.Drawing.Bitmap))));
                     bmap.Save(ms, ImageFormat.Bmp);
                 }
 
                 
                 picCapture.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte [] arrImage = ms.GetBuffer();
                 myclient =  new  TcpClient(remote_IP, port_number); //Connecting with server
                 myns = myclient.GetStream();
                 mysw =  new  BinaryWriter(myns);
                 mysw.Write(arrImage); //send the stream to above address
                 ms.Flush();
                 mysw.Flush();
                 myns.Flush();
                 ms.Close();
                 mysw.Close();
                 myns.Close();
                 myclient.Close();
             }
             catch  (Exception ex)
             {
                 MessageBox.Show(ex.Message,  "Video Conference Error Message" , MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }


C# code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
private  void  btnStart_Click( object  sender, System.EventArgs e)
         {
             iDevice =  int .Parse (device_number_textBox.Text);
             OpenPreviewWindow();
         }
         private  void  btnStop_Click( object  sender, System.EventArgs e)
         {
             ClosePreviewWindow();
 
         }
         private  void  Form1_Closing( object  sender, System.ComponentModel.CancelEventArgs e)
         {
             if  (btnStop.Enabled) 
             {
                 ClosePreviewWindow();
             }
 
         }
 
         private  void  button1_Click( object  sender, EventArgs e)
         {
             button1.Enabled =  false ;
             myth =  new  Thread( new  System.Threading.ThreadStart(Start_Receiving_Video_Conference));  // Start Thread Session
             myth.Start();  // Start Receiveing Camera
         }
 
         private  void  timer1_Tick( object  sender, EventArgs e)
         {
             Start_Sending_Video_Conference(IP_textBox.Text,6000);
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
 
         }
 
         private  void  Form1_FormClosing( object  sender, FormClosingEventArgs e)
         {
             try
             {
                 mytcpl.Stop();
                 myth.Abort();
             }
             catch  (Exception){}
         }
 
         private  void  button2_Click( object  sender, EventArgs e)
         {
             timer1.Enabled =  true ;
         }
     }


谢谢各位光临啊,我好久没有来csdn了。竟然推荐;
不过这个技术落后了。现在用DirectShow了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值