Signature Capturing Applet

Home  >>  Java  >>  Signature Capturing Applet

Signature Capturing Applet

3
Sep,2016

0
  Java

Hello friends, those who are looking for a Signature Capturing Applet with complete source code including Applet class and Servlet class, Check it out…

Applet Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 //SignatureApp.java
 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.applet.Applet;
 
/**
 *@author: Tapas Jena
 *@mail: tapas.friends@gmail.com
 */
public class SignatureApp 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 //UploadPhoto.java
 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
/**
 *@author: Tapas Jena
 *@mail: tapas.friends@gmail.com
 */
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);  
    }
}
Follow Me On:

Leave a Reply

Your email address will not be published. Required fields are marked *