Package com.tnsai.tools.utility
Class EncodingTools
java.lang.Object
com.tnsai.tools.utility.EncodingTools
Function-shape encoding tools — QR-code generation/reading via ZXing + text
translation via Google Cloud Translation v2. Replaces the legacy
QRCodeTool and GoogleTranslateTool.
Four typed @Tool methods:
| Method | Backend | Auth |
|---|---|---|
qrGenerate(java.lang.String, java.lang.String, int) | ZXing → PNG file | none |
qrBase64(java.lang.String, int) | ZXing → base64 PNG (no file) | none |
qrRead(java.lang.String) | ZXing decode | none |
googleTranslate(java.lang.String, java.lang.String, java.lang.String) | Google Translate v2 REST | GOOGLE_TRANSLATE_API_KEY |
QR methods are dependency-free (pure ZXing, already on the
tnsai-tools classpath); translate needs a Google Cloud
Translation API key.
- Since:
- 0.5.7 (Phase 2 batch 9 — built-in tool migration to function shape, see #188)
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor — picks GOOGLE_TRANSLATE_API_KEY from env.EncodingTools(String googleTranslateApiKey) Explicit-key constructor.EncodingTools(String googleTranslateApiKey, okhttp3.OkHttpClient http, com.fasterxml.jackson.databind.ObjectMapper mapper) Full constructor for tests / shared HTTP. -
Method Summary
-
Constructor Details
-
EncodingTools
public EncodingTools()Default constructor — picks GOOGLE_TRANSLATE_API_KEY from env. -
EncodingTools
Explicit-key constructor. -
EncodingTools
public EncodingTools(String googleTranslateApiKey, okhttp3.OkHttpClient http, com.fasterxml.jackson.databind.ObjectMapper mapper) Full constructor for tests / shared HTTP.
-
-
Method Details
-
qrGenerate
@Tool(name="qr_generate", description="Generate a QR code containing the given {content} and save it to {outputPath} as a PNG. {size} clamps to [50, 1000] pixels (default 250). Returns the absolute path of the written file. Read-safe to call repeatedly with the same arguments \u2014 the file is overwritten.", keywords={"qr","qrcode","generate","encode","png"}, idempotent=true) public String qrGenerate(@ToolParam(description="Text or URL to encode in the QR code") String content, @ToolParam(description="Absolute path where the PNG will be written; parent dirs must exist") String outputPath, @ToolParam(description="Side length in pixels (50..1000); 0 falls back to default 250",required=false,defaultValue="0") int size) throws IOException - Throws:
IOException
-
qrBase64
@Tool(name="qr_base64", description="Generate a QR code as a base64-encoded PNG (no file written). Useful when the agent needs to embed the QR in an HTML email or API response without touching the filesystem. {size} clamps to [50, 1000] (default 250). Returns the base64 string (no \"data:image/png;base64,\" prefix \u2014 caller adds it if rendering as a data URL).", keywords={"qr","qrcode","base64","encode","png"}, idempotent=true) public String qrBase64(@ToolParam(description="Text or URL to encode in the QR code") String content, @ToolParam(description="Side length in pixels (50..1000); 0 falls back to default 250",required=false,defaultValue="0") int size) throws IOException - Throws:
IOException
-
qrRead
@Tool(name="qr_read", description="Decode a QR code from a PNG/JPEG image file at {imagePath}. Returns the text content embedded in the QR. Throws NoSuchFileException when the path does not exist; throws IllegalArgumentException when the image contains no decodable QR.", keywords={"qr","qrcode","decode","read","scan"}, idempotent=true) public String qrRead(@ToolParam(description="Absolute path to a PNG/JPEG image containing a QR code") String imagePath) throws IOException - Throws:
IOException
-
googleTranslate
@Tool(name="google_translate", description="Translate {text} into {target} language using Google Cloud Translation v2. {target} is an ISO-639 code (\"en\", \"tr\", \"de\", \"ja\"\u2026). Optional {source} language code; if blank, Google auto-detects. Returns the translated text. Requires GOOGLE_TRANSLATE_API_KEY.", keywords={"translate","translation","language","google","localization"}, idempotent=true) public String googleTranslate(@ToolParam(description="Text to translate") String text, @ToolParam(description="ISO-639 target language code (\"en\", \"tr\", \"de\", \"ja\", \"fr\", \u2026)") String target, @ToolParam(description="ISO-639 source language code; blank = auto-detect",required=false,defaultValue="") String source) throws IOException - Throws:
IOException
-