'Is there a way to use Tesseract OCR for Java without specifying the path of the image?

I am currently creating a spring boot application that allows the user to store a database of books to organize and search throughout. I have added a feature within the addition portion of the application that uses Tesseract OCR to scan an image of a book cover to extract as much information as it can so the user can copy paste the title and author over to the addition text boxes. I want this to be used by users from their own computers but I'm not sure how to set up the File system so that it can read from the input source instead of the code being changed. Code below...

bookScanner.java

import java.io.File;

import net.sourceforge.tess4j.*;

public class bookScanner {
    
    public String booksScanner() {
        File imageFile = new File("\path\to\file\book.jpg");


        //Change Datapath based off of computer
        ITesseract instance = new Tesseract();
        instance.setDatapath("\path\to\file");
        
        try {
            String result = instance.doOCR(imageFile);
            return result;
        }
        
        catch (TesseractException e) {
            System.err.println(e.getMessage());
            return "";
        }
    }
}

File imageFile = new File("\path\to\file"); This snippet right here is the problem. I don't want this to be hardcoded because a foreign user may have the image labeled as BookCover and it might be a png or jpeg and it won't scan.

FIXED I used a file reading system to access and read the newest submitted file and it all works as intended now if anyone has troubles with this later on, here is the following code...

    File folder = new File("\path\to\file");
    File[] listOfFiles = folder.listFiles();
    long lastModifiedTime = Long.MIN_VALUE;
    File chosenFile = null;
    
    if (listOfFiles != null) {
        for (File file: listOfFiles) {  
            if (file.lastModified() > lastModifiedTime) {
                chosenFile = file;
                lastModifiedTime = file.lastModified();
                if (chosenFile.isFile()) {
                    chosenFile = new File("\path\to\file" + file.getName());
                    System.out.println(chosenFile);
                }
            }
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source