Search code examples
javacanvasdrawing2d

Array of points to image


I'm stuck trying to convert an array of points which represent a drawing(simple 2D black lines) taken from a mobile phone. I pass that array of points to a server which then emails that drawing to another person.

I can't find a way to convert that array of points to an image, png, or something else. And then upload it to an static server so I can insert it in an email and send to the other person.

I'm looking something like canvas on android but on the serverside so I can output an image. Preferably Java, but at this point I would take anything.

An ex of the array of points:

        {
            "drawing_objects": [
                [
                    {
                        "x": 177,
                        "y": 246
                    },
                    {
                        "x": 177,
                        "y": 246
                    },
                    {
                        "x": 177,
                        "y": 246
                    }
                ],
                [
                    {
                        "x": 870,
                        "y": 298
                    },
                    {
                        "x": 866.5316,
                        "y": 302.62445
                    }
                ]
            ]
        }

These are two lines, in a drawing, the first with 3 point, the second one with 2.


Solution

  • If you are using a Java based server, then you could create a Graphics2D, draw the points and then export that to PNG or whatever you want.

    import javax.imageio.ImageIO;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    
    //…
    
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    
    Graphics2D ig2 = bi.createGraphics();
    //Draw some lines to the graphic
    ig2.drawLine(x1,y1,x2,y2);
    ig2.drawLine(x2,y2,x3,y3);
    //...
    
    //Export the result to a file
    ImageIO.write(bi, "PNG", new File(“c:\\name.png”));