there is a class

public class Portret implements Serializable{ private Image img; public Image getImg(){ if(img!=null)return img; else img = new Image("..."); return img; } public void setImg(Image im){ img=im; } } 

it needs to be saved to a file, but the Image class (JavaFX) does not implement the Serializable interface, so when you try to save it to a file, you get the error "NotSerializableException" due to the presence of the Image img field. I decided to declare the field as "transient" but I need to save the image in the class instance itself in some form. Tell me how you can circumvent this limitation, or what can you convert an image (with a view to its subsequent restoration) within an instance of a class so that Java allows you to write an object to a file?

  • If anyone knows, you can simply suggest how easy it is to convert an Image into an array of bytes and vice versa. - arachnoden

2 answers 2

You can go different ways like this: If it is allowed to save an image drawn in FX to disk and serialize an object with a link to a saved image, then the short way to save the image is as follows:

 ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", new File("out.png")); 

For example, you can get an array and save it already ... as an example of such a strategy:

  byte[] buffer = new byte[(int) (img.getWidth()*img.getHeight())]; i.getPixelReader().getPixels(0, 0, img.getWidth(),img.getWidth(),PixelFormat.getByteRgbInstance(), buffer, img.getWidth()); 

In general, if a little fantasy, then there are still many options.

UPD: How to create an image from an array (Example! Read the documentation):

  byte[] buffer1 = /** **/; Canvas c = new Canvas(); GraphicsContext gc = c.getGraphicsContext2D(); gc.getPixelWriter().setPixels(0, 0, 10, 10, PixelFormat.getByteRgbInstance(), buffer , 10, 0); 
  • In an external file, you do not need to write a separate image, it should not leave the limits of the object in which it is in the form of a field, and therefore you need to turn it into an array of bytes. - arachnoden

Found, the simplest solution of those that dug. Converts an image into an int array (to be serialized) and back.

 import javafx.scene.image.Image; import javafx.scene.image.PixelReader; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import java.io.Serializable; public class SerializableImage implements Serializable { private int width, height; private int[][] data; public SerializableImage() {} public void setImage(Image image) { width = ((int) image.getWidth()); height = ((int) image.getHeight()); data = new int[width][height]; PixelReader r = image.getPixelReader(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { data[i][j] = r.getArgb(i, j); } } } public Image getImage() { WritableImage img = new WritableImage(width, height); PixelWriter w = img.getPixelWriter(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { w.setArgb(i, j, data[i][j]); } } return img; } }