苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3957|回复: 3

[其他] C#关于摄像头的编程

[复制链接]
发表于 2013-10-10 23:36:08 | 显示全部楼层 |阅读模式
1.调用摄像头捕捉画面,要求彩色和黑白画面分别呈现
2.可以随时截取摄像头画面(彩色和黑白)并保存到指定路径
3.可以预览截图(彩色和黑白),如果不满意可以选择删除截图


求C#高手帮忙!!!



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-10-11 08:12:13 | 显示全部楼层
你用的什么摄像头,直接找个驱动调用驱动最好,那样实现起来比较方便
可以直接使用用directshow

c#摄像头拍照的例子
[code=csharp]

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Video
{
/// <summary>
/// 一个控制摄像头的类
/// </summary>
public class VideoWork
{
private const int WM_USER = 0x400;
private const int WS_CHILD = 0x40000000;
private const int WS_VISIBLE = 0x10000000;
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_STOP = WM_CAP_START + 68;
private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;
private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;
private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;
private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;
private IntPtr hWndC;
private bool bWorkStart = false;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;
private int mLeft;
private int mTop;

/// <summary>
/// 初始化显示图像
/// </summary>
/// <param name="handle">控件的句柄</param>
/// <param name="left">开始显示的左边距</param>
/// <param name="top">开始显示的上边距</param>
/// <param name="width">要显示的宽度</param>
/// <param name="height">要显示的长度</param>
public VideoWork(IntPtr handle, int left, int top, int width,int height)
{
mControlPtr = handle;
mWidth = width;
mHeight = height;
mLeft = left;
mTop = top;
}
[DllImport("avicap32.dll")]
private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );
[DllImport("User32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);
/// <summary>
/// 开始显示图像
/// </summary>
public void Start()
{
if (bWorkStart)
return;
bWorkStart = true;
byte[] lpszName = new byte[100];
hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);
if (hWndC.ToInt32() != 0)
{
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
//Global.log.Write("SendMessage");
}
return;
}
/// <summary>
/// 停止显示
/// </summary>
public void Stop()
{
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
bWorkStart = false;
}
/// <summary>
/// 抓图
/// </summary>
/// <param name="path">要保存bmp文件的路径</param>
public void GrabImage(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());
}
}
}

这是一个控制摄像头进行拍照的类,我每次使用GrabImage抓图都是225K的一张照片,我想请问如何才能让我抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小?

if(this.Request.QueryString["filename"]!=null)
{
                //获取原图片
string filename=this.Request.QueryString["filename"];
Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename));
    //计算缩小比例
double d1;
if(bmpOld.Height>bmpOld.Width)
d1=(double)(MaxLength/(double)bmpOld.Width);
else
d1=(double)(MaxLength/(double)bmpOld.Height);
//产生缩图
Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1));
//清除缓冲
Response.Clear();
//生成图片
bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg);
Response.End();
//释放资源
bmpThumb.Dispose();
bmpOld.Dispose();
}[/code]
发表于 2013-10-11 08:12:41 | 显示全部楼层
或者你也可以直接使用Dll的方法
[code=csharp]using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsApplication2
{
    //视频API调用
    public class VideoAPI
    {
        [System.Runtime.InteropServices.DllImport("avicap32.dll")]
        public static extern IntPtr capCreatCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int Width,
            int nHeight, IntPtr hWndParent, int nID);
        [System.Runtime.InteropServices.DllImport("avicap32.dll")]
        public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
        [System.Runtime.InteropServices.DllImport("avicap32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
        [System.Runtime.InteropServices.DllImport("avicap32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

        // 常量
        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_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 void cla()
        {
        }
    }

    public class cVideo
    {
        private IntPtr lwndC;  //保存无符号句柄
        private IntPtr mControlPtr;  //保存管理指示器
        private int mWidth;
        private int mHeigth;
        public cVideo(IntPtr handle, int width, int heigth)
        {
            mControlPtr = handle;
            mWidth = width;
            mHeigth = heigth;
        }

        /// <summary>
        /// 打开视频设备
        /// </summary>
        public void StartWebCam()
        {
            byte[] lpszName = new byte[100];
            byte[] lpszVer = new byte[100];
            VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
            this.lwndC = VideoAPI.capCreatCaptureWindowA(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0,
                mWidth, mHeigth, mControlPtr, 0);
            if (VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
            {
                VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
                VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);
            }
        }
        /// <summary>
        /// 关闭视频设备
        /// </summary>
        public void CloseWebcam()
        {
            VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0);
        }

        /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="path">要保存bmp文件的路径</param>
        public void GrabImage(IntPtr hWndC, string path)
        {
            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
            VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
        }
    }

}[/code]
 楼主| 发表于 2013-10-11 12:00:18 | 显示全部楼层
站长苏飞 发表于 2013-10-11 08:12
或者你也可以直接使用Dll的方法

谢谢!!!
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-12-28 12:29

© 2014-2021

快速回复 返回顶部 返回列表