Skip to content

Instantly share code, notes, and snippets.

@Sythelux
Created January 8, 2016 11:37
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 Sythelux/9ebd760e2611bbee8592 to your computer and use it in GitHub Desktop.
Save Sythelux/9ebd760e2611bbee8592 to your computer and use it in GitHub Desktop.
shows current Time as realtime gif
@GET
@Path("timeAsGif.gif")
@Produces("image/gif")
public Response timeAsGif() throws IOException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(255);
ImageOutputStream output = ImageIO.createImageOutputStream(bos);
GifSequenceWriter writer = new GifSequenceWriter(output, BufferedImage.TYPE_BYTE_INDEXED, (int) TimeUnit.MINUTES.toMillis(1), false);
Font f = new Font("sans-serif", Font.BOLD, 50);
Color brass = Color.decode("#B5A642");
long cur = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis());
long max = cur + TimeUnit.HOURS.toMinutes(1);
BufferedImage bimg;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Rectangle2D r = f.getStringBounds("14:68", new FontRenderContext(null, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT));
int width = (int) Math.round(r.getWidth());
int height = (int) Math.round(r.getHeight());
int y = (int) Math.round(Math.abs(r.getY()));
for (long i = cur; i < max; i++) {
System.out.println((i - cur) + "/" + (max - cur));
String newStr = sdf.format(new Date(TimeUnit.MINUTES.toMillis(i)));
bimg = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g2d = bimg.createGraphics();
g2d.setFont(f);
g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
g2d.setPaint(brass);
g2d.drawString(newStr, 0, y);
g2d.dispose();
writer.writeToSequence(bimg);
}
byte[] arr = getBytes(output);
writer.close();
output.close();
bos.close();
return Response.ok(arr).expires(new Date(TimeUnit.MINUTES.toMillis(max))).build();
} catch (IOException e) {
Log.warn("/getCurrentTime threw Exception:", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).type(MediaType.TEXT_HTML).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment