首页 新闻 会员 周边 捐助

摄像头程序总是提示

0
悬赏园豆:20 [待解决问题]

做了一个摄像头程序,

 上面两个按钮,一个是启动的,一个是关闭的 轮流点击这两个按钮是没有任何问题的
可是执行完一次 后 关闭程序 ,然后再一次执行,就总是弹出 这个
提示
我是使用c#调用vfw
vfw
demps_c的主页 demps_c | 初学一级 | 园豆:128
提问于:2015-10-30 15:12
< >
分享
所有回答(1)
0

网上找的帮助类

public class VideoAPI
    {
        //视频调用API
        [DllImport("avicap32.dll")]
        public static extern IntPtr capCreateCaptureWindow(byte[] strWindowName, int dwStyle, int x, int y, int width, int height, IntPtr hwdParent, int nID);

        [DllImport("avicap32.dll")]
        public static extern bool capGetDriverDescription(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);

        [DllImport("User32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);

        [DllImport("User32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

        [DllImport("User32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, string lParam);

        [DllImport("Kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);

        //常量
        public const int WM_USER = 0x400; 
        public const int WS_CHILD = 0x40000000; 
        public const int WS_VISIBLE = 0x10000000; 
        public const int SWP_NOMOVE = 0x2; 
        public const int SWP_NOZORDER = 0x4;  
        public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; 
        public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; 
        public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5; 
        public const int WM_CAP_SET_PREVIEW = WM_USER + 50; 
        public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; 
        public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45; 
        public const int WM_CAP_START = WM_USER;  
        public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
        public const int WM_CAP_SET_SCALE = WM_USER + 53;
        public const int WM_COPYTOCLIPBORAD = WM_USER + 30;
        public const int WM_CAP_SEQUENCE = WM_USER + 62;
        public const int WM_CAP_FILE_SET_CAPTURE_FILE = WM_USER + 20;
        public const int WM_CAP_STOP = WM_USER + 68;

    }

    public class VideoClass
    {
        private IntPtr caphwnd;
        private IntPtr controlhwnd;
        private int height;
        private int width;
        private bool isstart = false;

        public VideoClass()
        {
            
        }

        public void Initialize(IntPtr controlhwnd, int width, int height)
        {
            this.controlhwnd = controlhwnd;
            this.width = width;
            this.height = height;
        }

        ~VideoClass()
        {
            if (isstart)
            {
                this.StopWebcam();
            }
        }

        /// <summary>
        /// 检查是否有视频设备
        /// </summary>
        /// <returns></returns>
        public bool CheckWebcamExist()
        {
            byte[] lpszName = new byte[100];
            byte[] lpszVer = new byte[100];
            VideoAPI.capGetDriverDescription(0, lpszName, 100, lpszVer, 100);

            caphwnd = VideoAPI.capCreateCaptureWindow(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, this.width, this.height, this.controlhwnd, 1);

            if (caphwnd == null)
            {
                return false;
            }

            int index = 0;
            bool isconnect = VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
            while (!isconnect)
            {
                isconnect = VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
                if (++index <= 10)
                    continue;
            }
            if (isconnect == false)
            {
                VideoAPI.CloseHandle(caphwnd);
                return false;
            }
            else
            {
                VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
                return true;
            }
        }

        /// <summary>
        /// 启动视频设备
        /// </summary>
        /// <returns></returns>
        public bool StartWebcam()
        {
            byte[] lpszName = new byte[100];
            byte[] lpszVer = new byte[100];
            VideoAPI.capGetDriverDescription(0, lpszName, 100, lpszVer, 100);

            caphwnd = VideoAPI.capCreateCaptureWindow(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, this.width, this.height, this.controlhwnd, 1);

            if (caphwnd == null)
            {
                return false;
            }

            //解决在Win7系统下摄像头获取不到问题
            int index=0;
            bool isconnect = VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
            while (!isconnect)
            {
                isconnect = VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
                if (++index <= 10)
                    continue;
            }
            if(isconnect == false)
            {
                VideoAPI.CloseHandle(caphwnd);
                return false;
            }

            if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEWRATE, 66, 0) == false)
            {
                return false;
            }

            if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEW, true, 0) == false)
            {
                return false;
            }

            if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_SCALE, 1, 0) == false)
            {
                return false;
            }

            isstart = true;
            return true;
        }

        /// <summary>
        /// 关闭视频设备
        /// </summary>
        /// <returns></returns>
        public bool StopWebcam()
        {
            if (caphwnd != null)
            {
                isstart = false;
                return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
            }
            else
                return false;
        }

        public bool GrabImage(IntPtr hWndC, string path)
        {
            if (caphwnd != null)
                return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SAVEDIB, 0, path);
            else
                return false;
        }

        public bool SaveImage(string path)
        {
            return GrabImage(this.caphwnd, path);
        }

        public void CopyToClipBorad()
        {
            VideoAPI.SendMessage(caphwnd, VideoAPI.WM_COPYTOCLIPBORAD, 0, 0);
        }

        public System.Drawing.Image getCaptureImage()
        {
            System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
            System.Drawing.Image retImage = null;
            if (iData != null)
            {
                if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
                {
                    retImage = (System.Drawing.Image)iData.GetData(System.Windows.Forms.DataFormats.Bitmap);
                }
                else if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Dib))
                {
                    retImage = (System.Drawing.Image)iData.GetData(System.Windows.Forms.DataFormats.Dib);
                }
            }
            return retImage;
        }

        public bool StartRecordVideo(string path)
        {
            VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_FILE_SET_CAPTURE_FILE, 0, path);
            VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SEQUENCE, 0, 0);
            return true;
        }

        public bool StopRecordVideo()
        {
            return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_STOP, 0, 0);
        }

    }

初始化:

vc.Initialize(videobox.Handle, videobox.Width, videobox.Height);

启动摄像头:

vc.StartWebcam();

关闭摄像头:

vc.StopWebcam();

jello chen | 园豆:7336 (大侠五级) | 2015-10-30 17:33

多谢了,不过原来的那个问题还是存在啊

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 18:06

@demps_c: 我在win8 64位上测试正常,可以换台机器试试

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 21:12

@jello chen: 你关掉然后运行程序没有出现上面的那个图片吗???

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:01

@demps_c: 没有,可以试下我这个,http://files.cnblogs.com/files/jellochen/Debug.rar

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 23:09

@jello chen:我的电脑是net 4.0你的程序是4.5

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:13

@demps_c: 配置文件(不是vshost这个)中改下这个<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />再运行

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 23:18

@jello chen: 在哪改,我不知道

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:17

@demps_c: 在ThreadDemo.exe.config中,后面sku的Version改成4.0,再运行程序

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 23:20

@jello chen: 是能运行,可是如果你关掉程序,然后马上运行还是会出现那个图片提示

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:24

@demps_c: 驱动ok么

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 23:32

@jello chen: ok这个是我的笔记本摄像头,都没有任何问题

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:32

@demps_c: 在设备管理器里面把摄像头禁用,然后再启用试下

支持(0) 反对(0) jello chen | 园豆:7336 (大侠五级) | 2015-10-30 23:44

@jello chen: 肯定不是这个问题,刚刚写了另外一个程序,成功了。

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2015-10-30 23:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册