diff --git a/.checkstyle b/.checkstyle
deleted file mode 100644
index 75246d3..0000000
--- a/.checkstyle
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
diff --git a/.gitignore b/.gitignore
index 0aae8cb..ced41cd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,6 @@
.idea
.project
.log
-.settings/
target/
build/
bin/
diff --git a/build.gradle b/build.gradle
index 4c6e2b8..790604b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,10 +2,14 @@ plugins {
id 'org.springframework.boot' version '3.2.4'
id 'java'
id 'idea'
+ id "com.diffplug.spotless" version "6.25.0"
}
apply plugin: 'io.spring.dependency-management'
+apply plugin: 'com.diffplug.spotless'
+
+
group = 'com.tess4j'
version = '1.4'
sourceCompatibility = '21'
@@ -36,6 +40,8 @@ dependencies {
//rest-assured
testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '5.4.0'
+ //spotless
+ implementation group: 'com.diffplug.spotless', name: 'spotless-plugin-gradle', version: '6.25.0'
}
sourceSets {
@@ -49,6 +55,17 @@ sourceSets {
}
}
+spotless {
+ java {
+ importOrder()
+ removeUnusedImports()
+ cleanthat()
+ googleJavaFormat()
+ formatAnnotations()
+ licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
+ }
+}
+
archivesBaseName = 'ocr-tess4j-rest'
jar {
diff --git a/src/main/java/com/tess4j/rest/Tess4jV1.java b/src/main/java/com/tess4j/rest/Tess4jV1.java
index d8a4c16..3017836 100644
--- a/src/main/java/com/tess4j/rest/Tess4jV1.java
+++ b/src/main/java/com/tess4j/rest/Tess4jV1.java
@@ -1,25 +1,17 @@
-/**
- * Copyright @ 2013 Arun Gopalpuri
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+/* (C) 2013 */
package com.tess4j.rest;
import com.tess4j.rest.model.Image;
import com.tess4j.rest.model.Status;
import com.tess4j.rest.model.Text;
import com.tess4j.rest.mongo.ImageRepository;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
+import javax.imageio.ImageIO;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.codec.binary.Base64;
@@ -32,98 +24,103 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
-import javax.imageio.ImageIO;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.commons.io.FileUtils;
-import java.net.URL;
-import java.net.URLConnection;
-
@SpringBootApplication
@RestController
public class Tess4jV1 {
- private Logger LOGGER = LoggerFactory.getLogger(Tess4jV1.class);
+ private Logger LOGGER = LoggerFactory.getLogger(Tess4jV1.class);
- @Autowired
- private ImageRepository repository;
+ @Autowired private ImageRepository repository;
- @RequestMapping(value = "ocr/ping", method = RequestMethod.GET)
- public Status ping() throws Exception {
- return new Status("OK");
- }
+ @RequestMapping(value = "ocr/ping", method = RequestMethod.GET)
+ public Status ping() throws Exception {
+ return new Status("OK");
+ }
- @RequestMapping(value = "ocr/v1/convert", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
- public Text convertImageToText(@RequestBody final Image image) throws Exception {
+ @RequestMapping(
+ value = "ocr/v1/convert",
+ method = RequestMethod.POST,
+ consumes = MediaType.APPLICATION_JSON_VALUE,
+ produces = MediaType.APPLICATION_JSON_VALUE)
+ public Text convertImageToText(@RequestBody final Image image) throws Exception {
- File tmpFile = File.createTempFile("ocr_image", image.getExtension());
- try {
- FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
- Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
- String imageText = tesseract.doOCR(tmpFile);
- LOGGER.debug("OCR Image Text = " + imageText);
- return new Text(imageText);
- } catch (Exception e) {
- LOGGER.error("Exception while converting/uploading image: ", e);
- throw new TesseractException();
- } finally {
- tmpFile.delete();
- }
+ File tmpFile = File.createTempFile("ocr_image", image.getExtension());
+ try {
+ FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
+ Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
+ String imageText = tesseract.doOCR(tmpFile);
+ LOGGER.debug("OCR Image Text = " + imageText);
+ return new Text(imageText);
+ } catch (Exception e) {
+ LOGGER.error("Exception while converting/uploading image: ", e);
+ throw new TesseractException();
+ } finally {
+ tmpFile.delete();
}
+ }
- @RequestMapping(value = "ocr/v1/convert", method = RequestMethod.GET)
- public Text convertImageToText(@RequestParam String url, @RequestParam(defaultValue = "png") String extension) throws Exception {
- File tmpFile = File.createTempFile("ocr_image", "." + extension);
- try {
- URLConnection conn = new URL(url).openConnection();
- conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
- conn.connect();
- FileUtils.copyInputStreamToFile(conn.getInputStream(), tmpFile);
- Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
- String imageText = tesseract.doOCR(tmpFile);
- LOGGER.debug("OCR Image Text = " + imageText);
- return new Text(imageText);
- } catch (Exception e) {
- LOGGER.error("Exception while converting/uploading image: ", e);
- throw new TesseractException();
- } finally {
- tmpFile.delete();
- }
+ @RequestMapping(value = "ocr/v1/convert", method = RequestMethod.GET)
+ public Text convertImageToText(
+ @RequestParam String url, @RequestParam(defaultValue = "png") String extension)
+ throws Exception {
+ File tmpFile = File.createTempFile("ocr_image", "." + extension);
+ try {
+ URLConnection conn = new URL(url).openConnection();
+ conn.setRequestProperty(
+ "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
+ conn.connect();
+ FileUtils.copyInputStreamToFile(conn.getInputStream(), tmpFile);
+ Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
+ String imageText = tesseract.doOCR(tmpFile);
+ LOGGER.debug("OCR Image Text = " + imageText);
+ return new Text(imageText);
+ } catch (Exception e) {
+ LOGGER.error("Exception while converting/uploading image: ", e);
+ throw new TesseractException();
+ } finally {
+ tmpFile.delete();
}
+ }
- @RequestMapping(value = "ocr/v1/upload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
- public Status doOcr(@RequestBody Image image) throws Exception {
- try {
- //FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
- ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(image.getImage()));
- Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
- String imageText = tesseract.doOCR(ImageIO.read(bis));
- image.setText(imageText);
- repository.save(image);
- LOGGER.debug("OCR Result = " + imageText);
- } catch (Exception e) {
- LOGGER.error("TessearctException while converting/uploading image: ", e);
- throw new TesseractException();
- }
-
- return new Status("success");
+ @RequestMapping(
+ value = "ocr/v1/upload",
+ method = RequestMethod.POST,
+ consumes = MediaType.APPLICATION_JSON_VALUE,
+ produces = MediaType.APPLICATION_JSON_VALUE)
+ public Status doOcr(@RequestBody Image image) throws Exception {
+ try {
+ // FileUtils.writeByteArrayToFile(tmpFile, Base64.decodeBase64(image.getImage()));
+ ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(image.getImage()));
+ Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
+ String imageText = tesseract.doOCR(ImageIO.read(bis));
+ image.setText(imageText);
+ repository.save(image);
+ LOGGER.debug("OCR Result = " + imageText);
+ } catch (Exception e) {
+ LOGGER.error("TessearctException while converting/uploading image: ", e);
+ throw new TesseractException();
}
- @RequestMapping(value = "ocr/v1/images/users/{userId}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
- public List getUserImages(@PathVariable String userId) throws Exception {
- List userImages = new ArrayList<>();
- try {
- userImages = repository.findByUserId(userId);
- } catch (Exception e) {
- LOGGER.error("Exception occurred finding image for userId: {} ", userId, e);
- throw new Exception();
- }
- return userImages;
- }
+ return new Status("success");
+ }
- public static void main(String[] args) {
- SpringApplication.run(Tess4jV1.class, args);
+ @RequestMapping(
+ value = "ocr/v1/images/users/{userId}",
+ method = RequestMethod.GET,
+ consumes = MediaType.APPLICATION_JSON_VALUE,
+ produces = MediaType.APPLICATION_JSON_VALUE)
+ public List getUserImages(@PathVariable String userId) throws Exception {
+ List userImages = new ArrayList<>();
+ try {
+ userImages = repository.findByUserId(userId);
+ } catch (Exception e) {
+ LOGGER.error("Exception occurred finding image for userId: {} ", userId, e);
+ throw new Exception();
}
+ return userImages;
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(Tess4jV1.class, args);
+ }
}
diff --git a/src/main/java/com/tess4j/rest/model/Image.java b/src/main/java/com/tess4j/rest/model/Image.java
index b0f00cc..31023ae 100644
--- a/src/main/java/com/tess4j/rest/model/Image.java
+++ b/src/main/java/com/tess4j/rest/model/Image.java
@@ -1,55 +1,55 @@
+/* (C) 2024 */
package com.tess4j.rest.model;
import org.springframework.data.annotation.Id;
public class Image {
- @Id
- private String id;
+ @Id private String id;
- private String userId;
+ private String userId;
- private byte[] image;
+ private byte[] image;
- private String extension;
+ private String extension;
- private String text;
+ private String text;
- public String getUserId() {
- return userId;
- }
+ public String getUserId() {
+ return userId;
+ }
- public void setUserId(String userId) {
- this.userId = userId;
- }
+ public void setUserId(String userId) {
+ this.userId = userId;
+ }
- public byte[] getImage() {
- return image;
- }
+ public byte[] getImage() {
+ return image;
+ }
- public void setImage(byte[] image) {
- this.image = image;
- }
+ public void setImage(byte[] image) {
+ this.image = image;
+ }
- public String getExtension() {
- return extension;
- }
+ public String getExtension() {
+ return extension;
+ }
- public void setExtension(String extension) {
- this.extension = extension;
- }
+ public void setExtension(String extension) {
+ this.extension = extension;
+ }
- public String getText() {
- return text;
- }
+ public String getText() {
+ return text;
+ }
- public void setText(String text) {
- this.text = text;
- }
+ public void setText(String text) {
+ this.text = text;
+ }
- @Override
- public String toString() {
- return String
- .format("Image[userId=%s, image='%s', extenstion='%s', text='%s']", userId, image, extension, text);
- }
+ @Override
+ public String toString() {
+ return String.format(
+ "Image[userId=%s, image='%s', extenstion='%s', text='%s']", userId, image, extension, text);
+ }
}
diff --git a/src/main/java/com/tess4j/rest/model/Status.java b/src/main/java/com/tess4j/rest/model/Status.java
index 364e3cc..f2a1b82 100644
--- a/src/main/java/com/tess4j/rest/model/Status.java
+++ b/src/main/java/com/tess4j/rest/model/Status.java
@@ -1,21 +1,19 @@
+/* (C) 2024 */
package com.tess4j.rest.model;
public class Status {
- /**
- * The status message
- */
- private String message;
+ /** The status message */
+ private String message;
- public Status(String message) {
- this.message = message;
- }
+ public Status(String message) {
+ this.message = message;
+ }
- public String getMessage() {
- return message;
- }
+ public String getMessage() {
+ return message;
+ }
- public void setMessage(String message) {
- this.message = message;
- }
+ public void setMessage(String message) {
+ this.message = message;
+ }
}
-
diff --git a/src/main/java/com/tess4j/rest/model/Text.java b/src/main/java/com/tess4j/rest/model/Text.java
index 8a59775..e5acfab 100644
--- a/src/main/java/com/tess4j/rest/model/Text.java
+++ b/src/main/java/com/tess4j/rest/model/Text.java
@@ -1,18 +1,19 @@
+/* (C) 2024 */
package com.tess4j.rest.model;
public class Text {
- private String text;
+ private String text;
- public Text(String text) {
- this.text = text;
- }
+ public Text(String text) {
+ this.text = text;
+ }
- public String getText() {
- return text;
- }
+ public String getText() {
+ return text;
+ }
- public void setText(String text) {
- this.text = text;
- }
+ public void setText(String text) {
+ this.text = text;
+ }
}
diff --git a/src/main/java/com/tess4j/rest/mongo/ImageRepository.java b/src/main/java/com/tess4j/rest/mongo/ImageRepository.java
index 6e24e3e..2e84992 100644
--- a/src/main/java/com/tess4j/rest/mongo/ImageRepository.java
+++ b/src/main/java/com/tess4j/rest/mongo/ImageRepository.java
@@ -1,13 +1,11 @@
+/* (C) 2024 */
package com.tess4j.rest.mongo;
import com.tess4j.rest.model.Image;
-import org.springframework.data.mongodb.repository.MongoRepository;
-
import java.util.List;
+import org.springframework.data.mongodb.repository.MongoRepository;
public interface ImageRepository extends MongoRepository {
- List findByUserId(String userId);
-
-
+ List findByUserId(String userId);
}
diff --git a/src/test/java/com/tess4j/rest/Tess4jV1SmokeTest.java b/src/test/java/com/tess4j/rest/Tess4jV1SmokeTest.java
index 8a7899c..6aff879 100644
--- a/src/test/java/com/tess4j/rest/Tess4jV1SmokeTest.java
+++ b/src/test/java/com/tess4j/rest/Tess4jV1SmokeTest.java
@@ -1,71 +1,97 @@
+/* (C) 2024 */
package com.tess4j.rest;
+import static io.restassured.RestAssured.given;
+
import com.tess4j.rest.model.Image;
+import io.restassured.response.ResponseBody;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import io.restassured.response.ResponseBody;
-import static io.restassured.RestAssured.given;
-
@Disabled
public class Tess4jV1SmokeTest {
- @Test
- public void testHealth() {
- ResponseBody> body = given().when().get("http://localhost:8080/ocr/ping");
- System.out.println(body.asString());
- }
+ @Test
+ public void testHealth() {
+ ResponseBody> body = given().when().get("http://localhost:8080/ocr/ping");
+ System.out.println(body.asString());
+ }
- @Test
- public void convertImageToText() throws IOException {
- Map headers = new HashMap();
- headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
- headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
+ @Test
+ public void convertImageToText() throws IOException {
+ Map headers = new HashMap();
+ headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
+ headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
- Image image = new Image();
- InputStream inputStream = ClassLoader.getSystemResourceAsStream("eurotext.png");
- image.setUserId("arun0009");
- image.setExtension(".png");
- image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
- String response = given().contentType("application/json").headers(headers).body(image).when().post("http://localhost:8080/ocr/v1/convert").then()
- .statusCode(200).extract().response().body().asString();
- System.out.println(response);
- }
+ Image image = new Image();
+ InputStream inputStream = ClassLoader.getSystemResourceAsStream("eurotext.png");
+ image.setUserId("arun0009");
+ image.setExtension(".png");
+ image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
+ String response =
+ given()
+ .contentType("application/json")
+ .headers(headers)
+ .body(image)
+ .when()
+ .post("http://localhost:8080/ocr/v1/convert")
+ .then()
+ .statusCode(200)
+ .extract()
+ .response()
+ .body()
+ .asString();
+ System.out.println(response);
+ }
- @Test
- public void testDoOcr() throws IOException {
- Map headers = new HashMap();
- headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
- headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
+ @Test
+ public void testDoOcr() throws IOException {
+ Map headers = new HashMap();
+ headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
+ headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
- Image image = new Image();
- InputStream inputStream = ClassLoader.getSystemResourceAsStream("eurotext.png");
- image.setUserId("arun0009");
- image.setExtension(".png");
- image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
- String response = given().contentType("application/json").headers(headers).body(image).when().post("http://localhost:8080/ocr/v1/upload").then()
- .statusCode(200).extract().response().body().asString();
- System.out.println(response);
- }
+ Image image = new Image();
+ InputStream inputStream = ClassLoader.getSystemResourceAsStream("eurotext.png");
+ image.setUserId("arun0009");
+ image.setExtension(".png");
+ image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
+ String response =
+ given()
+ .contentType("application/json")
+ .headers(headers)
+ .body(image)
+ .when()
+ .post("http://localhost:8080/ocr/v1/upload")
+ .then()
+ .statusCode(200)
+ .extract()
+ .response()
+ .body()
+ .asString();
+ System.out.println(response);
+ }
- @Test
- public void testGetUserImages() throws IOException {
- Map headers = new HashMap();
- headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
- headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
+ @Test
+ public void testGetUserImages() throws IOException {
+ Map headers = new HashMap();
+ headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
+ headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
- String response = given().contentType("application/json").headers(headers).when()
- .pathParam("userId", "arun0009").get("http://localhost:8080/ocr/v1/images/users/{userId}")
+ String response =
+ given()
+ .contentType("application/json")
+ .headers(headers)
+ .when()
+ .pathParam("userId", "arun0009")
+ .get("http://localhost:8080/ocr/v1/images/users/{userId}")
.asString();
- System.out.println(response);
- }
-
+ System.out.println(response);
+ }
}