Syed Asad Abbas Zaidi Syed Asad Abbas Zaidi - 3 months ago 9
Android Question

Android: Create an image from template + form data

In my project I'm using a simple form fields such as image,name,company,venue,datetime which are being entered by the user. But before entering their details I provide template choice section to the user which contains three predefined invitation card templates(image).
I want all the details of the user that was filled, to be embed into the invitation card template(image) that he selected and lastly I could show a final image containing all the details entered and that too on the invitation card template.

Answer
//template is the card template, strings is the details, x and y are the locations of the strings.
//The top left corner of the image is (0, 0), and the bottom right is (template.width, template.height)
private static Bitmap makeDetailedBitmap(final Bitmap template, final String[] strings, final float[] x, final float[] y)
{
    Bitmap result = Bitmap.createBitmap(template.getWidth(), template.getHeight(), template.getConfig());   //Create the base image
    Canvas canvas = new Canvas(result);   //Create a canvas so we can draw onto the base image
    canvas.drawBitmap(template, 0, 0, null);   //Draw the template

    //EDIT: Forgot to set the text paint
    Paint p = new Paint();
    //p.setTextAlign(Paint.Align.CENTER);   //Uncomment if you want centered text
    p.setTextSize(12);   //Change as you please
    p.setColor(Color.BLACK);   //Change as you please

    final int len = strings.length; //Assumes that the length of x and y are >= the length of strings
    for (int i = 0; i < len; i++)
    {
        canvas.drawText(strings[i], x[i], y[i], p);  //Draws text onto image
    }
    return result;   //Returns image that was modified using the canvas (aka image with details)
}

So just call this for each template you want.