2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-9 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucedemo_headers.h"
31 //==============================================================================
32 class CameraDemo
: public Component
,
33 public ComboBoxListener
,
34 public ButtonListener
,
35 public CameraDevice::Listener
38 //==============================================================================
40 : cameraSelectorComboBox ("Camera"),
41 snapshotButton ("Take a snapshot"),
42 recordMovieButton ("Record a movie file (to your desktop)..."),
43 recordingMovie (false)
47 addAndMakeVisible (&cameraSelectorComboBox
);
48 createListOfCameras();
49 cameraSelectorComboBox
.setSelectedId (1);
50 cameraSelectorComboBox
.addListener (this);
52 addAndMakeVisible (&snapshotButton
);
53 snapshotButton
.addListener (this);
54 snapshotButton
.setEnabled (false);
56 addAndMakeVisible (&recordMovieButton
);
57 recordMovieButton
.addListener (this);
58 recordMovieButton
.setEnabled (false);
60 cameraSelectorComboBox
.setSelectedId (2);
67 void paint (Graphics
& g
)
69 g
.drawImageWithin (lastSnapshot
,
70 getWidth() / 2 + 10, 40,
71 getWidth() / 2 - 20, getHeight() - 50,
72 RectanglePlacement::centred
, false);
77 cameraSelectorComboBox
.setBounds (10, 4, 250, 24);
78 snapshotButton
.changeWidthToFitText (24);
79 snapshotButton
.setTopLeftPosition (cameraSelectorComboBox
.getRight() + 20, 4);
80 recordMovieButton
.changeWidthToFitText (24);
81 recordMovieButton
.setTopLeftPosition (snapshotButton
.getRight() + 20, 4);
83 if (cameraPreviewComp
!= 0)
84 cameraPreviewComp
->setBounds (10, 40, getWidth() / 2 - 20, getHeight() - 50);
87 void comboBoxChanged (ComboBox
*)
89 // This is called when the user chooses a camera from the drop-down list.
91 cameraPreviewComp
= 0;
92 recordingMovie
= false;
94 if (cameraSelectorComboBox
.getSelectedId() > 1)
96 // Try to open the user's choice of camera..
97 cameraDevice
= CameraDevice::openDevice (cameraSelectorComboBox
.getSelectedId() - 2);
99 // and if it worked, create a preview component for it..
100 if (cameraDevice
!= 0)
101 addAndMakeVisible (cameraPreviewComp
= cameraDevice
->createViewerComponent());
104 snapshotButton
.setEnabled (cameraDevice
!= 0);
105 recordMovieButton
.setEnabled (cameraDevice
!= 0);
109 void createListOfCameras()
111 cameraSelectorComboBox
.clear();
112 cameraSelectorComboBox
.addItem ("No camera", 1);
113 cameraSelectorComboBox
.addSeparator();
115 StringArray cameras
= CameraDevice::getAvailableDevices();
117 for (int i
= 0; i
< cameras
.size(); ++i
)
118 cameraSelectorComboBox
.addItem (cameras
[i
], i
+ 2);
121 void buttonClicked (Button
* b
)
123 if (cameraDevice
!= 0)
125 if (b
== &recordMovieButton
)
127 // The user has clicked the record movie button..
128 if (! recordingMovie
)
130 // Start recording to a file on the user's desktop..
131 recordingMovie
= true;
133 File
file (File::getSpecialLocation (File::userDesktopDirectory
)
134 .getNonexistentChildFile ("JuceCameraDemo",
135 CameraDevice::getFileExtension()));
137 cameraDevice
->startRecordingToFile (file
);
138 recordMovieButton
.setButtonText ("Stop Recording");
142 // Already recording, so stop...
143 recordingMovie
= false;
144 cameraDevice
->stopRecording();
145 recordMovieButton
.setButtonText ("Start recording (to a file on your desktop)");
150 // When the user clicks the snapshot button, we'll attach ourselves to
151 // the camera as a listener, and wait for an image to arrive...
152 cameraDevice
->addListener (this);
157 // This is called by the camera device when a new image arrives
158 void imageReceived (const Image
& image
)
160 // In this app we just want to take one image, so as soon as this happens,
161 // we'll unregister ourselves as a listener.
162 if (cameraDevice
!= 0)
163 cameraDevice
->removeListener (this);
165 // This callback won't be on the message thread, so need to lock it before using
166 // data that may already be in use..
167 const MessageManagerLock mm
;
168 lastSnapshot
= image
;
173 //==============================================================================
174 ScopedPointer
<CameraDevice
> cameraDevice
;
175 ScopedPointer
<Component
> cameraPreviewComp
;
178 ComboBox cameraSelectorComboBox
;
179 TextButton snapshotButton
;
180 TextButton recordMovieButton
;
185 //==============================================================================
186 Component
* createCameraDemo()
188 return new CameraDemo();