|
| 1 | +import com.translated.lara.Credentials; |
| 2 | +import com.translated.lara.errors.LaraException; |
| 3 | +import com.translated.lara.translator.*; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * Complete image translation examples for the Lara Java SDK |
| 12 | + * |
| 13 | + * This example demonstrates: |
| 14 | + * - Basic image translation (returns translated image) |
| 15 | + * - Image translation with advanced options (memories, glossaries, style) |
| 16 | + * - Text extraction from images with translation |
| 17 | + * - Text extraction with verbose output for paragraph details |
| 18 | + */ |
| 19 | +public class ImageTranslation { |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + // All examples can use environment variables for credentials: |
| 23 | + // export LARA_ACCESS_KEY_ID="your-access-key-id" |
| 24 | + // export LARA_ACCESS_KEY_SECRET="your-access-key-secret" |
| 25 | + |
| 26 | + // Set your credentials here |
| 27 | + String accessKeyId = System.getenv("LARA_ACCESS_KEY_ID"); |
| 28 | + String accessKeySecret = System.getenv("LARA_ACCESS_KEY_SECRET"); |
| 29 | + |
| 30 | + Credentials credentials = new Credentials(accessKeyId, accessKeySecret); |
| 31 | + Translator lara = new Translator(credentials); |
| 32 | + |
| 33 | + // Replace with your actual image file path |
| 34 | + String sampleImagePath = "sample_image.png"; // Create this file with your content |
| 35 | + File sampleImage = new File(sampleImagePath); |
| 36 | + |
| 37 | + if (!sampleImage.exists()) { |
| 38 | + System.out.println("Please create a sample image file at: " + sampleImagePath); |
| 39 | + System.out.println("Use an image containing text to translate.\n"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Example 1: Basic image translation |
| 44 | + System.out.println("=== Basic Image Translation ==="); |
| 45 | + String targetLang = "de-DE"; |
| 46 | + |
| 47 | + System.out.println("Translating image: " + sampleImage.getName() + " to " + targetLang); |
| 48 | + |
| 49 | + try { |
| 50 | + InputStream imageStream = lara.images.translate(sampleImage, targetLang); |
| 51 | + |
| 52 | + // Save translated image - replace with your desired output path |
| 53 | + String outputPath = "sample_image_translated.png"; |
| 54 | + Files.copy(imageStream, Paths.get(outputPath)); |
| 55 | + imageStream.close(); |
| 56 | + |
| 57 | + System.out.println("✅ Image translation completed"); |
| 58 | + System.out.println("📄 Translated image saved to: " + outputPath + "\n"); |
| 59 | + } catch (LaraException | IOException e) { |
| 60 | + System.out.println("Error translating image: " + e.getMessage() + "\n"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Example 2: Image translation with source language and advanced options |
| 65 | + System.out.println("=== Image Translation with Advanced Options ==="); |
| 66 | + String sourceLang = "en-US"; |
| 67 | + |
| 68 | + try { |
| 69 | + ImageTranslateOptions options = new ImageTranslateOptions() |
| 70 | + .setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory IDs |
| 71 | + .setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary IDs |
| 72 | + .setStyle(TranslationStyle.FLUID) |
| 73 | + .setNoTrace(false); |
| 74 | + |
| 75 | + InputStream imageStream = lara.images.translate(sampleImage, sourceLang, "fr-FR", options); |
| 76 | + |
| 77 | + String outputPath = "advanced_image_translated.png"; |
| 78 | + Files.copy(imageStream, Paths.get(outputPath)); |
| 79 | + imageStream.close(); |
| 80 | + |
| 81 | + System.out.println("✅ Advanced image translation completed"); |
| 82 | + System.out.println("📄 Translated image saved to: " + outputPath + "\n"); |
| 83 | + } catch (LaraException | IOException e) { |
| 84 | + System.out.println("Error in advanced translation: " + e.getMessage() + "\n"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Example 3: Extract and translate text from image (basic) |
| 89 | + System.out.println("=== Text Extraction and Translation from Image ==="); |
| 90 | + |
| 91 | + try { |
| 92 | + ImageTextResult result = lara.images.translateText(sampleImage, "es-ES"); |
| 93 | + |
| 94 | + System.out.println("✅ Text extraction and translation completed"); |
| 95 | + System.out.println("🌍 Detected source language: " + result.getSourceLanguage()); |
| 96 | + System.out.println("📝 Extracted paragraphs:"); |
| 97 | + |
| 98 | + List<ImageParagraph> paragraphs = result.getParagraphs(); |
| 99 | + for (int i = 0; i < paragraphs.size(); i++) { |
| 100 | + ImageParagraph paragraph = paragraphs.get(i); |
| 101 | + System.out.println("\n Paragraph " + (i + 1) + ":"); |
| 102 | + System.out.println(" Original: " + paragraph.getText()); |
| 103 | + System.out.println(" Translated: " + paragraph.getTranslation()); |
| 104 | + } |
| 105 | + System.out.println(); |
| 106 | + } catch (LaraException e) { |
| 107 | + System.out.println("Error extracting text: " + e.getMessage() + "\n"); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Example 4: Extract and translate text with advanced options |
| 112 | + System.out.println("=== Text Extraction with Advanced Options ==="); |
| 113 | + |
| 114 | + try { |
| 115 | + ImageTextTranslateOptions options = new ImageTextTranslateOptions() |
| 116 | + .setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory IDs |
| 117 | + .setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary IDs |
| 118 | + .setStyle(TranslationStyle.CREATIVE) |
| 119 | + .setVerbose(true) // Include detailed matching information |
| 120 | + .setNoTrace(false); |
| 121 | + |
| 122 | + ImageTextResult result = lara.images.translateText(sampleImage, sourceLang, "it-IT", options); |
| 123 | + |
| 124 | + System.out.println("✅ Advanced text extraction completed"); |
| 125 | + System.out.println("🌍 Source language: " + result.getSourceLanguage()); |
| 126 | + |
| 127 | + // Show adapted-to resources used |
| 128 | + List<String> adaptedTo = result.getAdaptedTo(); |
| 129 | + if (adaptedTo != null && !adaptedTo.isEmpty()) { |
| 130 | + System.out.println("📚 Adapted to memories: " + String.join(", ", adaptedTo)); |
| 131 | + } |
| 132 | + |
| 133 | + // Show glossaries used |
| 134 | + List<String> glossaries = result.getGlossaries(); |
| 135 | + if (glossaries != null && !glossaries.isEmpty()) { |
| 136 | + System.out.println("📖 Glossaries applied: " + String.join(", ", glossaries)); |
| 137 | + } |
| 138 | + |
| 139 | + System.out.println("\n📝 Paragraphs with detailed matches:"); |
| 140 | + List<ImageParagraph> paragraphs = result.getParagraphs(); |
| 141 | + for (int i = 0; i < paragraphs.size(); i++) { |
| 142 | + ImageParagraph paragraph = paragraphs.get(i); |
| 143 | + System.out.println("\n Paragraph " + (i + 1) + ":"); |
| 144 | + System.out.println(" Original: " + paragraph.getText()); |
| 145 | + System.out.println(" Translated: " + paragraph.getTranslation()); |
| 146 | + |
| 147 | + // Show memory matches if available |
| 148 | + if (paragraph.getAdaptedToMatches() != null && !paragraph.getAdaptedToMatches().isEmpty()) { |
| 149 | + System.out.println(" Memory matches found: " + paragraph.getAdaptedToMatches().size()); |
| 150 | + } |
| 151 | + |
| 152 | + // Show glossary matches if available |
| 153 | + if (paragraph.getGlossariesMatches() != null && !paragraph.getGlossariesMatches().isEmpty()) { |
| 154 | + System.out.println(" Glossary matches found: " + paragraph.getGlossariesMatches().size()); |
| 155 | + } |
| 156 | + } |
| 157 | + System.out.println(); |
| 158 | + } catch (LaraException e) { |
| 159 | + System.out.println("Error in advanced text extraction: " + e.getMessage() + "\n"); |
| 160 | + } |
| 161 | + |
| 162 | + System.out.println("=== All examples completed ==="); |
| 163 | + } |
| 164 | +} |
| 165 | + |
0 commit comments