ResponseHandler
ResponseHandler.java
The ResponseHandler is a utility class designed to standardize all API responses, ensuring consistency in format (Map<String, Object>), HTTP status codes, and content across successful outcomes, errors, and specific comparison results. It also handles the crucial task of Base64 encoding the resulting difference image.
1. Overview and Design
| Detail | Value | Description |
| Class Name | ResponseHandler | Utility class for standardizing API responses. |
| Design Pattern | Utility Class | Private constructor (private ResponseHandler()) prevents instantiation; all methods are static. |
| Core Functions | Generates ResponseEntity for identical, different, and error states. Encodes Mat to Base64. |
2. Core Image Encoding Function
This function integrates OpenCV with Java I/O to prepare the difference image for JSON transmission.
matToBase64(Mat image)
- Encode to JPEG: Uses
Imgcodecs.imencode(".JPEG", image, buffer)to encode the OpenCVMatobject into a byte array format (JPEG) and stores it in aMatOfBytebuffer. - Base64 Conversion: The byte array from the buffer is then converted into a Base64 string using
Base64.getEncoder().encodeToString(). - Output: Returns the Base64 string, which is included in the JSON response body under the key
"image_base64".
3. Comparison Result Handlers (HTTP 200 OK)
These methods structure the successful comparison results, including detailed metadata and performance metrics.
identical(...)
| Key Value | Description |
"status" | "identical" |
"message" | "Provided images are identical" |
"image_id" | The name/identifier of the matching base image. |
"regions" | The raw JSON string of regions used for the comparison. |
"timings_ms" | A map containing performance metrics (e.g., total_comparison_ms). |
| HTTP Status | 200 OK |
different(...)
This method includes the encoded difference image and details about the detected regions.
| Key Value | Description |
"status" | "different" |
"message" | "Images differ" |
"image_id" | The identifier of the best-matching base image. |
"image_base64" | The Base64 encoded string of the output image, which has semi-transparent boxes drawn over the differences. |
"region_metadata" | An array of bounding box coordinates (if available) found by the ContourService. |
| HTTP Status | 200 OK |
4. Error and Status Handlers
These methods ensure API failure responses are consistent and map to appropriate HTTP status codes.
| Method | HTTP Status | Status Key | Purpose |
invalidImage() | 400 Bad Request | "error" | Returned when uploaded files are corrupt, missing, or cannot be processed by OpenCV. |
shapeMismatch() | 400 Bad Request | "error" | Returned when the base image and compare image do not have the same dimensions (if required by the comparison logic). |
error(String message) | 500 Internal Server Error | "error" | Catch-all for unexpected exceptions and internal processing failures within the Service Layer. |
5. Utility Helpers
| Helper Method | Purpose |
ok(...) | A generic wrapper for simple 200 OK responses with custom data, used less often than identical/different. |
timings(...) | A helper to conveniently build a structured Map of timing data (e.g., load time, diff time) for inclusion in the final response body. |
boxesToMapList(...) | Converts a List of OpenCV's org.opencv.core.Rect objects into a list of simple JSON-compatible maps ({x: 10, y: 10, width: 50, height: 50}). |