RenderedImage填充或绘制IMAGEVIEW

时间:2015-01-23 10:47:19

标签: java image-processing javafx

我正在努力用JavaFX中的ImageView填充从数字签名平板电脑获得的图像。它总是来自RenderedImage类型,我无法执行此任务。还试图转换RenderedImage BufferedImage,但也没有成功。我如何能?这是我的代码:

    @Override
  public void initialize(URL arg0, ResourceBundle arg1) {

//  drawpane.setPrefHeight(tabletHeight);
//  drawpane.setPrefWidth(tabletWidth);
//  pnlSignature.getChildren().add(drawpane);

    JSTUTablet.addPenPressedHandler((PenPressedHandler) new DemoPenPressHandler(pnlSignature));

    zhandleSetImage();
    start();

// Here is the focus of the problem
    try {

//Here is a method that returns a BufferedImage of my obtained the device RenderedImage.
      BufferedImage buf = convertRenderedImage(JSTUTablet.getImage());

      ImageView image = new ImageView();
      image.setFitHeight(tabletHeight);
      image.setFitWidth(tabletWidth);
      ByteArrayOutputStream os = new ByteArrayOutputStream();

      ImageIO.write(buf, "png", os);
      image.setImage(new Image(new ByteArrayInputStream(os.toByteArray())));

      pnlSignature.getChildren().add(image);

    } catch (InvalidOperationException | IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }


//Method convertRenderedImage
public BufferedImage convertRenderedImage(RenderedImage img) {
  if (img instanceof BufferedImage) {
      return (BufferedImage) img;
  }
  ColorModel cm = img.getColorModel();
  int width = img.getWidth();
  int height = img.getHeight();
  WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
  boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
  Hashtable properties = new Hashtable();
  String[] keys = img.getPropertyNames();
  if (keys != null) {
      for (int i = 0; i < keys.length; i++) {
          properties.put(keys[i], img.getProperty(keys[i]));
      }
  }
  BufferedImage result = new BufferedImage(cm, raster,isAlphaPremultiplied, properties);
  img.copyData(raster);
  return result;
}

问候!!!

2 个答案:

答案 0 :(得分:0)

您在寻找

吗?
image.setImage(SwingFXUtils.toFXImage(buf, null));

SwingFXUtils位于javafx.embed.swing

答案 1 :(得分:0)

我在这个Link中添加两个方法来解决我的问题 谢谢!