zoukankan      html  css  js  c++  java
  • How to convert from BufferedImage to JavaFX 2.2 Image

    http://blog.idrsolutions.com/2012/11/convert-bufferedimage-to-javafx-image/

    ——————————————————————————————————————————————————————————————————

    Until recently, if you wanted to load a BufferedImage in JavaFX you were out of luck – the only way to do it was to write out the BufferedImage to disk and then read it back in as a JavaFX Image.

    But, in August when JavaFX 2.2 was released, the update included a class called WritableImage which extends Image. Along with PixelWriter and PixelReader classes, this gives us greater control over images in JavaFX.

    Using the PixelWriter and the getRGB method of BufferedImage, we are now able to read a BufferedImage into a WritableImage. As WritableImage extends Image, we can now use this however we would an Image!

    Here’s how:

            BufferedImage bf = null;
            try {
                bf = ImageIO.read(new File("C:/location/of/image.png"));
            } catch (IOException ex) {
                System.out.println("Image failed to load.");
            }
     
            WritableImage wr = null;
            if (bf != null) {
                wr = new WritableImage(bf.getWidth(), bf.getHeight());
                PixelWriter pw = wr.getPixelWriter();
                for (int x = 0; x < bf.getWidth(); x++) {
                    for (int y = 0; y < bf.getHeight(); y++) {
                        pw.setArgb(x, y, bf.getRGB(x, y));
                    }
                }
            }
     
            ImageView imView = new ImageView(wr);

     

    Alternatively, you could use the toFXImage method from SwingFXUtils, but where’s the fun in that?

  • 相关阅读:
    Class.getSimpleName()的作用
    win7下制作ubuntu系统安装启动盘和U盘安装ubuntu全过程
    奈奎斯特三大准则
    [转]OFDM中保护间隔和循环前缀抵抗ISI和ICI
    频偏
    移动通信里面,OFDM技术所说的“载波相互正交”是什么意思?
    虚拟盘符映射
    npm 安装与常用命令
    ASP.NET WebAPI HTTPS
    记录下自己写的gulp打包脚本
  • 原文地址:https://www.cnblogs.com/cuizhf/p/3577825.html
Copyright © 2011-2022 走看看