|
@@ -0,0 +1,159 @@
|
|
|
+package com.example;
|
|
|
+
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import com.machinezoo.sourceafis.FingerprintImage;
|
|
|
+import com.machinezoo.sourceafis.FingerprintMatcher;
|
|
|
+import com.machinezoo.sourceafis.FingerprintTemplate;
|
|
|
+
|
|
|
+public class EventHandler {
|
|
|
+
|
|
|
+ private static final double MATCH_THRESHOLD = 40.0;
|
|
|
+
|
|
|
+ public static String handleEvent(String rawMessage) {
|
|
|
+ try {
|
|
|
+ System.err.println(rawMessage);
|
|
|
+
|
|
|
+ JSONObject message = new JSONObject(rawMessage);
|
|
|
+
|
|
|
+ String id = message.optString("id", "unknown");
|
|
|
+ String cmd = message.optString("cmd", "").toLowerCase();
|
|
|
+ String data = message.optString("data", null); // base64 string
|
|
|
+
|
|
|
+ if (data == null) {
|
|
|
+ throw new IllegalArgumentException("Missing base64 data payload");
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (cmd) {
|
|
|
+ case "registration":
|
|
|
+ return handleRegistration(id, data, message);
|
|
|
+ // case "verification":
|
|
|
+ // return handleVerification(...);
|
|
|
+ case "extract template":
|
|
|
+ return handleExtractTemplate(id, data);
|
|
|
+ default:
|
|
|
+ JSONObject unknown = new JSONObject();
|
|
|
+ unknown.put("id", id);
|
|
|
+ unknown.put("message", "Unknown command: " + cmd);
|
|
|
+ return unknown.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ JSONObject error = new JSONObject();
|
|
|
+ error.put("error", "Failed to process message: " + e.getMessage());
|
|
|
+ return error.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String handleRegistration(String id, String imageBase64, JSONObject message) {
|
|
|
+ try {
|
|
|
+ // Extract the fingerprint template array (fpTemplateArray) from the root-level message
|
|
|
+ JSONArray templateArray = message.optJSONArray("fpTemplateArray");
|
|
|
+
|
|
|
+ if (imageBase64 == null || templateArray == null) {
|
|
|
+ throw new IllegalArgumentException("Missing image data or template array.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Convert Base64 image to BufferedImage
|
|
|
+ byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
|
|
|
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
+
|
|
|
+ if (image == null) {
|
|
|
+ throw new IllegalArgumentException("Invalid image format.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create a fingerprint template from the image
|
|
|
+ FingerprintTemplate probeTemplate = new FingerprintTemplate(new FingerprintImage(imageBytes));
|
|
|
+
|
|
|
+ // If the template array is empty, skip the matching logic
|
|
|
+ if (templateArray.isEmpty()) {
|
|
|
+ // No existing templates to compare with, proceed with registration of the new template
|
|
|
+ String newTemplateB64 = Base64.getEncoder().encodeToString(probeTemplate.toByteArray());
|
|
|
+
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ response.put("id", id);
|
|
|
+ response.put("message", "New fingerprint template constructed.");
|
|
|
+ response.put("data", newTemplateB64);
|
|
|
+ response.put("score", 100);
|
|
|
+ return response.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Compare with existing templates (if any)
|
|
|
+ for (int i = 0; i < templateArray.length(); i++) {
|
|
|
+ String templateB64 = templateArray.getString(i);
|
|
|
+ byte[] templateBytes = Base64.getDecoder().decode(templateB64);
|
|
|
+ FingerprintTemplate candidate = new FingerprintTemplate(templateBytes);
|
|
|
+
|
|
|
+ double score = new FingerprintMatcher(probeTemplate).match(candidate);
|
|
|
+ if (score >= MATCH_THRESHOLD) {
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ response.put("id", id);
|
|
|
+ response.put("message", "Fingerprint already registered.");
|
|
|
+ response.put("score", score);
|
|
|
+ response.put("data", JSONObject.NULL);
|
|
|
+ return response.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // No match: register new
|
|
|
+ String newTemplateB64 = Base64.getEncoder().encodeToString(probeTemplate.toByteArray());
|
|
|
+
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ response.put("id", id);
|
|
|
+ response.put("message", "New fingerprint template constructed.");
|
|
|
+ response.put("data", newTemplateB64);
|
|
|
+ response.put("score", 100);
|
|
|
+ return response.toString();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ JSONObject error = new JSONObject();
|
|
|
+ error.put("error", "Registration failed: " + e.getMessage());
|
|
|
+ return error.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static String handleVerification(JSONObject data) {
|
|
|
+ try {
|
|
|
+ // logic here
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String handleExtractTemplate(String id, String base64) {
|
|
|
+ try {
|
|
|
+ byte[] imageBytes = Base64.getDecoder().decode(base64);
|
|
|
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
+
|
|
|
+ if (image == null) {
|
|
|
+ throw new IllegalArgumentException("Invalid image data.");
|
|
|
+ }
|
|
|
+
|
|
|
+ FingerprintTemplate template = new FingerprintTemplate(new FingerprintImage(imageBytes));
|
|
|
+
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ response.put("id", id);
|
|
|
+ response.put("message", "Fingerprint template successfully extracted.");
|
|
|
+ response.put("data", Base64.getEncoder().encodeToString(template.toByteArray())); // optional
|
|
|
+
|
|
|
+ return response.toString();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ JSONObject error = new JSONObject();
|
|
|
+ error.put("id", id);
|
|
|
+ error.put("error", "Template extraction failed: " + e.getMessage());
|
|
|
+ return error.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|