使用 WIA 在 C# 中使用网络摄像头自动拍照

发布于 2024-10-05 19:00:28 字数 984 浏览 5 评论 0原文

我正在使用 WIALib 访问我的网络摄像头。我正在开发的代码非常简单:按下按钮时,会拍摄网络摄像头图片,然后显示在图片框中。

我已经可以使用网络摄像头拍照,但尚未完全自动化。我发现检索网络摄像头拍摄的图片的唯一方法是使用以下方法:

wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) as CollectionClass;

但这要求用户选择图片。我总是想要拍最后一张照片。所以我尝试这样:

string imageFileName = Path.GetTempFileName(); // create temporary file for image

wiaItem = wiaRoot.TakePicture(); // take a picture

Cursor.Current = Cursors.WaitCursor; // could take some time

this.Refresh();

wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file

pictureBox1.Image = Image.FromFile(imageFileName); // create Image instance from file

Marshal.ReleaseComObject(wiaItem);

但是方法 TakePicture() 返回 null,所以我无法传输图像。最奇怪的是,照片确实是在调用 TakePicture() 方法之后拍摄的,因为如果我手动转到网络摄像头,照片就在那里!我只是不明白为什么它不返回值。

总而言之,我需要以下两者之一: 1. 让 TakePicture() 工作,返回一个我可以使用的值。 2. 自动访问网络摄像头的图片列表,这样我就可以检索最后拍摄的照片。

谨致问候并感谢您的帮助,迈克尔。

I'm using WIALib to access my webcam. The code I'm developing is pretty simple: when a button is pressed a webcam picture is taken, and then displayed in a picture box.

I can already take pictures with my webcam, but it isn't yet fully automated. The only way I found to retrieve the pictures taken by the webcam, is using this:

wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) as CollectionClass;

But this asks the user to select the picture. And I always want the last picture taken. So I'm trying this way:

string imageFileName = Path.GetTempFileName(); // create temporary file for image

wiaItem = wiaRoot.TakePicture(); // take a picture

Cursor.Current = Cursors.WaitCursor; // could take some time

this.Refresh();

wiaItem.Transfer(imageFileName, false); // transfer picture to our temporary file

pictureBox1.Image = Image.FromFile(imageFileName); // create Image instance from file

Marshal.ReleaseComObject(wiaItem);

But the method TakePicture() returns null, and so I can't transfer the image. The strangest thing is that the picture was really taken after the method TakePicture() was called, since if I go to the webcam manually the picture is there! I just don't get it why it doesn't return a value.

To summarize, I either need one of this two:
1. Get TakePicture() to work, returning a value I can use.
2. Access the list of the webcam's pictures automatically, so I can retrieve the last picture taken.

Best regards and thanks for the help, Micael.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-10-12 19:00:28

据我所知, wiaItem = wiaRoot.TakePicture() 走上了错误的道路。试试这个:

string imageFileName;
wiaRoot.TakePicture( out takenFileName);
pictureBox1.Image = Image.FromFile(imageFileName);

TakePicture 将图片保存到文件,并返回新文件的名称作为输出参数。

根据您的评论编辑 - 您使用的是“Windows 7 版本”的 WiaLib 吗?如果是这样,请尝试如下操作:

var manager = new DeviceManagerClass();
Item wiaItem;
Device device = null;
foreach (var info in manager.DeviceInfos)
{
    if (info.DeviceID == DESIRED_DEVICE_ID)
    {
        device = info.Connect();
        wiaItem = device.ExecuteCommand(CommandID.wiaCommandTakePicture);
    }
}

将 ExecuteCommand 与 众所周知的 guid (也从 COM 互操作包装器公开)而不是 TakePicture。无论如何,它对我的​​网络摄像头有效。

From what I can see, wiaItem = wiaRoot.TakePicture() is going down the wrong path. Try this:

string imageFileName;
wiaRoot.TakePicture( out takenFileName);
pictureBox1.Image = Image.FromFile(imageFileName);

TakePicture saves a picture to a file, and returns the new file's name as an output parameter.

Edit per your comment - are you using the "windows 7 version" of WiaLib? If so, try something like this:

var manager = new DeviceManagerClass();
Item wiaItem;
Device device = null;
foreach (var info in manager.DeviceInfos)
{
    if (info.DeviceID == DESIRED_DEVICE_ID)
    {
        device = info.Connect();
        wiaItem = device.ExecuteCommand(CommandID.wiaCommandTakePicture);
    }
}

where you use the ExecuteCommand with the well known guid (also exposed from the COM interop wrapper) rather than TakePicture. It worked for my webcam, in any case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文