aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/presence/avatar/imagepicker/WebcamDialog.java
blob: ede148e3c5790cf4ef663d742c38dc87a17a08b6 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.main.presence.avatar.imagepicker;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.service.audionotifier.*;
//import net.java.sip.communicator.service.media.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.swing.*;

/**
 * A dialog showing the webcam and allowing the user to grap a snapshot 
 *
 * @author Damien Roth
 */
public class WebcamDialog
    extends SIPCommDialog
    implements ActionListener
//        , VideoListener
{
    private VideoContainer videoContainer;
    private Component videoComponent = null;
    
    private JButton cancelButton;
    private JButton grabSnapshot;
    
    private byte[] grabbedImage = null;
    
    private TransparentPanel southPanel;
    
    private TransparentPanel timerPanel;
    private TimerImage[] timerImages = new TimerImage[3];
    
    /**
     * Construct a <tt>WebcamDialog</tt>
     * @param parent the ImagePickerDialog
     */
    public WebcamDialog(ImagePickerDialog parent)
    {
        super(parent);
        this.setTitle(GuiActivator.getResources()
                .getI18NString("service.gui.avatar.imagepicker.TAKE_PHOTO"));
        this.setModal(true);
        
        init();
    }

    /**
     * Init the dialog
     */
    private void init()
    {
        this.grabSnapshot = new JButton();
        this.grabSnapshot.setText(GuiActivator.getResources()
                .getI18NString("service.gui.avatar.imagepicker.CLICK"));
        this.grabSnapshot.setName("grab");
        this.grabSnapshot.addActionListener(this);
        this.grabSnapshot.setEnabled(false);

        this.cancelButton = new JButton(GuiActivator.getResources()
                .getI18NString("service.gui.avatar.imagepicker.CANCEL"));
        this.cancelButton.setName("cancel");
        this.cancelButton.addActionListener(this);
        
        initAccessWebcam();


        // Timer Panel
        this.timerPanel = new TransparentPanel();
        this.timerPanel.setLayout(new GridLayout(0, 3));
        
        TransparentPanel tp;
        for (int i = 0; i < this.timerImages.length; i++)
        {
            this.timerImages[i] = new TimerImage("" + (3-i));
            
            tp = new TransparentPanel();
            tp.add(this.timerImages[i], BorderLayout.CENTER);
            
            this.timerPanel.add(tp);
        }
        
        TransparentPanel buttonsPanel = new TransparentPanel(new GridLayout(1, 2));
        buttonsPanel.add(this.grabSnapshot);
        buttonsPanel.add(this.cancelButton);
        
        // South Panel
        this.southPanel = new TransparentPanel(new BorderLayout());
        this.southPanel.add(this.timerPanel, BorderLayout.CENTER);
        this.southPanel.add(buttonsPanel, BorderLayout.SOUTH);
        
        this.add(this.videoContainer, BorderLayout.CENTER);
        this.add(this.southPanel, BorderLayout.SOUTH);   
    }

    /**
     * Init the access to the webcam (asynchonous call)
     */
    private void initAccessWebcam()
    {
        Dimension d = new Dimension(320, 240);
        
        // Create a container for the video
        this.videoContainer = new VideoContainer(new JLabel(
                GuiActivator.getResources()
                .getI18NString("service.gui.avatar.imagepicker.INITIALIZING"),
                JLabel.CENTER));
        this.videoContainer.setPreferredSize(d);
        this.videoContainer.setMinimumSize(d);
        this.videoContainer.setMaximumSize(d);
        
//        try
//        {
//            // Call the method in the media service
//            GuiActivator.getMediaService().createLocalVideoComponent(this);
//        } catch (MediaException e)
//        {
//            //todo: In what scenarios are exceptions thrown and how to manage them?
//            this.videoContainer = new VideoContainer(new JLabel(
//                    GuiActivator.getResources()
//                    .getI18NString("service.gui.avatar.imagepicker.WEBCAM_ERROR")));
//            e.printStackTrace();
//        }
    }
    
    /**
     * Grap the current image of the webcam throught the MediaService
     */
    private void grabSnapshot()
    {
        // Just call the method "grabSnapshot" from the MediaService with the component
//        try
//        {
//            this.grabbedImage = GuiActivator.getMediaService()
//                .grabSnapshot(this.videoComponent);
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
        close(false);
        this.setVisible(false);
    }
    
    /**
     * Return the grabbed snapshot as a byte array
     * 
     * @return the grabbed snapshot
     */
    public byte[] getGrabbedImage()
    {
        return this.grabbedImage;
    }
    
    /**
     * Play a snapshot sound
     */
    private void playSound()
    {
        String soundKey = GuiActivator.getResources()
            .getSoundPath("WEBCAM_SNAPSHOT");
        
        SCAudioClip audio = GuiActivator.getAudioNotifier()
            .createAudio(soundKey);
        
        audio.play();
    }

    protected void close(boolean isEscaped)
    {
//        try
//        {
//            if (this.videoComponent != null)
//            {
//                GuiActivator.getMediaService()
//                    .disposeLocalVideoComponent(this.videoComponent);
//                this.videoComponent = null;
//            }
//        }
//        catch (MediaException e)
//        {
//            // Better manager the exception !
//            e.printStackTrace();
//        }
    }
    
    public void videoAdded(VideoEvent event)
    {
        // Here is the important part. With this event, you get the component
        // containing the video !

        // You need to keep it. The returned componant is the key for all
        // the other methods !
        this.videoComponent = event.getVisualComponent();

        // Add the component in the container
        this.videoContainer.add(this.videoComponent);

        this.grabSnapshot.setEnabled(true);
    }

    public void videoRemoved(VideoEvent event)
    {
        // In case of the video is removed elsewhere
        this.videoComponent = null;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        String actName = ((JButton) e.getSource()).getName();
        
        if (actName.equals("grab"))
        {
            if (this.videoComponent == null)
                return;
            
            this.grabSnapshot.setEnabled(false);
            new SnapshotTimer().start();
        }
        else
        {
            close(false);
            this.setVisible(false);
        }
    }
    
    private class SnapshotTimer
    extends Thread
    {        
        @Override
        public void run()
        {
            int i;
            
            for (i=0; i < timerImages.length; i++)
            {
                timerImages[i].setElapsed();
                try
                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            
            playSound();
            grabSnapshot();
            
            WebcamDialog.this.setVisible(false);
            WebcamDialog.this.dispose();

        }
    }
    
    private class TimerImage
        extends JComponent
    {
        private static final int WIDTH = 30;
        private static final int HEIGHT = 30;
        
        private boolean isElapsed = false;
        private Font textFont = null;
        private String second;
        
        public TimerImage(String second)
        {
            Dimension d = new Dimension(WIDTH, HEIGHT);
            this.setPreferredSize(d);
            this.setMinimumSize(d);
            
            
            this.textFont = new Font("Sans", Font.BOLD, 20);
            this.second = second;
        }
        
        public void setElapsed()
        {
            this.isElapsed = true;
            this.repaint();
        }
        
        @Override
        protected void paintComponent(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            AntialiasingManager.activateAntialiasing(g);
        
            Color c = (isElapsed)
                    ? Color.RED
                    : new Color(150, 0, 0);
        
            g2d.setColor(c);
            g2d.fillOval(0, 0, WIDTH, HEIGHT);
            
            g2d.setColor(Color.WHITE);
            g2d.setFont(textFont);
            g2d.drawString(this.second, 7, 21);
        }
    }
}