private void button1_Click(object sender, EventArgs e)
{
//if (pictureBox1.Image != null)
//{
// Image image = pictureBox1.Image;
// pictureBox1.Image = null;
// image.Dispose();
//}
StringBuilder stringBuilder = null;
// 开始监视代码运行时间
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 需要测试的代码 ....
//去读ArrayList里面图片的位置
foreach (Object item in Contrast())
{
stringBuilder = new StringBuilder(path);
stringBuilder.Append(@"\");
stringBuilder.Append(item.ToString());
// pictureBox1.Image = Image.FromFile(stringBuilder.ToString(),false );
this.textBox1.Text += stringBuilder.ToString() + "\n\n";
}
//MessageBox.Show(user().ToString());
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
double seconds = timespan.TotalSeconds; // 总秒数
MessageBox.Show("对比完成 !使用秒数:" + seconds);
}
static string path = System.Environment.CurrentDirectory + @"\相片";
/// <summary>
/// 1.bmp 为传输进来相片 对比文件相册文件内所有的图片 大于百分之八十存入ArrayList集合返回
/// </summary>
/// <returns></returns>
public ArrayList Contrast()
{
// 读取图片1
FileStream file1 = new FileStream("1.bmp", FileMode.Open);
byte[] img1 = new byte[file1.Length];
file1.Read(img1, 0, (int)file1.Length);
//第二种方法
DirectoryInfo folder = new DirectoryInfo(path);
ArrayList list = new ArrayList();
foreach (FileInfo file in folder.GetFiles("*.bmp"))
{
// 读取图片2
FileStream file2 = new FileStream("相片/" + file.Name, FileMode.Open);
byte[] img2 = new byte[file2.Length];
file2.Read(img2, 0, (int)file2.Length);
// 提取特征
byte[] feature1 = new byte[nRetInit * 2]; // 此处的 nRetInit 为 face_init 的返回值
byte[] feature2 = new byte[nRetInit * 2]; // 此处的 nRetInit 为 face_init 的返回值
int nRet1 = face_get_feature_from_image(hCtx[0], img1, (int)file1.Length, feature1);
int nRet2 = face_get_feature_from_image(hCtx[0], img2, (int)file2.Length, feature2);
//Console.WriteLine("提取特征1={0} \n" + strFeat1, nRet1);
// Console.WriteLine("提取特征2={0} \n" + strFeat2, nRet2);
// 比对得到分数
int nScore = face_comp_feature(hCtx[0], feature1, feature2);
// Console.Write("比对分数:{0}", nScore);
if (nScore > 80)
{
list.Add(file.Name);
}
// MessageBox.Show(file.Name);
file2.Close();
}
CountObject obj;
for (int i = 0; i < 5; i++)
{
obj = new CountObject();
//obj = null; // 这一步,只是为了更清晰些验证引用的对象是否释放!
GC.Collect();
}
file1.Close();
return list;
}