|
| 1 | +--- |
| 2 | +layout: default-layout |
| 3 | +title: Java User Guide - Dynamsoft Label Recognizer |
| 4 | +description: This is the user guide page of Dynamsoft Label Recognizer for Java Language. |
| 5 | +keywords: java, user guide |
| 6 | +needAutoGenerateSidebar: true |
| 7 | +needGenerateH3Content: true |
| 8 | +permalink: /programming/java/user-guide-v2.0.0.html |
| 9 | +--- |
| 10 | + |
| 11 | +# User Guide - Java |
| 12 | + |
| 13 | +- [User Guide - Java](#user-guide---java) |
| 14 | + - [Requirements](#requirements) |
| 15 | + - [Installation](#installation) |
| 16 | + - [Build your First Application](#build-your-first-application) |
| 17 | + - [Create a New Project](#create-a-new-project) |
| 18 | + - [Include the Label Recognizer library](#include-the-label-recognizer-library) |
| 19 | + - [Initialize the Label Recognizer](#initialize-the-label-recognizer) |
| 20 | + - [Recognition Process and How to Use the Results](#recognition-process-and-how-to-use-the-results) |
| 21 | + - [Build and Run the Project](#build-and-run-the-project) |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- Operating systems: |
| 26 | + - Windows 7, 8, 10 |
| 27 | + - Windows Server 2003, 2008, 2008 R2, 2012 |
| 28 | + - Linux x64 (Ubuntu 14.04.4+ LTS, Debian 8+, etc.) |
| 29 | + - JDK 1.7 and above |
| 30 | + |
| 31 | +- Environment: Eclipse 3.7 and above. |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +If you don't have SDK yet, please go to <a href="https://www.dynamsoft.com/survey/dlr/?utm_source=docs" target="_blank">Dynamsoft website</a> to get it. After the sdk is decompressed, the root directory of the DLR installation package is `DynamsoftLabelRecognizer`, which is represented by `[INSTALLATION FOLDER]`. |
| 36 | + |
| 37 | +## Build your First Application |
| 38 | + |
| 39 | +Let's start by creating a console application which demonstrates how to use the minimum code to recognize text from an image file. |
| 40 | + |
| 41 | +>You can download the similar complete source code from [Here](https://github.com/Dynamsoft/label-recognizer-java-samples/tree/master/samples/HelloWorld). |
| 42 | +
|
| 43 | +### Create a New Project |
| 44 | + |
| 45 | +1. Open Eclipse. Go to File > New > Project, create a new Java project `DLRJavaSample`. |
| 46 | + |
| 47 | +2. Add a new Class named `DLRJavaSample` into the project. |
| 48 | + |
| 49 | +### Include the Label Recognizer library |
| 50 | + |
| 51 | +1. Add the Dynamsoft Label Recognizer JAR file to your project. |
| 52 | + Click File > Properties > Java Build Path > Libraries > Add external JARs, add `dynamsoft-labelrecognizer-{version number}.jar` and `dynamsoft-core-{version number}.jar` click Apply. |
| 53 | + >Note: The JAR file can be found at `[INSTALLATION FOLDER]\lib`. |
| 54 | +
|
| 55 | +2. Import the package in the file `DLRJavaSample.java` |
| 56 | + |
| 57 | + ```java |
| 58 | + import com.dynamsoft.dlr.*; |
| 59 | + ``` |
| 60 | + |
| 61 | +### Initialize the Label Recognizer |
| 62 | + |
| 63 | +1. Initialize the license key |
| 64 | + |
| 65 | + ```java |
| 66 | + // 1.Initialize license. |
| 67 | + LabelRecognizer.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInByb2R1Y3RzIjoyfQ=="); |
| 68 | + ``` |
| 69 | + |
| 70 | + >Note: |
| 71 | + >- Network connection is required for the license to work. |
| 72 | + >- "DLS2***" is a default free public trial license used in the sample. |
| 73 | + >- You can request a 30-day trial license via the [Request a Trial License](https://www.dynamsoft.com/customer/license/trialLicense?product=dlr&utm_source=guide&package=java){:target="_blank"} link. |
| 74 | + |
| 75 | +2. Create an instance of Dynamsoft Label Recognizer |
| 76 | + |
| 77 | + ```java |
| 78 | + // 2.Create an instance of Label Recognizer. |
| 79 | + LabelRecognizer dlr = new LabelRecognizer(); |
| 80 | + ``` |
| 81 | + |
| 82 | +### Recognition Process and How to Use the Results |
| 83 | + |
| 84 | +1. Recognizing text in an image |
| 85 | + |
| 86 | + ```java |
| 87 | + DLRResult[] results = null; |
| 88 | + |
| 89 | + try { |
| 90 | + results = dlr.recognizeByFile("../../SampleImages/dlr-sample-vin.png", ""); |
| 91 | + } catch (LabelRecognizerException ex) { |
| 92 | + ex.printStackTrace(); |
| 93 | + } |
| 94 | + ``` |
| 95 | + |
| 96 | + >You can download the image [dlr-sample-vin.png](../assets/dlr-sample-vin.png) for testing. In addition, you can replace it with the full path of the image you want to recognize. |
| 97 | + >For the error handling mechanism, when an error occurs during the recognition process, an exception will be thrown. You should add codes for error handling based on your needs. Check out [Error Code]({{site.dlr_enumerations}}error-code.html) for full supported error codes. |
| 98 | + |
| 99 | +2. Get and output the recognition results |
| 100 | + |
| 101 | + ```java |
| 102 | + if (results != null && results.length > 0) { |
| 103 | + for (int i = 0; i < results.length; i++) { |
| 104 | + |
| 105 | + // Get result of each text area (also called label). |
| 106 | + DLRResult result = results[i]; |
| 107 | + System.out.println("Result " + i + ":"); |
| 108 | + for (int j = 0; j < result.lineResults.length; j++) { |
| 109 | + |
| 110 | + // Get the result of each text line in the label. |
| 111 | + DLRLineResult lineResult = result.lineResults[j]; |
| 112 | + System.out.println(">>Line Result " + j + ": " + lineResult.text); |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + System.out.println("No data detected."); |
| 117 | + } |
| 118 | + ``` |
| 119 | + |
| 120 | + The recognition results of SDK are organized into a four-tier structure: |
| 121 | + - `DLRResult[]` corresponds to the results of an `image` |
| 122 | + - `DLRResult` corresponds to the result of a `TextArea` (also called Label) |
| 123 | + - `DLRLineResult` corresponds to the result of each `TextLine` in the Label |
| 124 | + - `DLRCharacterResult` corresponds to the result of each `Character` in the `TextLine` |
| 125 | + |
| 126 | + The structure is shown in the figure below: |
| 127 | + |
| 128 | + <div align="center"> |
| 129 | + <img src="../assets/dlr_result2.png" alt="DLR Result Structure" width="80%"/> |
| 130 | + <p>Figure 1 – DLR Result Structure</p> |
| 131 | + </div> |
| 132 | + |
| 133 | +You can download the similar complete source code from [Here](https://github.com/Dynamsoft/label-recognizer-java-samples/tree/master/samples/HelloWorld). |
| 134 | + |
| 135 | +### Build and Run the Project |
| 136 | + |
| 137 | +1. Right click the project, click Run As > Java Application. |
0 commit comments