C# 在流数据中循环
我正在写一个使用指纹读取器的程序。我已将指纹数据存储在数组[arr]中。不幸的是,只读取第一个值,即[0]。因此,只检测到一个手指,其余的被忽略,但如果我在数组中放置一个特定的数字,例如2。仅对于该值,它就可以正常工作: 这是我的密码:C# 在流数据中循环,c#,loops,C#,Loops,我正在写一个使用指纹读取器的程序。我已将指纹数据存储在数组[arr]中。不幸的是,只读取第一个值,即[0]。因此,只检测到一个手指,其余的被忽略,但如果我在数组中放置一个特定的数字,例如2。仅对于该值,它就可以正常工作: 这是我的密码: for (int x = 0; x < (arr.Length - 1); x++) { byte[] fpbyte = GetStringToBytes(arr[x]); Stream st
for (int x = 0; x < (arr.Length - 1); x++)
{
byte[] fpbyte = GetStringToBytes(arr[x]);
Stream stream = new MemoryStream(fpbyte);
Data.Templates[x] = new DPFP.Template(stream);
}
foreach (DPFP.Template template in Data.Templates)
{
// Get template from storage.
if (template != null)
{
// Compare feature set with particular template.
ver.Verify(FeatureSet, template, ref res);
Data.IsFeatureSetMatched = res.Verified;
Data.FalseAcceptRate = res.FARAchieved;
if (res.Verified)
MessageBox.Show("Yes");
break; // success
}
}
if (!res.Verified)
Status = DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show("No");
Data.Update();
for(int x=0;x<(arr.Length-1);x++)
{
字节[]fpbyte=GetStringToBytes(arr[x]);
流=新内存流(fpbyte);
Data.Templates[x]=新的DPFP.Template(流);
}
foreach(Data.Templates中的DPFP.Template模板)
{
//从存储器中获取模板。
如果(模板!=null)
{
//将功能集与特定模板进行比较。
版本验证(特征集、模板、参考资源);
Data.IsFeatureSetMatched=已验证的分辨率;
Data.falseaccptrate=达到的分辨率;
如果(已验证)
MessageBox.Show(“是”);
中断;//成功
}
}
如果(!res.Verified)
Status=DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show(“否”);
Data.Update();
无论是否验证,您都可以无条件地中断循环
您的代码应为:
if (res.Verified) {
MessageBox.Show("Yes");
break; // success
}
这是一个很好的例子,为什么良好的编码实践建议始终使用括号,即使对于单行条件效果也是如此,因为错误会更加明显
同样,你也应该写
if (!res.Verified) {
Status = DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show("No");
}
在代码段的末尾。您无条件地中断了循环,无论是否经过验证 您的代码应为:
if (res.Verified) {
MessageBox.Show("Yes");
break; // success
}
这是一个很好的例子,为什么良好的编码实践建议始终使用括号,即使对于单行条件效果也是如此,因为错误会更加明显
同样,你也应该写
if (!res.Verified) {
Status = DPFP.Gui.EventHandlerStatus.Failure;
MessageBox.Show("No");
}
在您的代码片段的末尾。多亏了DragonThinks,我做了以下更改,代码工作正常:
for (int x = 0; x < (arr.Length - 1); x++)
{
byte[] fpbyte = GetStringToBytes(arr[x]);
using (Stream stream = new MemoryStream(fpbyte))
{
Data.Templates[x] = new DPFP.Template(stream);
// Get template from storage.
if (Data.Templates[x] != null)
{
// Compare feature set with particular template.
ver.Verify(FeatureSet, Data.Templates[x], ref res);
Data.IsFeatureSetMatched = res.Verified;
Data.FalseAcceptRate = res.FARAchieved;
if (res.Verified)
{
status.Text = "Verified";
break; // success
}
}
}
}
if (!res.Verified)
{
Status = DPFP.Gui.EventHandlerStatus.Failure;
status.Text = "Unverified";
}
Data.Update();
for(int x=0;x<(arr.Length-1);x++)
{
字节[]fpbyte=GetStringToBytes(arr[x]);
使用(流=新内存流(fpbyte))
{
Data.Templates[x]=新的DPFP.Template(流);
//从存储器中获取模板。
if(Data.Templates[x]!=null)
{
//将功能集与特定模板进行比较。
版本验证(特征集、数据模板[x],参考资源);
Data.IsFeatureSetMatched=已验证的分辨率;
Data.falseaccptrate=达到的分辨率;
如果(已验证)
{
status.Text=“已验证”;
中断;//成功
}
}
}
}
如果(!res.Verified)
{
Status=DPFP.Gui.EventHandlerStatus.Failure;
status.Text=“未验证”;
}
Data.Update();
多亏了DragonThinks,我做了以下更改,代码工作正常:
for (int x = 0; x < (arr.Length - 1); x++)
{
byte[] fpbyte = GetStringToBytes(arr[x]);
using (Stream stream = new MemoryStream(fpbyte))
{
Data.Templates[x] = new DPFP.Template(stream);
// Get template from storage.
if (Data.Templates[x] != null)
{
// Compare feature set with particular template.
ver.Verify(FeatureSet, Data.Templates[x], ref res);
Data.IsFeatureSetMatched = res.Verified;
Data.FalseAcceptRate = res.FARAchieved;
if (res.Verified)
{
status.Text = "Verified";
break; // success
}
}
}
}
if (!res.Verified)
{
Status = DPFP.Gui.EventHandlerStatus.Failure;
status.Text = "Unverified";
}
Data.Update();
for(int x=0;x<(arr.Length-1);x++)
{
字节[]fpbyte=GetStringToBytes(arr[x]);
使用(流=新内存流(fpbyte))
{
Data.Templates[x]=新的DPFP.Template(流);
//从存储器中获取模板。
if(Data.Templates[x]!=null)
{
//将功能集与特定模板进行比较。
版本验证(特征集、数据模板[x],参考资源);
Data.IsFeatureSetMatched=已验证的分辨率;
Data.falseaccptrate=达到的分辨率;
如果(已验证)
{
status.Text=“已验证”;
中断;//成功
}
}
}
}
如果(!res.Verified)
{
Status=DPFP.Gui.EventHandlerStatus.Failure;
status.Text=“未验证”;
}
Data.Update();
为什么arr.Length-1?循环部分中没有流MemoryStream
所以应该使用子句包装在中。@Marcsalerno,如果我放入arr,它有一个索引越界错误。Length@MatNyaga这没什么意义,也许您遗漏了什么为什么arr.Length-1?循环部分MemoryStream
中没有流,因此应该使用子句包装在中。@MarcoSalerno,如果我输入arr,它会有索引越界错误。Length@MatNyaga这没什么意义,也许你错过了什么,我会修改的。谢谢,我会修改的。