Convert JavaFX Image object to byte array -


we can create fx image object using

byte [] bytes = ------; //valid image in bytes javafx.scene.image.image image = new image(new bytearrayinputstream(bytes)); 

and can set imageview.

i need opposite without converting first bufferedimage (swingfxutils.fromfximage(image,null)).

so can directly write bytes file.

i've tried following:

pixelreader pixelreader = image.getpixelreader();     int width = (int)image.getwidth();     int height = (int)image.getheight();     byte[] buffer = new byte[width * height * 4];     pixelreader.getpixels(             0,             0,             width,             height,             pixelformat.getbytebgrainstance(),             buffer,             0,             width * 4     ); 

but file generated writing byte [] buffer not valid image.

any insights on this?

edit: solutions given @ how byte[] javafx imageview cannot applied question. i've mentioned not want use converting bufferedimage using swingfxutils. moreover, want convert byte array can written image file.

this way later, if wanting find out, try this:

// load image java fx image object //  image img = new image(new fileinputstream("someimagefile.png") );  // cache width , height 'int's (because getwidth/getheight return double) , getpixels needs 'int's //  int w = (int)img.getwidth(); int h = (int)img.getheight();  // create new byte buffer, we'll use bgra (1 byte each channel) //  byte[] buf = new byte[w * h * 4];  /* since can output in whatever format writablepixelformat,    we'll use created 1 ease-of-use. */  img.getpixelreader().getpixels(0, 0, w, h, pixelformat.getbytebgrainstance(), buf, 0, w * 4);  /* second last parameter byte offset want start in buffer,    , last parameter stride (in bytes) per line buffer. */ 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -