| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4254 人关注过本帖, 2 人收藏
标题:摄像头编程类+类调用+例子
只看楼主 加入收藏
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏(2)
 问题点数:0 回复次数:17 
摄像头编程类+类调用+例子
using System;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;

namespace YHhotel.YHhotelWindows   //注意这里要改为自己的项目名
{
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class video
    {
        //5个传入参数
        private IntPtr myHand;
        private int myWidth;
        private short myHeight;
        private int myLeft;
        private int myTop;
        //调用avicap32.dll
        public struct videohdr_tag
        {
            public byte[] lpData;
            public int dwBufferLength;
            public int dwBytesUsed;
            public int dwTimeCaptured;
            public int dwUser;
            public int dwFlags;
            public int[] dwReserved;

        }
        [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)]   ref   string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
        [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)]   ref   string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)]   ref   string lpszVer, int cbVer);
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool DestroyWindow(int hndw);
        [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)]   object lParam);
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
        [DllImport("vfw32.dll")]
        public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
        [DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool capSetCallbackOnFrame(int hwnd, string s);
        //自定义参数:
        private int hHwnd;
        //构造函数
        public video(IntPtr myPtr, int left, int top, int width, short height)
        {
            myHand = myPtr;
            myLeft = left;
            myTop = top;
            myWidth = width;
            myHeight = height;
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        //打开视频:
        public void opVideo()
        {
            int intDevice = 0;
            string refDevice = intDevice.ToString();
            hHwnd = capCreateCaptureWindowA(ref   refDevice, 1342177280, 0, 0, 640, 480, myHand.ToInt32(), 0);
            if (SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
            {
                SendMessage(this.hHwnd, 0x435, -1, 0);
                SendMessage(this.hHwnd, 0x434, 0x42, 0);
                SendMessage(this.hHwnd, 0x432, -1, 0);
                SetWindowPos(this.hHwnd, 1, 0, 0, myWidth, Convert.ToInt32(myHeight), 6);
            }
            else
            {
                DestroyWindow(this.hHwnd);
            }
        }
        //停止视频
        public void CloVideo()
        {
            SendMessage(this.hHwnd, 0x40b, 0, 0);
            DestroyWindow(this.hHwnd);
        }
        //捕获视频
        public Image CatchVideo()
        {
            SendMessage(this.hHwnd, 0x41e, 0, 0);
            IDataObject obj1 = Clipboard.GetDataObject();
            Image getIma = null;
            if (obj1.GetDataPresent(typeof(Bitmap)))
            {
                Image image1 = (Image)obj1.GetData(typeof(Bitmap));
                getIma = image1;
            }
            return getIma;
        }
    }
}




以下是WINFORM调用代码

public video vd;//申明Video,最好是全局的Video变量


        private void Form1_Load(object sender, EventArgs e)
        {
            vd = new video(pictureBox1.Handle, this.pictureBox1.Left, this.pictureBox1.Top, this.pictureBox1.Width, (short)this.pictureBox1.Height);
            vd.opVideo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = vd.CatchVideo();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox2.Image =null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

[[it] 本帖最后由 hxhfg 于 2008-4-25 12:56 编辑 [/it]]

YHhotel.YHhotelWindows.rar (35.55 KB) 例子



09.GIF (41.29 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 摄像头 例子 
2008-04-25 10:00
shezhenhui1989
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:287
专家分:0
注 册:2008-2-1
收藏
得分:0 
谢谢了
2008-04-25 12:04
shezhenhui1989
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:287
专家分:0
注 册:2008-2-1
收藏
得分:0 
就是啊,有没有全的啊,谢
2008-04-25 12:04
drychq
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2007-10-30
收藏
得分:0 
呵呵..
  不错 啊 ..
2008-04-25 15:57
p645867491
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-4-26
收藏
得分:0 
绝对支持   学习
2008-04-26 13:19
depv
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-5
收藏
得分:0 
Thanks so much!
2008-08-05 17:28
蓝色鸟
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2008-5-21
收藏
得分:0 
不错,,,,,,,,

JAVA技术交流群:55180211  欢迎加盟
一起学习,一起成长
2008-08-05 23:16
seiya027848
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2008-6-27
收藏
得分:0 
有长知识了
2008-08-06 09:15
jetlee2008
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-10
收藏
得分:0 
搂住能留下qq吗,我最近也在做这个
2008-08-09 15:04
TianQi
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2008-12-5
收藏
得分:0 
谢谢咯。已经下载学习。
2009-07-23 15:11
快速回复:摄像头编程类+类调用+例子
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020319 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved