Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Created June 20, 2020 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/82e18ef6a74255e4d794bc3fb9a3769e to your computer and use it in GitHub Desktop.
Save dmi3coder/82e18ef6a74255e4d794bc3fb9a3769e to your computer and use it in GitHub Desktop.
Example of avatar generation from Java via C language in Quarkus
package net.quarkify.cpp;
import io.quarkus.runtime.StartupEvent;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import javax.enterprise.event.Observes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.IOException;
import java.net.URL;
@Path("/avatar")
public class AvatarResource {
private Value drawMethod;
public void onStart(@Observes StartupEvent se) {
new Thread() {{
final URL drawImage = getClass().getClassLoader().getResource("hello");
Context polyglot = Context.newBuilder().allowAllAccess(true).build();
Source source = null;
try {
source = Source.newBuilder("llvm", drawImage).build();
} catch (IOException e) {
e.printStackTrace();
}
drawMethod = polyglot.eval(source);
}}.start();
}
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public File uniqueImage() throws IOException {
drawMethod.execute();
return new File("avatar.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment