java.lang.Object
jku.se.CloudOCRService
Service class for performing OCR (Optical Character Recognition) using Google Cloud Vision API.
Extracts structured data such as date, amount, and category from an image of a receipt or invoice.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Inner class to encapsulate OCR results. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyzeImage
(File imageFile) Analyzes an image using Google Cloud Vision API and extracts structured information.buildRequestJson
(String base64Image) Builds the JSON request body for the OCR API.detectCategory
(String text) Attempts to detect whether the receipt belongs to a restaurant or supermarket.encodeImageToBase64
(File imageFile) Encodes an image file to a Base64 string.extractAmount
(String text) Extracts the amount from OCR text, prioritizing labeled values (e.g., "Amount").extractDate
(String text) Extracts a date string from raw text using several regex formats.extractTextFromJson
(String json) Parses the OCR API response JSON and extracts the full detected text.
-
Constructor Details
-
CloudOCRService
public CloudOCRService()
-
-
Method Details
-
analyzeImage
Analyzes an image using Google Cloud Vision API and extracts structured information.- Parameters:
imageFile
- The image file to analyze.- Returns:
- OCRResult containing date, amount, and category.
- Throws:
IOException
- if the request fails.
-
encodeImageToBase64
Encodes an image file to a Base64 string.- Parameters:
imageFile
- The file to encode.- Returns:
- Base64-encoded image string.
- Throws:
IOException
- if reading the file fails.
-
buildRequestJson
Builds the JSON request body for the OCR API.- Parameters:
base64Image
- Base64-encoded image string.- Returns:
- JSON request as a string.
-
extractTextFromJson
Parses the OCR API response JSON and extracts the full detected text.- Parameters:
json
- JSON response from the API.- Returns:
- Extracted plain text.
-
extractDate
Extracts a date string from raw text using several regex formats.- Parameters:
text
- Raw OCR text.- Returns:
- Extracted date string or "Not found".
-
extractAmount
Extracts the amount from OCR text, prioritizing labeled values (e.g., "Amount").- Parameters:
text
- OCR-detected text.- Returns:
- Extracted amount string or "Not found".
-
detectCategory
Attempts to detect whether the receipt belongs to a restaurant or supermarket.- Parameters:
text
- OCR-detected text.- Returns:
- Category label: "RESTAURANT", "SUPERMARKET", or "OTHER".
-