saving graphics to PNG using ggbApplet.writePNGtoFile

schnortz shared this question 1 month ago
Answered

I am enjoying the coincidence of my question with regards to the top topic in "All Topics".


So I am trying to use JavaScript to export a batch of PNG graphics to a Windows machine. The program is being run on GeoGebra Classic 5.0.604.0-d (which is my preferred method for creating).


As of now, I am using a button to advance through loop "i" . Names are being auto-generated into a variable "filename" which pull the info from text "txt". I was also going to have a directory set via text "dir".


var i = ggbApplet.getValue('i');
var stop = ggbApplet.getValue('i_{stop}');
i = (i + 1) % stop;
ggbApplet.setValue('i', i);
//var dir = ggbApplet.getValueString('dir', true) + '\\\\';
var filename = ggbApplet.getValueString("txt", true) + '.png';
ggbApplet.setTextValue("cmdtxt", filename);
var cmdSuccess = ggbApplet.writePNGtoFile(filename, 1, false, 300);
ggbApplet.setValue("cmd", cmdSuccess ? 1 : 0);


Issue #1:

However, when I use the line... (based on what I'm reading in Reference:GeoGebra Apps API )

var cmdSuccess = ggbApplet.writePNGtoFile(filename, 1, false, 300);

I am receiving the following error:

Error in JavaScript

Can't find method org.geogebra.desktop.plugin.GgbAPID.writePNGtoFile(org.mozilla.javascript.ConsString,number,boolean,number).


Suggestions? Work arounds?


Follow-Up#2:

If I do get that to work, may I assume I may need to declare my ActiveView first using

var viewSuccess = ggbApplet.evalCommand("SetActiveView(1)");


Thanks in advance.


var i = ggbApplet.getValue('i');
var stop = ggbApplet.getValue('i_{stop}');
i = (i + 1) % stop;
ggbApplet.setValue('i', i);
var dir = ggbApplet.getValueString('dir', true) + '\\\\';
var filename = dir + ggbApplet.getValueString("txt", true) + '.png';
ggbApplet.setTextValue("cmdtxt", filename);
//var cmd = 'ExportImage("filename", "' + filename + '", "type", "png", "view", 1, "width", 700)';
//ggbApplet.setTextValue("cmdtxt", cmd);
//var cmdSuccess = ggbApplet.evalCommand(cmd);
var cmdSuccess = ggbApplet.writePNGtoFile(filename, 1, false, 300);
ggbApplet.setValue("cmd", cmdSuccess ? 1 : 0);

Best Answer

Comments (5)

photo
1

Try passing more parameters

 public synchronized boolean writePNGtoFile(String filename,
final double exportScale, final boolean transparent,
final double DPI0, final boolean greyscale) {


If you use the HTML5 version then you can add them all to a single .zip easily - see https://help.geogebra.org/t...

photo
1

With so many different flavors of Geogebra out there, there may have been some confusion.


I redid the code to keep it simple knowing I would be prompted each time to save.


The JavaScript version...


var i = ggbApplet.getValue('i');
var filename = ggbApplet.getValueString("filename", true) + '.png';
var cmdSuccess = ggbApplet.writePNGtoFile(filename, 1, false, 300);

var stop = ggbApplet.getValue('i_{stop}');
i = (i + 1) % stop;
ggbApplet.setValue('i', i);


The GeoGebra script version...

ExportImage("type", "png", "filename", filename + ".png", "view", 1, "width", 700)
SetValue(i, Mod(i+1, i_{stop})) 


I always begin by building my apps with...

GeoGebra Classic 5.0.604.0-d (08 September 2020)

Java: 1.8.0_161

Codebase: file:/C:/Users/SPLLC/AppData/Roaming/GeoGebra%205.0/jars/current/

OS: Windows 10

Architecture: x86 / x86

Heap: 247MB

CAS: CAS Initializing

(Windows Install... thus offline and not run through a browser)


JavaScript Issue:

Error in JavaScript

Can't find method org.geogebra.desktop.plugin.GgbAPID.writePNGtoFile(org.mozilla.javascript.ConsString,number,boolean,number).


GeoGebra Script Issue:

No prompt to save file, nor does it save file automatically. (Or I just don't know where to find it.) Also nothing is stored in the clipboard. It does however advance the counter i by one.

If I replace my first line with...


ExportImage("type", "png","view", 1, "width", 700)

it does work, but I now manually need to enter the name which defeats the purpose of the script.


If nothing else, I can get both to work in Geogebra 6.06.604.0-offline (08 September 2020). However, I feel limited in some regards as I can pop-out the Settings Windows and use the tab key to move between options.

photo
1

Did you actually bother to try this and see what it does?


https://test.geogebra.org/~...

photo
1

While I was initially writing my response, I had been experimenting & tweaking with the code / various programs for almost two hours (I write texts methodically to avoid misinterpretations) to provide as much insight as possible. Therefore, your second response starting with "Updated..." never appeared within this topic until AFTER I wrote my return response and then refreshed my screen.


So no, I had not seen it, because it was not available at the time. Now seeing it and tried it, your examples did generate a batched zip of PNG or SVG files when I pulled it up in the browser.


However... the concerns that I brought in my second response are still there.

  • Geogebra 5 is not recognizing .writePNGtoFile even though the Reference:GeoGebra Apps API says it should since 4.0.
  • And for some reason, GeoGebra 5 ExportImage will also not produce a prompt for me to save with a dynamic or static name. During this time, I have also tried

    ExportImage("type", "png", "filename", "image.png", "view", 1, "width", 700)

    with no success.

Comments have been locked on this page!

© 2020 International GeoGebra Institute