Java : Get image as a BufferedImage from classpath
<p>I am working on a JavaFX project in which I have to print a few images for which I have the path. Now the problem is, I want to get the image as a BufferedImage and then call the print function which I have. How can I achieve this. Any help would be nice.</p>

<p>Code :</p>

<pre><code>public void printThis(String localPath){

       // What should I do here? 

    System.out.println("Lets print this. ");
}


private void printImage(BufferedImage image) {
    java.awt.print.PrinterJob printJob = java.awt.print.PrinterJob.getPrinterJob();
    printJob.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            // Get the upper left corner that it printable
            int x = (int) Math.ceil(pageFormat.getImageableX());
            int y = (int) Math.ceil(pageFormat.getImageableY());
            if (pageIndex != 0) {
                return NO_SUCH_PAGE;
            }
            graphics.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
            return PAGE_EXISTS;
        }
    });

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                printJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    });
}
</code></pre>

<p>Edit :</p>

<p>Updated print function :</p>

<pre><code>public void printImage(ImageView image) {
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printJob = PrinterJob.createPrinterJob(printer);
        PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        if (printJob != null) {
            boolean success = printJob.printPage(image);
            if (success) {
                printJob.endJob();
            }
        }
    }
</code></pre>

<p>The images are in classpath. Kindly let me know. Thank you. :-)</p>