• 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?

  • 相关阅读:
    ionic3-ng4学习见闻--(aot方式打包问题解决方案)
    ionic3-ng4学习见闻--(轮播图完美方案)
    ionic3-ng4学习见闻--(多语言方案)
    死也要上npm 而不是cnpm苟且偷生
    2017埙箫简谱清单分享(附音频Demo)
    《好久不见》(Cover 陈奕迅)箫声远 洞箫
    React-onsenui之RouterNavigator组件解读
    基于3ds Max的游戏建模方案
    重拾SQL——从无到有
    重拾SQL——表中索值
  • 原文地址:https://www.cnblogs.com/cuizhf/p/3577825.html
Copyright © 2020-2023  润新知