拍照自动使用网络摄像头在C#中使用WIA [英] Take a picture automatically using a webcam in C# using WIA

查看:861
本文介绍了拍照自动使用网络摄像头在C#中使用WIA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WIALib访问我的摄像头。我开发的代码非常简单:当按下一个按钮,一个摄像头拍摄照片,然后在图片中显示

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);



但这种方法TakePicture()返回null,所以我可以将图像无法传输。最奇怪的是,照片是该方法TakePicture后真正实现了()被调用,因为如果我去手动摄像头的画面是存在的!我只是不明白为什么它不返回一个值

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.

总之,我要么需要这两种方法之一:
1.使用TakePicture ()工作,返回值我可以使用。
2.访问网络摄像头的图片自动列表中,这样我就可以找回拍摄的最后一张照片。

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.

推荐答案

从我所看到的, wiaItem = wiaRoot.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节省了画面的到文件的,并返回新的文件的名称。作为输出参数

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

修改根据您的评论 - 您使用的是Windows 7的版本WiaLib的?如果是这样,尝试这样的事情:

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);
    }
}



在这里您使用的众所周知的GUID (也由COM互操作包装暴露),而不是TakePicture。它的工作对我的摄像头,在任何情况下。

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.

这篇关于拍照自动使用网络摄像头在C#中使用WIA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆