Wednesday, November 11, 2009

Signature Capturing Applet

Categories:

Hello friends, those who are looking for an Signature Capturing Applet with complete source code including Applet class and servlet class, Check it here...

Applet Class
/**
 *@ Tapas ku. jena
 *mail: tapas.friends@gmail.com
 */

 //SignaptureApp.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.applet.Applet;

public class SignaptureApp extends Applet implements MouseMotionListener ,MouseListener{
    
    int width;
    int height;
    Image img;
    Graphics graph;
    int lock = 0;
    Point p;

    public void init() {

        width = getSize().width;
        height = getSize().height;
        img = createImage(width, height);
        graph = img.getGraphics();
        graph.setColor(Color.BLUE);

        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        
        if (lock == 1) {
            return;
        }
        int x = e.getX();
        int y = e.getY();
        graph.setColor(Color.BLUE);
        graph.drawLine(p.x,p.y,x, y);
        p=e.getPoint();    
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        
    }
    
    @Override
    public void paint(Graphics g) {
        update(g);
    }

    @Override
    public void update(Graphics g) {
        g.drawImage(img, 0, 0,this);
    }

    //This Method save the signeture to a file in to server as an image(png)
    public void sign() {
        BufferedImage bufferedImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        bufferedImage.createGraphics().drawImage(img, 0, 0, this);
        try {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", byteStream);
            byte[] buf = byteStream.toByteArray();
            URL url = new URL("http://localhost:8080/SignApp/UploadPhoto");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setChunkedStreamingMode(1000);
            con.setRequestProperty("Content-Type", "application/octet-stream");
            con.setRequestMethod("POST");
            OutputStream os = con.getOutputStream();
            os.write(buf);
            System.out.println("Saved");
            os.flush();
            os.close();
        } catch (IOException e1) {
            System.out.println("Error during sending file to server");
            e1.printStackTrace();
        }
    }

    public void lock() {
        lock = 1;
    }

    public void unlock() {
        lock = 0;
    }

    public void clear() {
        graph.setColor(Color.white);
        graph.fillRect(0, 0, width, height);
        graph.setColor(Color.black);
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (lock == 1) {
            return;
        }
        int x = e.getX();
        int y = e.getY();
        graph.fillOval(x, y, 3, 3);
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        p=e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

}
Servlet Class

/**
 *@ Tapas ku. jena
 *mail: tapas.friends@gmail.com
 */

 //UploadPhoto.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UploadPhoto extends HttpServlet {
        
 public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException ,                IOException {

    
        ServletContext sc = this.getServletContext();
    
            try {
        
                String path = sc.getRealPath(File.separator)+"Images/image.png";
        
                File yourFile = new File(path);
                FileOutputStream toFile = new FileOutputStream( yourFile );
                BufferedInputStream fromClient = new BufferedInputStream( req.getInputStream() );
                
                int c;
                while( (c = fromClient.read()) != -1) {
                    toFile.write(c);
                    System.out.println("Image Uploading");
                }
                
                System.out.println("Upload Completed....");
                toFile.flush();
                toFile.close();
                fromClient.close();
                
            } catch (Exception e) {
                System.err.println("Error occoured at serverside during upload");
                e.printStackTrace();
            }
        }

 public void doGet(HttpServletRequest request,HttpServletResponse response) throws                              IOException,ServletException {

            doPost(request, response);  
    }
}

if anybody really benefited with this code...Leave a commet.

Spread The Love, Share Our Article

Related Posts

4 Response to Signature Capturing Applet

November 12, 2011 at 1:59 AM

I went to http://www.emuzix.net/capture.php and tried to record file and than save it but nothing has been wrote on my system.
The Applet works in Eclipse but when I run Applet in browser than I can only see Applet and nothing record on system. Y SO ?

January 5, 2014 at 10:25 PM

Hi ,
In applet,how capture the sign .
can you explain how to capture and store it in system.

January 6, 2014 at 9:04 AM

Hi Janardhan...You can drag and draw your signature on mouse.

January 6, 2014 at 11:02 PM

Hi ,
After drawing my signature ,how to save the signature as a image or any other format.
how to use the upload photo class ,in the SignatureApp class you mentioned url
i.e URL url = new URL("http://localhost:8080/SignApp/UploadPhoto");
One more thing there is no jsp or html page , how to run this one on server.
Can you please explain me,

Thanks ,
Janardhan

Post a Comment