我在使用 sourceAFIS 提取指纹 "Afis.Extract()" 时出现空指针异常

I get Null Pointer Exception on extraction "Afis.Extract()" of fingerprint Extraction using sourceAFIS

我使用 sourceAFIS 示例控制台应用程序,它 运行 很好。我正在尝试调整示例以从 windows 表单中读取指纹图像,并且在提取方法中得到了 NullReferenceException。

我在所有地方都打开了调试,但无法看到这是从哪里来的。

class FingerprintProcessor
{
    // Shared AfisEngine instance (cannot be shared between different threads though)
    static AfisEngine Afis;
    public static MyPerson EnrollCustomer(string fileName, Customer cust)
    //static CustomerBio EnrollCustomer(Customer cust, FingerprintData custFingerprint)
    {
        Console.WriteLine("Enrolling {0}...", cust.FirstName);

        // Initialize empty fingerprint object and set properties
        CustomerFingerprint fp = new CustomerFingerprint();
        fp.Filename = fileName;
        // Load image from the file
        Console.WriteLine(" Loading image from {0}...", fileName);
        BitmapImage image = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute));
        fp.AsBitmapSource = image;
        // Above update of fp.AsBitmapSource initialized also raw image in fp.Image
        // Check raw image dimensions, Y axis is first, X axis is second
        MessageBox.Show(fp.Image.GetLength(1) +" "+ fp.Image.GetLength(0));

        // Initialize empty person object and set its properties
        MyPerson customer = new MyPerson();
        customer.Name = cust.FirstName;
        // Add fingerprint to the person
        customer.Fingerprints.Add(fp);

        // Execute extraction in order to initialize fp.Template
        Console.WriteLine(" Extracting template...");
        try
        {
                Afis.Extract(customer); This is where the NullReference is thrown.
        catch (Exception e1) {
            MessageBox.Show(e1.ToString());
        }
        // Check template size
        Console.WriteLine(" Template size = {0} bytes", fp.Template.Length);

        return customer;
    }




class MyPerson :Person
{
    public string Name { get; set; }

    public MyPerson processCustomer(Customer aCust)
    {
        return new MyPerson
        {
            Name = aCust.FirstName + " " + aCust.LastName,
        };
    }
}




    private void btnSaveCustomer_Click(object sender, EventArgs e)
    {
        // Initialize SourceAFIS
        Afis = new AfisEngine();
        try 
        {
            FaceRecEntities custEnt = new FaceRecEntities();

            var allCustomers=custEnt.Customers.Select(theCusts=> new MyPerson{Name=theCusts.FirstName,
            /*CFingerprint=theCusts.FingerprintImage*/}).AsEnumerable();

            Customer cust = new Customer();

            cust.FirstName = txtFirstName.Text;
            cust.LastName = txtLastName.Text;
            cust.PhoneNo = textBox1.Text;
            cust.Email = txtPhone.Text;
            cust.Picture = imgConversion.ConvertFileToByte(this.pictureBoxPhoto.ImageLocation);
            cust.FingerprintImage = imgConversion.ConvertFileToByte(this.pictureBoxFingerPrint1.ImageLocation);

            //MyPerson customerBio=new MyPerson().processCustomer(cust);

            MyPerson customerBio = new MyPerson();

            try
            {
                FingerprintProcessor.EnrollCustomer(this.pictureBoxFingerPrint1.ImageLocation, cust);
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString(),"The Enroll Not Working");
            }
            Afis.Threshold = 10;

            MyPerson match = Afis.Identify(customerBio, allCustomers).FirstOrDefault() as MyPerson;
            // Null result means that there is no candidate with similarity score above threshold
            if (match == null)
            {
                Console.WriteLine("No matching person found.");
                MessageBox.Show("No matching person found.");

                return;
            }
            MessageBox.Show("Probe " +customerBio.Name+ " matches registered person "+ match.Name);

            // Compute similarity score
            float score = Afis.Verify(customerBio, match);
            Console.WriteLine("Similarity score between {0} and {1} = {2:F3}", customerBio.Name, match.Name, score);

            custEnt.Customers.Add(cust);
            custEnt.SaveChanges();
            MessageBox.Show("Customer Save Success");

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString(),"Can't add customer");
        }
    }

static AfisEngine Afis 未在您的 class 中初始化。要么你必须在静态 class -

中初始化它
class FingerprintProcessor
{
    static AfisEngine Afis =  new AfisEngine();
}

并且不要在btnSaveCustomer_Click中初始化静态变量。

private void btnSaveCustomer_Click(object sender, EventArgs e)
{
    // Initialize SourceAFIS
    Afis = new AfisEngine(); // this is wrong if its static variable.
}

假设如果您希望 Afis 成为实例变量,那么您需要像这样将它作为参数传递给静态 class。

public static MyPerson EnrollCustomer(string fileName, Customer cust, AfisEngine Afis)
{
 ...
}