Commit a41c54a7 authored by Drahomír Karchňák's avatar Drahomír Karchňák

WIP Added new AbstractImageGenerator and ForumBannerGenerator. Refactored bits...

WIP Added new AbstractImageGenerator and ForumBannerGenerator. Refactored bits of export image generators.
parent 6ccf6c31
package at.alladin.rmbt.statisticServer.export;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.text.FieldPosition;
import java.text.Format;
import java.util.Date;
import java.util.Locale;
import javax.imageio.ImageIO;
import at.alladin.rmbt.shared.SignificantFormat;
public abstract class AbstractImageGenerator {
private static final String TEMPLATES_PATH = "templates/";
private static final String FONT_PATH = "fonts/";
private static final String FALLBACK_FONT_NAME = "Droid Sans";
/**
* Generates a image for showing the user its speed test result
* @param lang: Language of the image, currently either 'de' or 'en'
* @param upload: Upload speed in mbps
* @param download: Download speed in mbps
* @param ping: Ping in ms
* @param isp: ISP name
* @param typ: Test type (LAN, 3G, 4G, etc.)
* @param signal: Signal strength in dbm
* @param os: Plattform used for conducting the test (Android, IOS, Applet, Browser)
* @return rendered image
*/
public abstract BufferedImage generateImage(
String lang,
double upload,
double download,
double ping,
String isp,
String typ,
String signal,
String os,
Date date
) throws IOException;
protected BufferedImage loadTemplateImage(String name) throws IOException {
return ImageIO.read(super.getClass().getResourceAsStream(TEMPLATES_PATH + name));
}
protected Font loadFont(String name) {
try {
InputStream fontInputStream = super.getClass().getResourceAsStream(FONT_PATH + name);
return Font.createFont(Font.TRUETYPE_FONT, fontInputStream);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
System.out.println("Couldn't load font w/ name: " + name + ". Using fallback font!");
return new Font(FALLBACK_FONT_NAME, Font.PLAIN, 1);
}
}
/**
* Formats a number to 2 significant digits
* @param number the number
* @return the formatted number
*/
protected String formatNumber(double number, String lang) {
final Locale locale = new Locale(lang);
final Format format = new SignificantFormat(2, locale);
final StringBuffer buf = format.format(number, new StringBuffer(), new FieldPosition(0));
return buf.toString();
}
protected void drawCenteredString(String s, int x, int y, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
x += (w - fm.stringWidth(s)) / 2;
y += (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
}
package at.alladin.rmbt.statisticServer.export;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by bcx on 1/30/18.
*/
public class ForumBannerGenerator extends AbstractImageGenerator {
private static final int BANNER_WIDTH = 560;
private static final int BANNER_HEIGHT = 300;
private static final String TEMPLATE_FORMAT = "forumbanner_%s.png";
private static final String PRIMARY_FONT = "Roboto-Light.ttf";
private static final String SECONDARY_FONT = "Roboto-Medium.ttf";
private Font primaryFont = null;
private Font secondaryFont = null;
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os, Date time) throws IOException {
//Initial setup of final image
BufferedImage finalImage = new BufferedImage(BANNER_WIDTH, BANNER_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D finalImageGraphics = finalImage.createGraphics();
finalImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
prepareFonts();
renderTemplate(lang, finalImageGraphics);
renderMainValue(formatNumber(download, lang), 93, 132, finalImageGraphics);
renderMainValue(formatNumber(upload, lang), 280, 132, finalImageGraphics);
renderMainValue(formatNumber(ping, lang), 469, 132, finalImageGraphics);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
renderAdditionalValue(isp, 151, 246, finalImageGraphics);
// renderAdditionalValue(typ, 151, 278, finalImageGraphics);
renderAdditionalValue(simpleDateFormat.format(time), 151, 278, finalImageGraphics); //TODO
renderAdditionalValue(signal, 432, 246, finalImageGraphics);
renderAdditionalValue(os, 432, 278, finalImageGraphics);
return finalImage;
}
private void prepareFonts() {
primaryFont = loadFont(PRIMARY_FONT);
secondaryFont = loadFont(SECONDARY_FONT);
}
private void renderTemplate(String language, Graphics2D graphics) {
try {
BufferedImage templateImage = loadTemplateImage(String.format(TEMPLATE_FORMAT, language));
graphics.drawImage(templateImage, null, 0, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
private void renderMainValue(String value, int centerX, int centerY, Graphics2D graphics) {
graphics.setColor(Color.WHITE);
Font font = primaryFont.deriveFont(80f);
graphics.setFont(font);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int width = fontMetrics.stringWidth(value);
int height = fontMetrics.getHeight();
graphics.drawString(value, centerX - width/2, centerY + height/2);
}
private void renderAdditionalValue(String value, int posX, int posY, Graphics2D graphics) {
if(value == null || value.isEmpty()) value = "n/a";
graphics.setColor(Color.WHITE);
Font font = primaryFont.deriveFont(18f);
graphics.setFont(font);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int height = fontMetrics.getHeight();
graphics.drawString(value, posX, posY + height/2);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment