使用 WIA、C# 和 Win 7 进行相机捕捉
我尝试在 Windows 7 上使用 WIA 2.0、C#、.net 4.0 从网络摄像头拍照。我尝试了许多不同的示例,但没有任何效果。我总是收到此异常:“ComException was unhandled”来自 HRESULT 的异常:0x80210015
”。代码是 WIA_S_NO_DEVICE_AVAILABLE
。我检查了 WIA 服务是否正在运行以及摄像头是否正在运行显示在扫描仪和相机中。我不知道这里
出了
什么问题!有人可以帮忙吗?设备 d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);:
这里是代码
string DeviceID;
const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 0x00000001;
public const uint FLATBED = 0x00000002;
}
class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 0x00000001;
}
class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const uint WIA_DIP_FIRST = 2;
public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
//
// Scanner only device properties (DPS)
//
public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
}
class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 0x80210000;
public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
}
public void ADFScan()
{
//Choose Scanner
CommonDialogClass class1 = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);
if (d != null)
{
this.DeviceID = d.DeviceID;
}
else
{
//no scanner chosen
return;
}
WIA.CommonDialog WiaCommonDialog = new CommonDialogClass();
bool hasMorePages = true;
int x = 0;
int numPages = 0;
while (hasMorePages)
{
//Create DeviceManager
DeviceManager manager = new DeviceManagerClass();
Device WiaDev = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == this.DeviceID)
{
WIA.Properties infoprop = null;
infoprop = info.Properties;
//connect to scanner
WiaDev = info.Connect();
break;
}
}
//Start Scan
WIA.ImageFile img = null;
WIA.Item Item = WiaDev.Items[1] as WIA.Item;
try
{
img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, wiaFormatJPEG, false);
//process image:
//one would do image processing here
//
//Save to file
string varImageFileName = "c:\\test" + x.ToString() + ".jpg";
if (File.Exists(varImageFileName))
{
//file exists, delete it
File.Delete(varImageFileName);
}
img.SaveFile(varImageFileName);
numPages++;
img = null;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
Item = null;
//determine if there are any more pages waiting
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
foreach (Property prop in WiaDev.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null)
//may not exist on flatbed scanner but required for feeder
{
//check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
x++;
}
}
}
感谢您的帮助!
I try to take a picture from a webcam with WIA 2.0, C#, .net 4.0 on Windows 7. I tried many different samples, but nothing works. I always get this Exception: "ComException was unhandled" Exception from HRESULT: 0x80210015
". The code is WIA_S_NO_DEVICE_AVAILABLE
. I checked, if WIA-Service is running and if the cam shows up in Scanners and Camera. I have no idea whats wrong here! Can anybody help?
Exception is thrown on this line:
Device d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);:
Here the code
string DeviceID;
const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 0x00000001;
public const uint FLATBED = 0x00000002;
}
class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 0x00000001;
}
class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const uint WIA_DIP_FIRST = 2;
public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
//
// Scanner only device properties (DPS)
//
public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
}
class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 0x80210000;
public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
}
public void ADFScan()
{
//Choose Scanner
CommonDialogClass class1 = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);
if (d != null)
{
this.DeviceID = d.DeviceID;
}
else
{
//no scanner chosen
return;
}
WIA.CommonDialog WiaCommonDialog = new CommonDialogClass();
bool hasMorePages = true;
int x = 0;
int numPages = 0;
while (hasMorePages)
{
//Create DeviceManager
DeviceManager manager = new DeviceManagerClass();
Device WiaDev = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == this.DeviceID)
{
WIA.Properties infoprop = null;
infoprop = info.Properties;
//connect to scanner
WiaDev = info.Connect();
break;
}
}
//Start Scan
WIA.ImageFile img = null;
WIA.Item Item = WiaDev.Items[1] as WIA.Item;
try
{
img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, wiaFormatJPEG, false);
//process image:
//one would do image processing here
//
//Save to file
string varImageFileName = "c:\\test" + x.ToString() + ".jpg";
if (File.Exists(varImageFileName))
{
//file exists, delete it
File.Delete(varImageFileName);
}
img.SaveFile(varImageFileName);
numPages++;
img = null;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
Item = null;
//determine if there are any more pages waiting
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
foreach (Property prop in WiaDev.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null)
//may not exist on flatbed scanner but required for feeder
{
//check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
x++;
}
}
}
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是来自 Microsoft 的未记录错误的典型示例...错误代码仅表示未找到设备,或者未连接或未被 WIA 识别。
This is a typical example of non-documented error from Microsoft... The error code simply means that no device has been found, either not connected or not-WIA recognized.
尝试添加CameraTest之类的方法来测试是否有摄像头连接:
Try to add a method like CameraTest to test, if there is a camera connected:
尝试下面的代码:
Try the code below: