我在使用源AFIS提取指纹提取的“Afis.Extract()”时遇到空指针异常

本文关键字:提取 Extract 空指针异常 遇到 Afis AFIS 指纹 | 更新日期: 2023-09-27 18:35:47

我去源AFIS示例控制台应用程序,它运行良好。我正在尝试调整示例以从 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");
        }
    }

我在使用源AFIS提取指纹提取的“Afis.Extract()”时遇到空指针异常

static AfisEngine Afis未在类中初始化。 要么你必须在静态类中初始化它 -

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 作为实例变量,那么您需要将其作为参数传递给静态类,如下所示。

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