RenderedImage 填充或绘制 IMAGEVIEW

RenderedImage filling or Drawing a IMAGEVIEW

我正在努力用从数字签名板获得的图像填充 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;
}

此致!!!

你在找

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

SwingFXUtilsjavafx.embed.swing

我通过添加两种方法解决了我的问题,在此Link 谢谢!