import base64
from pyzkfp import ZKFP2
import json

def fp_capture():
    zkfp2 = ZKFP2()
    zkfp2.Init()
    # if zkfp2.Init() != 0:
    #     print("Failed to initialize the device")
    #     return

    device_count = zkfp2.GetDeviceCount()

    if device_count > 0:
        zkfp2.OpenDevice(0)
        zkfp2.Light('white')

        while True:
            capture = zkfp2.AcquireFingerprint()
            if capture:
                tmp, img = capture
                
                tmp_bytes = bytearray(tmp)
                tmp_base64 = base64.b64encode(tmp_bytes).decode('utf-8')
                blob_image = zkfp2.Blob2Base64String(img)

                # Return both values in JSON format
                result = json.dumps({
                    'template': tmp_base64,
                    'image': blob_image
                })
                print(result)

                break
            # zkfp2.Terminate()

if __name__ == "__main__":
    fp_capture()
