private void refreshImage() {
// lazy initialization of service proxy
if (ditherSvc == null) {
ditherSvc = GWT.create(DitherService.class);
}
AsyncCallback<String> callback = new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// do something with errors
}
public void onSuccess(String result) {
img.setUrl("data:image/png;base64," + result);
}
};
// make the call to the dither service
ditherSvc.getDither(steps, spiralDistance, magnitude, callback);
}
On the server side, you need to get a library which is available free for java on the web which encodes a string as base64... an example server side is:
package edu.wiyn.odi.server;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import org.wiyn.odi.op2.application.DitherPatternCoverage;
import org.wiyn.odi.op2.application.op2;
import org.wiyn.odi.op2.otalib.otaMosaic;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import edu.wiyn.odi.client.DitherService;
public class DitherServiceImpl extends RemoteServiceServlet implements
DitherService {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getDither(int steps, double spiralDistance, double magnitude) {
// Read in a png file and make it into a byte array
// Encode this file as a string and pass it to the client
//System.out.println(System.getProperty("user.dir"));
System.out.println("Steps: " + steps + ", spiralDistance: " + spiralDistance + ", magnitude: " + magnitude);
String encoded = null;
try {
// BasicConfigurator.configure ();
DitherPatternCoverage cover = new DitherPatternCoverage(800);
op2.updateCatalog(120., 60.);
Vector<otaMosaic> myDither = cover.generateDitherSequence(120., 60.,
cover.getSpiralDither(steps, spiralDistance));
long start = System.currentTimeMillis();
cover.calculateCoverage(myDither);
long end = System.currentTimeMillis();
System.out.println("rendering took: " + (end - start) + "ms");
cover.writeToDisk("./coveragetest.png");
encoded = Base64.encodeFromFile("./coveragetest.png");
} catch (IOException e) {
e.printStackTrace();
}
return(encoded);
}
}
The main lines are:
cover.writeToDisk("./coveragetest.png");
encoded = Base64.encodeFromFile("./coveragetest.png");
return(encoded);