Skip to main content

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​

DetailValueDescription
Class NameResponseHandlerUtility class for standardizing API responses.
Design PatternUtility ClassPrivate constructor (private ResponseHandler()) prevents instantiation; all methods are static.
Core FunctionsGenerates 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)​

  1. Encode to JPEG: Uses Imgcodecs.imencode(".JPEG", image, buffer) to encode the OpenCV Mat object into a byte array format (JPEG) and stores it in a MatOfByte buffer.
  2. Base64 Conversion: The byte array from the buffer is then converted into a Base64 string using Base64.getEncoder().encodeToString().
  3. 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 ValueDescription
"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 Status200 OK

different(...)​

This method includes the encoded difference image and details about the detected regions.

Key ValueDescription
"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 Status200 OK

4. Error and Status Handlers​

These methods ensure API failure responses are consistent and map to appropriate HTTP status codes.

MethodHTTP StatusStatus KeyPurpose
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 MethodPurpose
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}).