B4J Question How do I draw on a bitmap in a non-ui B4J application?

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Is there any way to draw text on a bitmap in non-ui applications?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi Eme!
Thank you for your response!
Of course it does but I prefer to maintain one single application, so it’s more convenient to do it within the non-ui app!
If it is not possible, then I may prepare the bitmaps having the proper text but it’s not so elegant solution!
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
If the server is not running in high load (less than 1 request per second) then a helper app can be a good solution. You can call it with jShell and pass the output file name.
Hi Erel! Unfortunately the server is heavy loaded, so I can’t use this solution!
Anyway, thank you very much!
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Here's a rough example.
Input : project_path/redsun.jpg
Output: project_path/result.jpg
Hi Frederic!
Your example is what exactly I need! It's working like a charm!
Thank you very very much!!!
I did some minor modifications to draw two lines on the images.
Here is the modified code in case that anyone in the community needs the same functionality!
B4X:
#if Java
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;

    public static void DrawText(String input, String text1, String text2, int sts, int X, int Y, String output) throws Exception
    {
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(input));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

        Graphics graphics = img.getGraphics();
        if (sts == 2) {
            graphics.setColor(Color.BLACK); }
        else {
            graphics.setColor(Color.WHITE);
        }
        
        // Get the FontMetrics
        Font font = new Font("Arial", Font.BOLD, 40);
        FontMetrics metrics = graphics.getFontMetrics(font);
        
        // Determine the X coordinate for the text1
        Rectangle rect = new Rectangle(0,0,img.getWidth(),img.getHeight());
        int x = rect.x + (rect.width - metrics.stringWidth(text1)) / 2;
        
        // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
        int y = rect.y + ((rect.height - metrics.getHeight()) / 4) + metrics.getAscent();
        
        // Set the font
        graphics.setFont(font);
        
        graphics.drawString(text1, x, y);
        
        int x2;
        int y2;
        
        if (!"^".equals(text2)) {
            font = new Font("Arial", Font.BOLD, 32);
            metrics = graphics.getFontMetrics(font);
            
            // Determine the X coordinate for the text2
            x2 = rect.x + (rect.width - metrics.stringWidth(text2)) / 2;
            y2 = y + rect.y + ((rect.height - metrics.getHeight()) / 4) + 15;
            
            graphics.setFont(font);
            graphics.drawString(text2, x2, y2);
        }
        
        ImageIO.write(img, "png", new File(output));
    }
    #End If
 
Upvote 0
Top