2016-01-15 48 views
0

我正在开发一个JavaFX项目,在该项目中,我必须打印几张图像,以便获取路径。现在的问题是,我想把图像作为一个BufferedImage,然后调用我所拥有的打印功能。我怎样才能做到这一点。你能帮忙的话,我会很高兴。Java:从类路径获取图像作为BufferedImage

代码:

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(); 
      } 
     } 
    }); 
} 

编辑:

更新打印功能:

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(); 
      } 
     } 
    } 

的图像是在类路径中。请让我知道。谢谢。 :-)

+0

你能否详细说明“图像在类路径中”以及在这种情况下'localPath'参数是什么?你的意思是你将图像与你为应用程序创建的jar文件捆绑在一起? –

+0

@James_D:我的项目架构是src - > Main.java(这里是打印函数),src包含目录资源 - > img - > abc.png。现在我想打印这个abc.png –

+0

因此,图像相对于类路径被部署为'/ img/abc.png'或'/ resources/img/abc.png'?那是('“/img/abc.png”或'“/resources/img/abc.png”')作为'localPath'传入的值吗? –

回答

1

我不太清楚是什么参数包含,但其基本思想是

public void printThis(String localPath) { 
    try { 
     InputStream in = getClass().getResourceAsStream(localPath); 
     BufferedImage image = ImageIO.read(in); 
     printImage(image); 
    } catch (Exception exc) { 
     exc.printStackTrace(); 
     // handle elegantly... 
    } 
} 

这是假设localPath包含资源的名称定义here,或者相对于当前类(但是请注意,路径的所有组件必须是有效的Java标识符,因此不是..等),或者是绝对的(即相对于类路径),如果它开始/

但是,由于您正在编写JavaFX项目,因此使用JavaFX API代替它肯定更有意义。还要注意,没有必要在FX应用程序线程上进行打印,并且可能不建议。所以你可以做

public void printThis(String localPath) { 

    // note you can use overloaded forms of the Image constructor 
    // if you want to scale, etc 
    Image image = new Image(getClass().getResource(localPath).toExternalForm()); 
    new Thread(() -> printImage(image)).start(); 
} 

public void printImage(Image image) { 
    ImageView imageView = new ImageView(image); 
    PrinterJob printJob = PrinterJob.createPrinterJob(); 
    if (printJob != null) { 
     // scale image if necessary by using imageView.setPreserveRatio(true) 
     // along with imageView.setFitWidth(...) and imageView.setFitHeight(...) 
     boolean success = printJob.printPage(imageView); 
     if (success) { 
      printJob.endJob(); 
     } 
    } 
} 
+0

图像仅适用于前25%,我想打印整个图像。你知道我必须改变printImage()方法吗? –

+0

你是什么意思?该页面的图片太大了吗? –

+0

不,上面的方法是将图像从顶部缩放到25%。所以,如果将图像分成4部分,只有一部分被打印。我认为printImage代码是这样做的。使用graphics.DrawImage()。 –