Convert email address to image with Java code

Author Joe Date one year ago
Views 356 Reviews 0
Rating Favoured
0

Given an email adress email2image@gmail.com, it will convert the adress to the image 

  • EmailImage

    package org.ostree.utils;
    import javax.imageio.ImageIO;
    
    import java.awt.*;
    import java.awt.font.FontRenderContext;
    import java.awt.font.GlyphVector;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    
    public class EmailImage {
        public static void main(String[] args) throws IOException {
            long start = System.currentTimeMillis();
            String email = "email2image@gmail.com";
            BufferedImage image = renderWord(email, 200, 20);
            ImageIO.write(image, "png", new File("/home/joe/email2image.png"));
            long l = System.currentTimeMillis() - start;
            System.out.println(l);
        }
    
        public static BufferedImage renderWord(String word, int width, int height) {
            int fontSize = 14;
            width=10*word.length();
            Font[] fonts = new Font[]{
                    new Font("Arial", Font.BOLD, fontSize), new Font("Courier", Font.BOLD, fontSize)
            };
            Color color = Color.BLACK;
            int charSpace = 3;
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2D = image.createGraphics();
            g2D.setColor(color);
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            hints.add(new RenderingHints(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY));
            g2D.setRenderingHints(hints);
    
            FontRenderContext frc = g2D.getFontRenderContext();
    
            Random random = new Random();
    
            int startPosY = (height - fontSize) / 5 + fontSize;
    
            char[] wordChars = word.toCharArray();
            Font[] chosenFonts = new Font[wordChars.length];
            int[] charWidths = new int[wordChars.length];
            int widthNeeded = 0;
            for (int i = 0; i < wordChars.length; i++) {
                chosenFonts[i] = fonts[random.nextInt(fonts.length)];
    
                char[] charToDraw = new char[]{
                        wordChars[i]
                };
                GlyphVector gv = chosenFonts[i].createGlyphVector(frc, charToDraw);
                charWidths[i] = (int) gv.getVisualBounds().getWidth();
                if (i > 0) {
                    widthNeeded = widthNeeded + 2;
                }
                widthNeeded = widthNeeded + charWidths[i];
            }
    
            int startPosX = (width - widthNeeded) / 2;
            for (int i = 0; i < wordChars.length; i++) {
                g2D.setFont(chosenFonts[i]);
                char[] charToDraw = new char[]{
                        wordChars[i]
                };
                g2D.drawChars(charToDraw, 0, charToDraw.length, startPosX, startPosY);
                startPosX = startPosX + (int) charWidths[i] + charSpace;
            }
    
            return image;
        }
    }
    

0 comments

Your rating:


Back to reviews top | Back to top
© OSTree(Open Source Tree Community) | About OSTree | Advertisement | Feedback