Skip to content

Commit

Permalink
using spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Gopalpuri committed Mar 29, 2024
1 parent 5c06f68 commit 52ac988
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 217 deletions.
7 changes: 0 additions & 7 deletions .checkstyle

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.idea
.project
.log
.settings/
target/
build/
bin/
Expand Down
17 changes: 17 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand All @@ -49,6 +55,17 @@ sourceSets {
}
}

spotless {
java {
importOrder()
removeUnusedImports()
cleanthat()
googleJavaFormat()
formatAnnotations()
licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
}
}

archivesBaseName = 'ocr-tess4j-rest'

jar {
Expand Down
185 changes: 91 additions & 94 deletions src/main/java/com/tess4j/rest/Tess4jV1.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Image> getUserImages(@PathVariable String userId) throws Exception {
List<Image> 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<Image> getUserImages(@PathVariable String userId) throws Exception {
List<Image> 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);
}
}
70 changes: 35 additions & 35 deletions src/main/java/com/tess4j/rest/model/Image.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 12 additions & 14 deletions src/main/java/com/tess4j/rest/model/Status.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

Loading

0 comments on commit 52ac988

Please sign in to comment.