java - JavaFX Chart to Image to Base64 String use in PHP -


i'am using javafx , illustrate chart linechart concept in javafx. if draw chart, export screenshot of this code.

writableimage image = lc.snapshot(new snapshotparameters(), null);   file file = new file("chart.png");         try {         imageio.write(swingfxutils.fromfximage(image, null), "png", file); }          catch (ioexception e) {         //bla } 

this works perfect!

now: there simple way create "writableimage" image base64 string? furthermore want use reproduce base64-string png file in php.

any ideas? thx

your code need continue this:

        //file file = new file("chart.png"); -> there         fileinputstream fis = new fileinputstream(file);         bytearrayoutputstream baos = new bytearrayoutputstream();         byte[] buffer = new byte[1024];         int read= 0;         while( (read = fis.read(buffer)) > -1){              baos.write(buffer, 0, read);         }         fis.close();         baos.close();         byte pgnbytes [] = baos.tobytearray();         base64.encoder base64_enc = base64.getencoder();         string base64_image = base64_enc.encodetostring(pgnbytes); 

this can optimized further write file directly byte array, if not need graphic stored file:

            writableimage image = lc.snapshot(new snapshotparameters(), null);           bytearrayoutputstream baos = new bytearrayoutputstream();          try {                 imageio.write(swingfxutils.fromfximage(image, null), "png", baos);         }                  catch (ioexception e) {                 //bla         }          byte pgnbytes [] = baos.tobytearray();         base64.encoder base64_enc = base64.getencoder();         string base64_image = base64_enc.encodetostring(pgnbytes);      } 

in both cases image stored in memory, can cause outofmemory error, if image large etc.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -