### Scan Image with Language Selection in Java Source: https://context7.com/eiceblue/spire.ocr-for-java/llms.txt This Java example shows how to perform OCR on an image using a specific language data file, such as for Japanese text. It requires dependent files and a language data file. The input is an image file and a language file path, and the output is a text file with the recognized text. ```java import com.spire.ocr.OcrScanner; import java.io.*; public class ScanImageWithLanguageSelection { public static void main(String[] args) throws Exception { // Set the path of the dependent file, please follow the instructions in readme.txt to obtain dependencies String dependencies = "dependencies/"; // Define the path to the language file String languageFile = "data/japandata"; // Define the path of the image file to scan String imageFile = "data/JapaneseSample.png"; // Define the path to the output file String outputFile = "ScanImageWithLanguageSelection_out.txt"; // Create an OcrScanner object OcrScanner scanner = new OcrScanner(); // Set the dependent file path of the OcrScanner object scanner.setDependencies(dependencies); // Loads the specified language file scanner.loadLanguageFile(languageFile); // The OcrScanner object is used to scan the specified image file scanner.scan(imageFile); // Get the scanned text content String scannedText = scanner.getText().toString(); // Create an output file object File output = new File(outputFile); // If the output file already exists, delete it if (output.exists()) { output.delete(); } // Create a BufferedWriter object to write to the output file BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); // Write the scanned text contents to the output file writer.write(scannedText); // Close the BufferedWriter object to release resources writer.close(); // Expected output: Text file containing Japanese text extracted from JapaneseSample.png } } ``` -------------------------------- ### Scan Local Image with Basic Configuration in Java Source: https://context7.com/eiceblue/spire.ocr-for-java/llms.txt This Java code snippet demonstrates how to extract text from a local image file using the Spire.OCR library with basic configuration. It requires dependent files and outputs the recognized text to a file. The primary input is the image file path, and the output is a text file containing the extracted text. ```java import com.spire.ocr.OcrScanner; import java.io.*; public class ScanLocalImage { public static void main(String[] args) throws Exception { // Set the path of the dependent file, please follow the instructions in readme.txt to obtain dependencies String dependencies = "dependencies/"; // Set the path of the image file to be scanned String imageFile = "data/Sample.png"; // Set the path of the output file String outputFile = "ScanLocalImage_out.txt"; // Create an OcrScanner object OcrScanner scanner = new OcrScanner(); // Set the dependent file path of the OcrScanner object scanner.setDependencies(dependencies); // The OcrScanner object is used to scan the specified image file scanner.scan(imageFile); // Get the scanned text content String scannedText = scanner.getText().toString(); // Create an output file object File output = new File(outputFile); // If the output file already exists, delete it if (output.exists()) { output.delete(); } // Create a BufferedWriter object to write to the output file BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); // Write the scanned text contents to the output file writer.write(scannedText); // Close the BufferedWriter object to release resources writer.close(); // Expected output: Text file containing all recognized text from Sample.png } } ``` -------------------------------- ### Scan Image File with New OCR Model in Java Source: https://context7.com/eiceblue/spire.ocr-for-java/llms.txt Extracts text from a specified image file using Spire.OCR's enhanced model. It requires configuring language and model paths, and outputs the recognized text to a file. Dependencies include the Spire OCR library. ```java import com.spire.ocr.*; import java.io.FileWriter; import java.io.IOException; public class scanImageWithNewModel { public static void main(String[] args) throws Exception { // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English (supported languages: English, Chinese, ChineseTraditional, French, German, Japanese, and Korean) configureOptions.setLanguage("English"); // Please download the corresponding model dependencies based on your environment in the guide article // Specify the model path configureOptions.setModelPath("F:\\win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("Sample.png"); // Get the recognized text String scannedText = scanner.getText().toString(); // Write the obtained text to a text file try (FileWriter writer = new FileWriter("extractedOut.txt")) { writer.write(scannedText); System.out.println("Done!"); } catch (IOException e) { System.out.println("Error!"); e.printStackTrace(); } // Expected output: "Done!" message and extractedOut.txt with recognized text } } ``` -------------------------------- ### Java: Extract Text and Coordinates using Spire.OCR Source: https://context7.com/eiceblue/spire.ocr-for-java/llms.txt This Java code snippet demonstrates how to use the Spire.OCR library to extract text from an image along with the precise bounding box coordinates of each text block. It includes setting up the scanner, configuring language and model paths, scanning an image file, and writing the results to a text file. Dependencies include Spire.OCR and standard Java libraries for file I/O and geometry. ```java import com.spire.ocr.*; import java.awt.geom.Rectangle2D; import java.io.FileWriter; import java.io.IOException; public class obtainTextCoordinates { public static void main(String[] args) throws Exception { // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English (supported languages: English, Chinese, ChineseTraditional, French, German, Japanese, and Korean) configureOptions.setLanguage("English"); // Please download the corresponding model dependencies based on your environment // Specify the model path configureOptions.setModelPath("win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("data/Sample.png"); // Retrieve all text blocks detected by the OCR engine IOCRTextBlock[] blocks = scanner.getText().getBlocks(); // Get the text coordinate String scannedText = PrintTextBlock(blocks, null); // Write the obtained text to a text file try (FileWriter writer = new FileWriter("obtainTextCoordinates.txt")) { writer.write(scannedText); System.out.println("Done!"); } catch (IOException e) { System.out.println("Error!"); e.printStackTrace(); } // Expected output: "Done!" and text file with format: // Rectangle: [x, y, width, height] ,Level(Block): text content } private static String PrintTextBlock(IOCRTextBlock[] blocks, StringBuilder sb) { // Create a new StringBuilder if (sb == null || sb.length() == 0) sb = new StringBuilder(); // Skip processing if no blocks are provided if (blocks != null && blocks.length > 0) { for (IOCRTextBlock block : blocks) { // Retrieve the bounding rectangle of the current block Rectangle2D rectangle = block.getBox(); // Format the rectangle coordinates and block details String t1 = String.format("Rectangle: [%f, %f, %f, %f] ,", rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); String t2 = String.format("Level(%s): %s", block.getLevel(), block.getText()); String Text = t1 + t2; Text = Text + "\n"; // Append the formatted information to the result buffer sb.append(Text); // Recursively process any child text blocks PrintTextBlock(block.getTextBlock(), sb); // Add a newline after each line-level block for readability if (block.getLevel() == TextBlockType.Line) sb.append("\n"); } } return sb.toString(); } } ``` -------------------------------- ### Java OCR: Auto-Rotate Image Scan Source: https://context7.com/eiceblue/spire.ocr-for-java/llms.txt This Java code snippet demonstrates how to use Spire.OCR to automatically detect and correct image rotation before performing OCR. It requires the Spire.OCR library and model dependencies. The input is an image file, and the output is a text file containing the recognized text. ```java import com.spire.ocr.*; import java.io.FileWriter; import java.io.IOException; public class scanRotatedImage { public static void main(String[] args) throws Exception { // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English (supported languages: English, Chinese, ChineseTraditional, French, German, Japanese, and Korean) configureOptions.setLanguage("English"); // Set whether the image rotates or not configureOptions.setAutoRotate(true); // Please download the corresponding model dependencies based on your environment // Specify the model path configureOptions.setModelPath("win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("data/RotatedImage.png"); // Get the recognized text VisualTextAligner visualTextAligner = new VisualTextAligner(scanner.getText()); String scannedText = visualTextAligner.toString(); // Write the obtained text to a text file try (FileWriter writer = new FileWriter("RotatedExtractedOut.txt")) { writer.write(scannedText); System.out.println("Done!"); } catch (IOException e) { System.out.println("Error!"); e.printStackTrace(); } // Expected output: "Done!" and text file with corrected, aligned text from rotated image } } ``` -------------------------------- ### Scan Local Image and Extract Text (Java) Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md Performs OCR on a local image file using Spire OCR for Java. This snippet demonstrates setting the dependencies path and then scanning a specified image file to extract its text content. ```java import spire.ocr.sdk.OcrScanner; // Create an OcrScanner object OcrScanner scanner = new OcrScanner(); // Set the dependent file path scanner.setDependencies("dependencies/"); // Scan the specified image file scanner.scan("data/Sample.png"); // Get the scanned text content String scannedText = scanner.getText().toString(); ``` -------------------------------- ### Scan Image with New OCR Model (Java) Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md Scans a local image file using the new OCR model with Spire.OCR for Java. This method requires specifying the image file path, along with language and model path configurations. The extracted text is then returned. ```java import spire.ocr.sdk.OcrScanner; import spire.ocr.sdk.configuration.ConfigureOptions; // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English (supported languages: English, Chinese, ChineseTraditional, French, German, Japanese, and Korean) configureOptions.setLanguage("English"); // Specify the model path configureOptions.setModelPath("F:\\win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("Sample.png"); // Get the recognized text String scannedText = scanner.getText().toString(); ``` -------------------------------- ### Scan Image Stream with New OCR Model (Java) Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md Scans an image stream using the new OCR model with Spire.OCR for Java. It requires setting up ConfigureOptions for language and model path before scanning. The output is the recognized text. ```java import spire.ocr.sdk.OcrScanner; import spire.ocr.sdk.common.OCRImageFormat; import spire.ocr.sdk.configuration.ConfigureOptions; // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English (supported languages: English, Chinese, ChineseTraditional, French, German, Japanese, and Korean) configureOptions.setLanguage("English"); // Specify the model path configureOptions.setModelPath("F:\\win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image stream scanner.Scan(stream, OCRImageFormat.Gif); // Get the recognized text String scannedText = scanner.getText().toString(); ``` -------------------------------- ### Scan Image with Language Selection (Java) Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md Scans a local image file and extracts text using Spire OCR, with support for specifying the language. It involves setting dependencies, loading language files, and then performing the scan. The recognized text is retrieved afterward. ```java import spire.ocr.sdk.OcrScanner; // Create an OcrScanner object OcrScanner scanner = new OcrScanner(); // Set the dependent file path of the OcrScanner object scanner.setDependencies(dependencies); // Loads the specified language file scanner.loadLanguageFile(languageFile); // The OcrScanner object is used to scan the specified image file scanner.scan(imageFile); // Get the scanned text content String scannedText = scanner.getText().toString(); ``` -------------------------------- ### Extract Text and Coordinates with Java OCR Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md This Java code demonstrates how to extract text and its bounding box coordinates from an image using Spire.OCR. It involves configuring the OCR scanner with language and model paths, scanning an image, and then iterating through the detected text blocks to retrieve their text content and rectangular coordinates. The function PrintTextBlock recursively processes text blocks at different levels. ```java public class obtainTextCoordinates { public static void main(String[] args) throws Exception { // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English configureOptions.setLanguage("English"); // Specify the model path configureOptions.setModelPath("win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("data/Sample.png"); // Retrieve all text blocks detected by the OCR engine IOCRTextBlock[] blocks = scanner.getText().getBlocks(); // Get the text coordinate String scannedText = PrintTextBlock(blocks, null); } private static String PrintTextBlock(IOCRTextBlock[] blocks, StringBuilder sb) { // Create a new StringBuilder if (sb == null || sb.length() == 0) sb = new StringBuilder(); // Skip processing if no blocks are provided if (blocks != null && blocks.length > 0) { for (IOCRTextBlock block : blocks) { // Retrieve the bounding rectangle of the current block Rectangle2D rectangle = block.getBox(); // Format the rectangle coordinates and block details String t1 = String.format("Rectangle: [%f, %f, %f, %f] ,", rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); String t2 = String.format("Level(%s): %s", block.getLevel(), block.getText()); String Text = t1 + t2; Text = Text + "\n"; // Append the formatted information to the result buffer sb.append(Text); // Recursively process any child text blocks PrintTextBlock(block.getTextBlock(), sb); // Add a newline after each line-level block for readability if (block.getLevel() == TextBlockType.Line) sb.append("\n"); } } return sb.toString(); } } ``` -------------------------------- ### OCR Scan Rotated Image with Java Source: https://github.com/eiceblue/spire.ocr-for-java/blob/main/ocr_java.md This Java code snippet demonstrates how to extract text from a rotated image using Spire.OCR. It configures the `OcrScanner` to automatically detect and correct image rotation by setting `configureOptions.setAutoRotate(true)`. After scanning the rotated image, it retrieves the recognized text using `VisualTextAligner`. ```java // Create an instance of OcrScanner OcrScanner scanner = new OcrScanner(); // Set up scanner configuration ConfigureOptions configureOptions = new ConfigureOptions(); // Specify the language for text recognition, default is English configureOptions.setLanguage("English"); // Set whether the image rotates or not configureOptions.setAutoRotate(true); // Specify the model path configureOptions.setModelPath("win-x64"); // Apply the configuration to the scanner scanner.ConfigureDependencies(configureOptions); // Recognize text from the image scanner.scan("data/RotatedImage.png"); // Get the recognized text VisualTextAligner visualTextAligner = new VisualTextAligner(scanner.getText()); String scannedText = visualTextAligner.toString(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.