我在提取时得到Null Pointer Exception" Afis.Extract()"使用sourceAFIS进行指纹提取

时间:2016-02-23 14:26:57

标签: c# nullreferenceexception fingerprint

我去了sourceAFIS示例控制台应用程序,运行正常。我正在尝试调整示例以从窗体中读取指纹图像,并且在提取方法中得到了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");
        }
    }

1 个答案:

答案 0 :(得分:0)

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)
{
 ...
}