C# 使用摄像头拍照 支持Win7 64位

简介: 原文:C# 使用摄像头拍照 支持Win7 64位So, how do we capture an image from a WebCam? Once you download the source code that is attached to the article you should h...
原文: C# 使用摄像头拍照 支持Win7 64位

So, how do we capture an image from a WebCam?

Once you download the source code that is attached to the article you should have the following three projects:

  • Demo – simple Windows Forms project that demonstrates how a WebCam is used. It references WebCamWrapper which in turn references WebCamLib.
  • WebCamLib – this is where the magic is happening – it is a C++ project with just two files (WebCamLib.h andWebCamLib.cpp) that queries a WebCam using DirectShow and returns results.
  • WebCamWrapper – a C# wrapper on top of the C++ project that enables easy integration into the .NET world.

For a starting point I recommend a code view of Demo\MainForm.cs. This form implements most of the operations you can think of when it comes to WebCam access. First is the iteration through the WebCams hooked up to the computer:

private void MainForm_Load(object sender, EventArgs e)
{
    if (!DesignMode)
    {
        comboBoxCameras.Items.Clear();
        foreach (Camera cam in CameraService.AvailableCameras)
            comboBoxCameras.Items.Add(cam);

        if (comboBoxCameras.Items.Count > 0)
            comboBoxCameras.SelectedIndex = 0;
    }
}

The CameraService class you see in the code is contained in the WebCamWrapper project and is the main wrapper over the main class CameraMethods that is the only class implemented in the C++ WebCamLib project.CameraService exposes AvailableCameras as a list of Camera classes that contain the logic for a certain WebCam. Once the user makes a choice of camera, you’ll obviously want to start the capture:

private CameraFrameSource _frameSource;
private static Bitmap _latestFrame;

private void btnStart_Click(object sender, EventArgs e)
{
    if (_frameSource != null && _frameSource.Camera == comboBoxCameras.SelectedItem)
        return;

    thrashOldCamera();
    startCapturing();
}

  

_frameSource is the variable in which we’ll save the currently selected Camera. Touchless developers decided not to tie their capture source exclusively to WebCam (good choice obviously) so they made a generic IFrameSourceinterface that CameraFrameSource implements… and that’s how this class ended up as a container instead of theCamera class directly. The rest of the code is pretty self-explanatory – if we select the same frame source, we’ll just exit; if not we will thrash the old camera and start a new one. Onto the startCapturing method:

private void startCapturing()
{
    try
    {
        Camera c = (Camera)comboBoxCameras.SelectedItem;
        setFrameSource(new CameraFrameSource(c));
        _frameSource.Camera.CaptureWidth = 320;
        _frameSource.Camera.CaptureHeight = 240;
        _frameSource.Camera.Fps = 20;
        _frameSource.NewFrame += OnImageCaptured;

        pictureBoxDisplay.Paint += new PaintEventHandler(drawLatestImage);
        _frameSource.StartFrameCapture();
    }
    catch (Exception ex)
    {
        comboBoxCameras.Text = "Select A Camera";
        MessageBox.Show(ex.Message);
    }
}

private void setFrameSource(CameraFrameSource cameraFrameSource)
{
    if (_frameSource == cameraFrameSource)
        return;

    _frameSource = cameraFrameSource;
}

private void drawLatestImage(object sender, PaintEventArgs e)
{
    if (_latestFrame != null)
    {
        e.Graphics.DrawImage(_latestFrame, 0, 0, _latestFrame.Width, _latestFrame.Height);
    }
}

public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, 
                            Touchless.Vision.Contracts.Frame frame, double fps)
{
    _latestFrame = frame.Image;
    pictureBoxDisplay.Invalidate();
}

  

We start off by fetching the selected Camera from the ComboBox which we then use to create and set theCameraFrameSource. Lines after that influence the capture parameters (be sure to remember these three lines as we will be getting back to them later) and after that we have a subscription to two events.

The first event, NewFrame, is raised whenever WebCamLib captures an image from the WebCam. As you can see, we save that image into a local variable _latestFrame and from there you can do any additional image processing you like. The second event is just a fancy (and more efficient) way of saying pictureBoxDisplay.Image = frame.Image. For some reason, setting the Image property on a PictureBox too often causes flicker and we obviously do not want that – instead we resort to invalidating the PictureBox and then handling its paint event to draw the current image from the WebCam.

Now that all that is implemented, we just StartFrameCapture and enjoy the view from our WebCam. Try it out – press F5 and then click the ‘Start’ button once the Form loads up.

Rent is too damn high

When you grow tired of watching yourself, simply close the form. Once you are back in Visual Studio, check out thethrashOldCamera method (that is utilized from the Form_Closing and btnStop_Click methods also):

private void thrashOldCamera()
{
    if (_frameSource != null)
    {
        _frameSource.NewFrame -= OnImageCaptured;
        _frameSource.Camera.Dispose();
        setFrameSource(null);
        pictureBoxDisplay.Paint -= new PaintEventHandler(drawLatestImage);
    }
}

  

Well, nothing too fancy – we unsubscribe from the two mentioned events, set the _frameSource variable to null, and call Dispose on Camera so that the C++ WebCamLib can perform cleanup operations.

Believe it or not – that’s it. There is nothing more critical to explain or implement in order to use images from your WebCam in C#. The extra code that exists in MainForm.cs is just there for saving the current image:

private void btnSave_Click(object sender, EventArgs e)
{
    if (_frameSource == null)
        return;

    Bitmap current = (Bitmap)_latestFrame.Clone();
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "*.bmp|*.bmp";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            current.Save(sfd.FileName);
        }
    }

    current.Dispose();
}

  

And bringing up the configuration dialog:

private void btnConfig_Click(object sender, EventArgs e)
{
    // snap camera
    if (_frameSource != null)
        _frameSource.Camera.ShowPropertiesDialog();
}

  

Configuration Dialog

Problem(s)

As you can see from the code – the implementation is pretty easy and clean (unlike some other approaches that use WIA, obscure DLLs, clipboard, or hard disk for saving images, etc.), meaning that there are not many problems. Actually, currently there is only one problem I can identify. You remember these three lines?

_frameSource.Camera.CaptureWidth = 320;
_frameSource.Camera.CaptureHeight = 240;
_frameSource.Camera.Fps = 20;

Well, it turns out that they are not working as advertised. First, let’s talk about FPS. If we dive into the Camera class (line 254) here is what we will see (the method that gets called after an image is captured from the webcam):

Even if you just glanced at the method, you probably saw that most of it is dedicated to calculating the time between frames and ditching the frame if it came too soon. Which is not too bad, I guess – controlling the frame rate on C# level rather than on hardware level will probably not kill you.

But what about finding out the other two lines, which influence the size of the captured image, also not working (line 235 in Camera.cs)?

As you can see, the image size is actually faked. Majority of cameras I’ve tested out will tend to return images in the 640x480 size. Which is fine in most cases – if you need a smaller image, b.GetThumbnailImage will allow you to easily resize it. However, if you wish a higher resolution image, you are stuck and that’s not a good thing.

So, anyone from the C++ world is more than welcome to help with this. The following links I’ve read gave me the impression that all that’s needed to be done is somehow invoke the Video Format window in C++, the same way we are now invoking the Video Source Configuration window (for setting Brightness, Contracts, etc):

    • Setting up Webcam properties with DirectShow forum post
    • EasyWebCam project – for most part it is bad, but it does have a Video Format window. I decompiledWebCam_Capture.dll from the project only to find out that everything is implemented using PInvoke - meaning that it’s useless for our approach. So, if somebody can bring up that same window using C++ and DirectShow – please help out by extending the existing CameraMethods class.

Video Format Dialog

 

 

源码下载地址: Source

 

目录
相关文章
|
人工智能 自然语言处理 安全
从 ChatGPT 到 AI 大模型私有化部署,为什么企业需要私有化专属大模型?
目前,大模型已经能够切实的影响到我们每个人的工作、学习、生活,赋能千行万业,但是开放的大模型却无法很好的适应企业或单位的内部需要,为此,此处研究并提出为什么企业需要私有化大模型,并探讨私有化大模型的优势和挑战,同时本文也举出了一些实践落地的例子,希望能给读者带来一些思考和启发。
|
Java
filebeat占用文件句柄磁盘满
filebeat作为日志采集客户端,相比较于java编写的fluent,有着低功耗的特性。但在一些极端情况下,忽视filebeat的一些特性配置,可能会带来灾难。之前发过一篇关于filebeat内存占用的案例和分析,今天再说下filebeat占用文件句柄、耗费主机磁盘甚至导致磁盘满的案例。
9471 0
|
11月前
|
存储 前端开发 数据处理
ArkTS 常用状态管理:深入理解与实践
在HarmonyOS应用开发中,ArkTS的状态管理机制是构建响应式应用的核心。本文详细介绍了ArkTS中的状态管理,包括@State、@Prop、@Link、@Provide和@Consume等装饰器的使用及其在实际开发中的应用和最佳实践。通过这些装饰器,开发者可以实现组件内状态管理、父子组件单向和双向同步、跨组件层级状态同步等功能,从而提高应用的可维护性、可扩展性和性能。
629 1
|
编解码 边缘计算 vr&ar
未来已来:MaxFrame引领下的下一代沉浸式视觉体验探索
【10月更文挑战第12天】随着技术的不断进步,视频消费模式正在经历一场革命性的变革。从高清到超高清,再到如今的沉浸式体验,每一次技术革新都为用户带来了前所未有的视听享受。在这一进程中,MaxFrame技术以其独特的图像处理能力,成为了推动下一代沉浸式视觉体验的关键力量。本文将深入探讨MaxFrame技术的发展现状、未来趋势以及其在虚拟现实(VR)、增强现实(AR)等新兴领域的应用潜力,并分析行业面临的挑战及解决方案。
227 1
|
前端开发
HTML+CSS动画实现动感3D卡片墙:现代Web设计的视觉盛宴
HTML+CSS动画实现动感3D卡片墙:现代Web设计的视觉盛宴
|
存储 Java 内存技术
USB-C与TYPE-C接口的区别与应用
USB-C与TYPE-C接口的区别与应用
|
Shell C++
【C++ 实用函数 】C++ 14 std::exchange 使用说明
【C++ 实用函数 】C++ 14 std::exchange 使用说明
398 1
|
缓存 网络协议 安全
什么是防火墙?详解三种常见的防火墙及各自的优缺点
什么是防火墙?详解三种常见的防火墙及各自的优缺点
1535 2
|
数据安全/隐私保护 计算机视觉
推荐五款实用的良心软件,无广告无弹窗
分享是一种神奇的东西,它使快乐增大,它使悲伤减小。
350 0
推荐五款实用的良心软件,无广告无弹窗