|
@@ -4,8 +4,6 @@ import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Base64;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
@@ -13,14 +11,19 @@ import javax.imageio.ImageIO;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
+import com.example.types.Fingerprint;
|
|
|
+import com.example.types.FingerprintCompareListItem;
|
|
|
+import com.example.types.PersonFingerprintData;
|
|
|
+import com.example.types.VerificationResult;
|
|
|
+import static com.example.utils.JavaResponse.buildJavaResponse;
|
|
|
+import com.example.utils.TransparencyContents;
|
|
|
+import com.example.utils.Verification;
|
|
|
import com.machinezoo.sourceafis.FingerprintImage;
|
|
|
-import com.machinezoo.sourceafis.FingerprintMatcher;
|
|
|
import com.machinezoo.sourceafis.FingerprintTemplate;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
public class EventHandler {
|
|
|
|
|
|
- private static final double MATCH_THRESHOLD = 40.0;
|
|
|
+ private static FingerprintCompareListItem[] registeredFingerprints = new FingerprintCompareListItem[0];
|
|
|
|
|
|
public static String handleEvent(String rawMessage) {
|
|
|
try {
|
|
@@ -81,6 +84,8 @@ public class EventHandler {
|
|
|
return handleVerification(id, imageBase64, fpTemplateList, personInfo, fingerPosition);
|
|
|
case "qualityassurance":
|
|
|
return handleQualityAssurance(id, imageBase64, fpTemplateList, personInfo, fingerPosition);
|
|
|
+ case "updateRegisteredFingerprints":
|
|
|
+ return updateRegisteredFingerprints(id, fpTemplateList);
|
|
|
default:
|
|
|
// Unknown command fallback
|
|
|
JSONObject unknown = new JSONObject();
|
|
@@ -100,62 +105,40 @@ public class EventHandler {
|
|
|
public static String handleRegistration(String id, String imageBase64, FingerprintCompareListItem[] templateArray, PersonFingerprintData personInfo, int fingerPosition) {
|
|
|
try {
|
|
|
// Decode the fingerprint image from base64
|
|
|
- Integer edgeScore = null;
|
|
|
+ AtomicInteger edgeScore = new AtomicInteger();
|
|
|
byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
|
|
|
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
- if (image == null) {
|
|
|
- throw new IllegalArgumentException("Invalid image format.");
|
|
|
- }
|
|
|
-
|
|
|
// Create a FingerprintTemplate from the imageBytes
|
|
|
FingerprintTemplate probeTemplate = new FingerprintTemplate(new FingerprintImage(imageBytes));
|
|
|
|
|
|
- // Logic for transparency goes here
|
|
|
- // ... <Edge Number Must be 250 to be considered success>
|
|
|
- try (TransparencyContents transparency = new TransparencyContents()) {
|
|
|
- FingerprintTemplate probe = new FingerprintTemplate(imageBytes);
|
|
|
- transparency.accepts("edge-table");
|
|
|
- transparency.take("edge-table", "application/cbor", imageBytes);
|
|
|
- edgeScore = transparency.countEdgeNumber();
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("Error: " + e.getMessage());
|
|
|
+ if (image == null) {
|
|
|
+ throw new IllegalArgumentException("Invalid image format.");
|
|
|
}
|
|
|
|
|
|
+ // Calculate edge scores to determine fingerprint quality
|
|
|
+ edgeScore.set(TransparencyContents.calculateEdgeScore(imageBytes));
|
|
|
+
|
|
|
// If no existing templates are provided, register the new one
|
|
|
- if (templateArray == null || templateArray.length <= 0) {
|
|
|
+ if (templateArray == null || templateArray.length <= 0 && registeredFingerprints.length <= 0) {
|
|
|
// Register this new template
|
|
|
- String response = buildJavaResponse(id, "Registration", "Success", "No existing templates. Registered as new.", probeTemplate, 100.0, edgeScore, personInfo, fingerPosition);
|
|
|
+ String response = buildJavaResponse(id, "Registration", "Success", "No existing templates. Registered as new.", probeTemplate, 100.0, edgeScore.get(), personInfo, fingerPosition);
|
|
|
return response.toString(); // Return the response as a JSON string
|
|
|
}
|
|
|
|
|
|
- // Compare with existing templates
|
|
|
- double bestScore = 0;
|
|
|
- String matchedPerson = null;
|
|
|
-
|
|
|
- for (FingerprintCompareListItem entry : templateArray) {
|
|
|
- String personName = entry.getName();
|
|
|
- FingerprintTemplate candidate = entry.getFingerprint();
|
|
|
-
|
|
|
- double score = new FingerprintMatcher(probeTemplate).match(candidate);
|
|
|
- if (score > bestScore) {
|
|
|
- bestScore = score;
|
|
|
- matchedPerson = personName;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ VerificationResult matchedPerson = Verification.verifyFingeprint(probeTemplate, templateArray, registeredFingerprints);
|
|
|
// Return response based on score
|
|
|
- if (bestScore >= MATCH_THRESHOLD) {
|
|
|
- // Match found
|
|
|
- return buildJavaResponse(id, "Registration", "Registered", "Existing Fingerprint template found for " + matchedPerson + "!", probeTemplate, bestScore, edgeScore, personInfo, fingerPosition).toString();
|
|
|
+ if (matchedPerson != null) { // Match found
|
|
|
+ String response = buildJavaResponse(id, "Registration", "Registered", "Existing Fingerprint template found for " + matchedPerson + "!", probeTemplate, matchedPerson.getScore(), edgeScore.get(), personInfo, fingerPosition);
|
|
|
+ return response;
|
|
|
} else {
|
|
|
// Register new template
|
|
|
- String response = buildJavaResponse(id, "Registration", "Success", "New fingerprint template constructed.", probeTemplate, 100.0, edgeScore, personInfo, fingerPosition);
|
|
|
+ String response = buildJavaResponse(id, "Registration", "Success", "New fingerprint template constructed.", probeTemplate, 100.0, edgeScore.get(), personInfo, fingerPosition);
|
|
|
return response.toString();
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
// Handle errors during registration
|
|
|
- String errorResponse = buildJavaResponse(id, "Registration", "Failed", "Template Construction Failed: " + e.getMessage(), null, 0.0, null, personInfo, fingerPosition);
|
|
|
+ String errorResponse = buildJavaResponse(id, "Registration", "Failed", "Template Construction Failed: " + e.getMessage(), null, 0.0, 0, personInfo, fingerPosition);
|
|
|
return errorResponse.toString();
|
|
|
}
|
|
|
}
|
|
@@ -163,7 +146,7 @@ public class EventHandler {
|
|
|
public static String handleVerification(String id, String imageBase64, FingerprintCompareListItem[] templateArray, PersonFingerprintData personInfo, int fingerPosition) {
|
|
|
try {
|
|
|
// Decode the fingerprint image from base64
|
|
|
- Integer edgeScore = null;
|
|
|
+ AtomicInteger edgeScore = new AtomicInteger();
|
|
|
byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
|
|
|
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
if (image == null) {
|
|
@@ -173,87 +156,28 @@ public class EventHandler {
|
|
|
// Create a FingerprintTemplate from the imageBytes
|
|
|
FingerprintTemplate probeTemplate = new FingerprintTemplate(new FingerprintImage(imageBytes));
|
|
|
|
|
|
- // Logic for transparency goes here
|
|
|
- // ... <Edge Number Must be 250 to be considered success>
|
|
|
- try (TransparencyContents transparency = new TransparencyContents()) {
|
|
|
- FingerprintTemplate probe = new FingerprintTemplate(imageBytes);
|
|
|
- transparency.accepts("edge-table");
|
|
|
- transparency.take("edge-table", "application/cbor", imageBytes);
|
|
|
- edgeScore = transparency.countEdgeNumber();
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("Error: " + e.getMessage());
|
|
|
- }
|
|
|
+ // Calculate edge scores to determine fingerprint quality
|
|
|
+ edgeScore.set(TransparencyContents.calculateEdgeScore(imageBytes));
|
|
|
|
|
|
- double bestScore = 0;
|
|
|
- int matchedFingerprintPosition = 0; // just put 0 as default value for now
|
|
|
- String matchedPerson = null;
|
|
|
- FingerprintTemplate matchedFingerprintTemplate = null;
|
|
|
+ VerificationResult matchedPerson = Verification.verifyFingeprint(probeTemplate, templateArray, registeredFingerprints);
|
|
|
|
|
|
- for (int i = 0; i < templateArray.length; i++) {
|
|
|
- FingerprintCompareListItem entry = templateArray[i];
|
|
|
- String personName = entry.getName();
|
|
|
- int fpPosition = entry.getFingerPosition();
|
|
|
- FingerprintTemplate candidateTemplate = entry.getFingerprint();
|
|
|
-
|
|
|
- // Maybe there's no need to check? will see about it later
|
|
|
- if (candidateTemplate == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- double score = new FingerprintMatcher(probeTemplate).match(candidateTemplate);
|
|
|
-
|
|
|
- if (score > bestScore) {
|
|
|
- bestScore = score;
|
|
|
- matchedFingerprintPosition = fpPosition;
|
|
|
- matchedPerson = personName;
|
|
|
- matchedFingerprintTemplate = candidateTemplate;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (bestScore >= MATCH_THRESHOLD) {
|
|
|
- // Match found
|
|
|
- Fingerprint matchedFingerprint = new Fingerprint(matchedPerson, matchedFingerprintPosition, Base64.getEncoder().encodeToString(matchedFingerprintTemplate.toByteArray()));
|
|
|
- List<Fingerprint> fingerprints = Collections.singletonList(matchedFingerprint);
|
|
|
- PersonFingerprintData matchedData = new PersonFingerprintData(
|
|
|
- personInfo.getId(),
|
|
|
- matchedPerson,
|
|
|
- personInfo.getOrg(),
|
|
|
- personInfo.getCode(),
|
|
|
- fingerprints
|
|
|
- );
|
|
|
- // JavaResponse response = new JavaResponse(id, "Verification", "Match found.", matchedData, bestScore);
|
|
|
- String response = buildJavaResponse(id, "Verification", "Registered", "Match found.", matchedFingerprintTemplate, bestScore, null, matchedData, matchedFingerprintPosition);
|
|
|
- return response.toString();
|
|
|
+ // Return response based on score
|
|
|
+ if (matchedPerson != null) { // Match found
|
|
|
+ return buildJavaResponse(id, "Verification", "Registered", "Existing Fingerprint template found for " + matchedPerson.getName() + "!", probeTemplate, matchedPerson.getScore(), edgeScore.get(), personInfo, fingerPosition).toString().toString();
|
|
|
} else {
|
|
|
- // No match
|
|
|
- PersonFingerprintData noMatchData = new PersonFingerprintData(
|
|
|
- personInfo.getId(),
|
|
|
- personInfo.getName(),
|
|
|
- personInfo.getOrg(),
|
|
|
- personInfo.getCode(),
|
|
|
- new ArrayList<Fingerprint>()
|
|
|
- );
|
|
|
- String response = buildJavaResponse(id, "Verification", "Not Registered", "No Match found.", probeTemplate, bestScore, null, noMatchData, fingerPosition); // defaul value
|
|
|
- return response.toString();
|
|
|
+ // Register new template
|
|
|
+ return buildJavaResponse(id, "Verification", "Not Registered", "Fingerprint Data Not found.", probeTemplate, 0, 0, personInfo, fingerPosition);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- PersonFingerprintData errorData = new PersonFingerprintData(
|
|
|
- personInfo.getId(),
|
|
|
- personInfo.getName(),
|
|
|
- personInfo.getOrg(),
|
|
|
- personInfo.getCode(),
|
|
|
- new ArrayList<Fingerprint>()
|
|
|
- );
|
|
|
- String errorResponse = buildJavaResponse(id, "Verification", "Failed", "No Match found.", null, 0, null, personInfo, fingerPosition); // defaul value
|
|
|
+ String errorResponse = buildJavaResponse(id, "Verification", "Failed", "Verification operation faulty...", null, 0, 0, personInfo, fingerPosition); // defaul value
|
|
|
return errorResponse.toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static String handleQualityAssurance(String id, String imageBase64, FingerprintCompareListItem[] templateArray, PersonFingerprintData personInfo, int fingerPosition) {
|
|
|
try {
|
|
|
- Integer edgeScore = null;
|
|
|
- double bestScore = 0;
|
|
|
+ AtomicInteger edgeScore = new AtomicInteger();
|
|
|
// Decode the fingerprint image from base64
|
|
|
byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
|
|
|
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
@@ -264,61 +188,31 @@ public class EventHandler {
|
|
|
// Create a FingerprintTemplate from the imageBytes
|
|
|
FingerprintTemplate probeTemplate = new FingerprintTemplate(new FingerprintImage(imageBytes));
|
|
|
|
|
|
- // Logic for transparency goes here
|
|
|
- // ... <Edge Number Must be 250 to be considered success>
|
|
|
- try (TransparencyContents transparency = new TransparencyContents()) {
|
|
|
- transparency.accepts("edge-table");
|
|
|
- transparency.take("edge-table", "application/cbor", imageBytes);
|
|
|
- edgeScore = transparency.countEdgeNumber();
|
|
|
+ // Calculate edge scores to determine fingerprint quality
|
|
|
+ edgeScore.set(TransparencyContents.calculateEdgeScore(imageBytes));
|
|
|
+ System.out.println("Edge Score " + edgeScore);
|
|
|
|
|
|
- // Addition features to verify against existing fingerprint data
|
|
|
- String response = buildJavaResponse(id, "QualityAssurance", "Success", "Edge score result: " + bestScore, probeTemplate, bestScore, edgeScore, personInfo, fingerPosition); // defaul value
|
|
|
- return response.toString();
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("Error: " + e.getMessage());
|
|
|
- String response = buildJavaResponse(id, "QualityAssurance", "Failed", "Edge score result: " + bestScore, probeTemplate, bestScore, edgeScore, personInfo, fingerPosition); // defaul value
|
|
|
- return response.toString();
|
|
|
+ VerificationResult matchedPerson = Verification.verifyFingeprint(probeTemplate, templateArray, registeredFingerprints);
|
|
|
+
|
|
|
+ // Return response based on score
|
|
|
+ if (matchedPerson != null) { // Match found
|
|
|
+ return buildJavaResponse(id, "QualityAssurance", "Registered", "Registered fingeprint. ", null, 0, edgeScore.get(), personInfo, fingerPosition).toString();
|
|
|
+ } else {
|
|
|
+ return buildJavaResponse(id, "QualityAssurance", "Not Registered", "New Fingerprint detected...", probeTemplate, 100, edgeScore.get(), personInfo, fingerPosition).toString();
|
|
|
}
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
// Handle errors during checking
|
|
|
- String errorResponse = buildJavaResponse(id, "QualityAssurance", "Failed", "Unable to produce Edge score... " + e.getMessage(), null, 0.0, null, personInfo, fingerPosition);
|
|
|
+ String errorResponse = buildJavaResponse(id, "QualityAssurance", "Failed", "Unable to produce Edge score... " + e.getMessage(), null, 0.0, 0, personInfo, fingerPosition);
|
|
|
return errorResponse.toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String buildJavaResponse(
|
|
|
- String id,
|
|
|
- String operation,
|
|
|
- String status,
|
|
|
- String message,
|
|
|
- FingerprintTemplate fingerprintTemplate,
|
|
|
- double score,
|
|
|
- Integer edgeScore,
|
|
|
- PersonFingerprintData personInfo,
|
|
|
- int fingerPosition) {
|
|
|
-
|
|
|
- // Convert the FingerprintTemplate to a base64-encoded string (assuming `FingerprintTemplate` has a way to do this)
|
|
|
- String templateBase64 = (fingerprintTemplate != null) ? Base64.getEncoder().encodeToString(fingerprintTemplate.toByteArray()) : "";
|
|
|
-
|
|
|
- // Construct the fingerprint data (if there is a fingerprint template)
|
|
|
- Fingerprint fingerprint = new Fingerprint(personInfo.getName(), fingerPosition, templateBase64);
|
|
|
-
|
|
|
- // Add the fingerprint to the personInfo
|
|
|
- personInfo.addFingerprint(fingerprint);
|
|
|
-
|
|
|
- // Build the JavaResponse
|
|
|
- JavaResponse javaResponse = new JavaResponse(id, operation, status, message, personInfo, score, edgeScore);
|
|
|
- // System.out.println("Java Response: " + javaResponse.toString());
|
|
|
-
|
|
|
- // Serialize JavaResponse to JSON string using Jackson
|
|
|
+ public static String updateRegisteredFingerprints(String id, FingerprintCompareListItem[] fpTemplateList) {
|
|
|
try {
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
- String jsonResponse = mapper.writeValueAsString(javaResponse);
|
|
|
- return jsonResponse;
|
|
|
+ return "response here for now...";
|
|
|
} catch (Exception e) {
|
|
|
- // Handle exception if serialization fails
|
|
|
- e.printStackTrace();
|
|
|
- return "{\"error\":\"Failed to serialize response\"}";
|
|
|
+ return "error response here for now...";
|
|
|
}
|
|
|
}
|
|
|
|