8

I have been trying to put together code to actually save image from fingerprint sensors. I have already tried forums and this is my current code which saves file with correct file size but When i open the image, Its not image of fingerprint rather it looks like a corrupted image. Here is what it looks like.

enter image description here

My code is given below. Any help will be appreciated. I am new to windows development.

bool SaveBMP(BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile)
    {
        BITMAPFILEHEADER bmfh;
        BITMAPINFOHEADER info;
        memset(&bmfh, 0, sizeof(BITMAPFILEHEADER));
        memset(&info, 0, sizeof(BITMAPINFOHEADER));
        //Next we fill the file header with data:
        bmfh.bfType = 0x4d42;       // 0x4d42 = 'BM'
        bmfh.bfReserved1 = 0;
        bmfh.bfReserved2 = 0;
        bmfh.bfSize = sizeof(BITMAPFILEHEADER) +
            sizeof(BITMAPINFOHEADER) + paddedsize;
        bmfh.bfOffBits = 0x36;
        //and the info header:
        info.biSize = sizeof(BITMAPINFOHEADER);
        info.biWidth = width;
        info.biHeight = height;
        info.biPlanes = 1;

        info.biBitCount = 8;
        info.biCompression = BI_RGB;

        info.biSizeImage = 0;
        info.biXPelsPerMeter = 0x0ec4;
        info.biYPelsPerMeter = 0x0ec4;
        info.biClrUsed = 0;

        info.biClrImportant = 0;

        HANDLE file = CreateFile(bmpfile, GENERIC_WRITE, FILE_SHARE_READ,
            NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

        //Now we write the file header and info header:
        unsigned long bwritten;
        if (WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER),
            &bwritten, NULL) == false)
        {
            CloseHandle(file);
            return false;
        }

        if (WriteFile(file, &info, sizeof(BITMAPINFOHEADER),
            &bwritten, NULL) == false)
        {
            CloseHandle(file);
            return false;
        }
        //and finally the image data:
        if (WriteFile(file, Buffer, paddedsize, &bwritten, NULL) == false)
        {
            CloseHandle(file);
            return false;
        }
        //Now we can close our function with
        CloseHandle(file);
        return true;
    }

    HRESULT CaptureSample()
    {
        HRESULT hr = S_OK;
        WINBIO_SESSION_HANDLE sessionHandle = NULL;
        WINBIO_UNIT_ID unitId = 0;
        WINBIO_REJECT_DETAIL rejectDetail = 0;
        PWINBIO_BIR sample = NULL;
        SIZE_T sampleSize = 0;

        // Connect to the system pool. 
        hr = WinBioOpenSession(
            WINBIO_TYPE_FINGERPRINT,    // Service provider
            WINBIO_POOL_SYSTEM,         // Pool type
            WINBIO_FLAG_RAW,            // Access: Capture raw data
            NULL,                       // Array of biometric unit IDs
            0,                          // Count of biometric unit IDs
            WINBIO_DB_DEFAULT,          // Default database
            &sessionHandle              // [out] Session handle
            );


        // Capture a biometric sample.
        wprintf_s(L"\n Calling WinBioCaptureSample - Swipe sensor...\n");
        hr = WinBioCaptureSample(
            sessionHandle,
            WINBIO_NO_PURPOSE_AVAILABLE,
            WINBIO_DATA_FLAG_RAW,
            &unitId,
            &sample,
            &sampleSize,
            &rejectDetail
            );

        wprintf_s(L"\n Swipe processed - Unit ID: %d\n", unitId);
        wprintf_s(L"\n Captured %d bytes.\n", sampleSize);

        PWINBIO_BIR_HEADER BirHeader = (PWINBIO_BIR_HEADER)(((PBYTE)sample) + sample->HeaderBlock.Offset);
        PWINBIO_BDB_ANSI_381_HEADER AnsiBdbHeader = (PWINBIO_BDB_ANSI_381_HEADER)(((PBYTE)sample) + sample->StandardDataBlock.Offset);
        PWINBIO_BDB_ANSI_381_RECORD AnsiBdbRecord = (PWINBIO_BDB_ANSI_381_RECORD)(((PBYTE)AnsiBdbHeader) + sizeof(WINBIO_BDB_ANSI_381_HEADER));
        PBYTE firstPixel = (PBYTE)((PBYTE)AnsiBdbRecord) + sizeof(WINBIO_BDB_ANSI_381_RECORD);
        SaveBMP(firstPixel, AnsiBdbRecord->HorizontalLineLength, AnsiBdbRecord->VerticalLineLength, AnsiBdbRecord->BlockLength, "D://test.bmp");

        wprintf_s(L"\n Press any key to exit.");
        _getch();
    }
3
  • 1
    BITMAPINFOHEADER: "If biCompression equals BI_RGB and the bitmap uses 8 bpp or less, the bitmap has a color table immediatelly following the BITMAPINFOHEADER structure. The color table consists of an array of RGBQUAD values. The size of the array is given by the biClrUsed member. If biClrUsed is zero, the array contains the maximum number of colors for the given bitdepth; that is, 2^ biBitCount colors." Apr 14, 2015 at 16:00
  • is fingerprint connect directly to a computer or micro controller? how do you connect finger print to your system? Apr 25, 2015 at 18:00
  • No , it's directly connected to pc using usb. May 20, 2015 at 13:29

1 Answer 1

2

IInspectable is correct, the corruption looks like it's coming from your implicit use of color tables:

info.biBitCount = 8;
info.biCompression = BI_RGB;

If your data is actually just 24-bit RGB, you can do info.biBitCount = 24; to render a valid bitmap. If it's lower (or higher) than that, then you'll need to do some conversion work. You can check AnsiBdbHeader->PixelDepth to confirm that it's the 8 bits per pixel that you expect.

It also looks like your passing AnsiBdbRecord->BlockLength to SaveBMP isn't quite right. The docs for this field say:

WINBIO_BDB_ANSI_381_RECORD structure
BlockLength
Contains the number of bytes in this structure plus the number of bytes of sample image data.

So you'll want to make sure to subtract sizeof(WINBIO_BDB_ANSI_381_RECORD) before passing it as your bitmap buffer size.

Side note, make sure you free the memory involved after the capture.

WinBioFree(sample);
WinBioCloseSession(sessionHandle);
1
  • The data is not RGB, fingerprint scanners generate a monochrome image. WINBIO_BDB_ANSI_381_HEADER.PixelDepth indicates the number of bits per pixel. The ImageCompressionAlg member is relevant too. Apr 25, 2015 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.