Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

How to convert from BufferedImage to JavaFX 2.2 Image

49 sec read

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?

This article was inspired by converting the PageFlow mode in out Java PDF Viewer from Java3D to JavaFX. I have written several articles on converting our Java3D usage into JavaFX and you can read the other articles here.

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to write WebP images in Java

In this article, I will walk you through how to write out images as WebP images in Java. ImageIO does not support WebP images...
Mark Stephens
1 min read

How to convert EMF files to SVG in Java…

This article shows you how to convert EMF files into SVG files using our JDeli Java Image library. What is EMF? EMF is a...
Mark Stephens
1 min read

One Reply to “How to convert from BufferedImage to JavaFX 2.2 Image”

  1. I have a problem I want to send a webcam image for sokets in javafx and I do not know how I put the code to see if you can help me

    Server:

    @Override
    public void run() {

    try {
    ss = new ServerSocket(puerto);
    System.out.println(“Escuchando…”);

    //Cam cam;
    while(true) {
    s = ss.accept();
    System.out.println(“Conectado”);

    final float FACTOR = 4f;

    BufferedImage bf = ImageIO.read(s.getInputStream());
    //ImageIO.write(bufferedImage,”png”, new FileOutputStream(“/home/leyer/image.png”));
    System.out.println(“Image received!”);

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

    scanner.setGraphic(new ImageView(wr));

    s.close();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    Cliente:

    Thread hiloWebcam = new Thread() {
    @Override
    public void run() {
    // inicializamos webcam
    Webcam webcam = Webcam.getDefault();
    webcam.setViewSize(WebcamResolution.VGA.getSize());

    WebcamPanel panel = new WebcamPanel(webcam);
    panel.setFPSDisplayed(true);
    panel.setDisplayDebugInfo(true);
    panel.setImageSizeDisplayed(true);
    panel.setMirrored(true);
    // la insertamos al panel
    swingNode.setContent(panel);

    try {

    while (true)
    {
    s = new Socket(ip, puerto);
    ImageIO.write(webcam.getImage(), "png", s.getOutputStream());
    s.getOutputStream().flush();
    }

    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    };
    hiloWebcam.setDaemon(true);
    hiloWebcam.start();

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2020. All rights reserved.