Link to home
Start Free TrialLog in
Avatar of Joe
JoeFlag for United States of America

asked on

Draw JPEG From Plot Points

I have an applet that I found to do signature capture.  The applet takes x,y values of the lines and puts them into a hidden param on the form, which then needs to be submitted to something on the server to write the image.


http://www.lawrencegoetz.com/programs/signature/

There are 2 sets of X,Y corridinates per line, 1 for the start of the line and one for the end.  Can somone give me some direction on what I should use to accomplish this, and some sample code if possible?  I am looking to do this with a servlet.

Thanks,

Joe
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The easiest way to do it is to take the Graphics context of the component in the applet that's capturing the signature and make an image out of it

BufferedImage bi = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
component.paint(g);


Then, using a technique like the following:

http://www.javaworld.com/javaworld/javatips/jw-javatip34.html

with the following variation:

javax.imageio.ImageIO.write(bi, "PNG", printout);

you can make the applet POST to a servlet or script on the server it was loaded from

Avatar of Joe

ASKER

Unfortuently I dont have the source code for the applet to be able to do this.  Is there something else I d\could do that would work with the applets current functionality?

Thanks
Perhaps you meant that you're looking for something to plot graphs? If so http://www.jfree.org/jfreechart/ could be used
Avatar of Joe

ASKER

Perhaps I have not phrased my question correctly.

I have found an existing applet, that I do not have the code for, which will capture a signature.  The applet provides a JavaScript function to grab the x,y coordinates for all of the lines in the signature.  Each line has 4 coordinates.  The x,y start point and the x,y end point.  I need to pass these into something that will draw these lines out into a JPEG.  The JPEG would not exist yet, and would be created as a new image.

I was looking at the java.awt.Graphics2D class, but I am not sure if it is what I really need.  I can not figure out how to instate it.  From what I can tell it may just be for canvas rendering.  

I am not looking to draw a graph.  I am looking to put the signature back together from the X,Y coordinates which came from the applet.  I then want the servlet to perminetly save the signature into a JPEG.

Thanks for the help so far.  I hope this gives a better understanding of what I am looking for.

Joe
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joe

ASKER

That was exactly what I was looking for.  Thanks a million.

Joe
no worries :)
Avatar of Joe

ASKER

In case anyone is looking to do this...I thought I would post out the code.  The applet to capture the signature can be found in the link on my first post.

/*
 * Created on Apr 10, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package Test;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zepernick
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DrawJpeg extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
              
        draw(request,response);
       
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
              draw(request,response);

    }
   
    private void draw(HttpServletRequest req,HttpServletResponse resp){
       
        // Write generated image to a file
        try {
            RenderedImage rendImage = myCreateImage(req.getParameter("signature"));
            File file = null;
            // Save as JPEG
            file = new File("c:/newimage.jpg");
            ImageIO.write(rendImage, "jpg", file);
        } catch (Exception e) {
            e.printStackTrace();
        }        
       
    }
   
    // Returns a generated image.
    public RenderedImage myCreateImage(String cords) throws Exception{
        int width = 500;
        int height = 300;
        String[] cordsArray = null;
   
        // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   
        // Create a graphics contents on the buffered image
        Graphics2D g2d = bufferedImage.createGraphics();
   
        // Draw graphics
     
               
        g2d.setBackground(Color.WHITE);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.black);
       
        //get an array of cordinates.  Each line has 4 cordinats
        //Each 4 numbers represent (x1, y1) , (x2, y2) for a line.
        //these are the coordinates that came from the applet
     
       
        cordsArray = cords.split("\\ ");
       
        if (cordsArray.length % 4 != 0){
            //not divisable by 4 we have a problem obtaining the coordinates
            throw new Exception("INVALID NUMBER OF COORDINATES.  SHOULD DIVIDE BY 4");
        }
       
        for (int i = 0; i < cordsArray.length; i = i + 4){
            g2d.drawLine(Integer.parseInt(cordsArray[i]),
                          Integer.parseInt(cordsArray[i + 1]),
                          Integer.parseInt(cordsArray[i + 2]),
                          Integer.parseInt(cordsArray[i + 3]));
        }
       
 
        // Graphics context no longer needed so dispose it
        g2d.dispose();
   
        return bufferedImage;
    }


}
JoeZ430, the accepted answer does not differ from what i posted orginally - can you explain?
Avatar of Joe

ASKER

Cehj,

You wanted me to make changes in the Applet which I did not have the source code to, and then use a URLConnection to somehow send it over.  This was not the solution I was looking for, since I did not have the source to the applet, which I tried to explain later on in my post.  Thanks for trying to help, but this was not a solution that would work for me.


Joe