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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
==============================================================================
This file is part of the JUCE examples.
Copyright (c) 2022 - Raw Material Software Limited
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: CameraDemo
version: 1.0.0
vendor: JUCE
website: http://juce.com
description: Showcases camera features.
dependencies: juce_audio_basics, juce_audio_devices, juce_core, juce_cryptography,
juce_data_structures, juce_events, juce_graphics, juce_gui_basics,
juce_gui_extra, juce_video
exporters: xcode_mac, vs2022, androidstudio, xcode_iphone
moduleFlags: JUCE_USE_CAMERA=1, JUCE_STRICT_REFCOUNTEDPOINTER=1
type: Component
mainClass: CameraDemo
useLocalCopy: 1
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
#include "../Assets/DemoUtilities.h"
//==============================================================================
class CameraDemo : public Component
{
public:
CameraDemo()
{
setOpaque (true);
#if JUCE_ANDROID
// Android requires exclusive access to the audio device when recording videos.
audioDeviceManager.closeAudioDevice();
#endif
addAndMakeVisible (cameraSelectorComboBox);
updateCameraList();
cameraSelectorComboBox.setSelectedId (1);
cameraSelectorComboBox.onChange = [this] { cameraChanged(); };
addAndMakeVisible (snapshotButton);
snapshotButton.onClick = [this] { takeSnapshot(); };
snapshotButton.setEnabled (false);
addAndMakeVisible (recordMovieButton);
recordMovieButton.onClick = [this] { startRecording(); };
recordMovieButton.setEnabled (false);
addAndMakeVisible (lastSnapshot);
cameraSelectorComboBox.setSelectedId (2);
setSize (500, 500);
#if JUCE_IOS || JUCE_ANDROID
setPortraitOrientationEnabled (true);
#endif
}
~CameraDemo() override
{
#if JUCE_IOS || JUCE_ANDROID
setPortraitOrientationEnabled (false);
#endif
#if JUCE_ANDROID
audioDeviceManager.restartLastAudioDevice();
#endif
}
//==============================================================================
void paint (Graphics& g) override
{
g.fillAll (Colours::black);
}
void resized() override
{
auto r = getLocalBounds().reduced (5);
auto top = r.removeFromTop (25);
cameraSelectorComboBox.setBounds (top.removeFromLeft (250));
r.removeFromTop (4);
top = r.removeFromTop (25);
snapshotButton.changeWidthToFitText (24);
snapshotButton.setBounds (top.removeFromLeft (snapshotButton.getWidth()));
top.removeFromLeft (4);
recordMovieButton.changeWidthToFitText (24);
recordMovieButton.setBounds (top.removeFromLeft (recordMovieButton.getWidth()));
r.removeFromTop (4);
auto previewArea = shouldUseLandscapeLayout() ? r.removeFromLeft (r.getWidth() / 2)
: r.removeFromTop (r.getHeight() / 2);
if (cameraPreviewComp.get() != nullptr)
cameraPreviewComp->setBounds (previewArea);
if (shouldUseLandscapeLayout())
r.removeFromLeft (4);
else
r.removeFromTop (4);
lastSnapshot.setBounds (r);
}
private:
//==============================================================================
// if this PIP is running inside the demo runner, we'll use the shared device manager instead
#ifndef JUCE_DEMO_RUNNER
AudioDeviceManager audioDeviceManager;
#else
AudioDeviceManager& audioDeviceManager { getSharedAudioDeviceManager (0, 2) };
#endif
std::unique_ptr<CameraDevice> cameraDevice;
std::unique_ptr<Component> cameraPreviewComp;
ImageComponent lastSnapshot;
ComboBox cameraSelectorComboBox { "Camera" };
TextButton snapshotButton { "Take a snapshot" };
#if ! JUCE_ANDROID && ! JUCE_IOS
TextButton recordMovieButton { "Record a movie (to your desktop)..." };
#else
TextButton recordMovieButton { "Record a movie" };
#endif
bool recordingMovie = false;
File recordingFile;
bool contentSharingPending = false;
void setPortraitOrientationEnabled (bool shouldBeEnabled)
{
auto allowedOrientations = Desktop::getInstance().getOrientationsEnabled();
if (shouldBeEnabled)
allowedOrientations |= Desktop::upright;
else
allowedOrientations &= ~Desktop::upright;
Desktop::getInstance().setOrientationsEnabled (allowedOrientations);
}
bool shouldUseLandscapeLayout() const noexcept
{
#if JUCE_ANDROID || JUCE_IOS
auto orientation = Desktop::getInstance().getCurrentOrientation();
return orientation == Desktop::rotatedClockwise || orientation == Desktop::rotatedAntiClockwise;
#else
return false;
#endif
}
void updateCameraList()
{
cameraSelectorComboBox.clear();
cameraSelectorComboBox.addItem ("No camera", 1);
cameraSelectorComboBox.addSeparator();
auto cameras = CameraDevice::getAvailableDevices();
for (int i = 0; i < cameras.size(); ++i)
cameraSelectorComboBox.addItem (cameras[i], i + 2);
}
void cameraChanged()
{
// This is called when the user chooses a camera from the drop-down list.
#if JUCE_IOS
// On iOS, when switching camera, open the new camera first, so that it can
// share the underlying camera session with the old camera. Otherwise, the
// session would have to be closed first, which can take several seconds.
if (cameraSelectorComboBox.getSelectedId() == 1)
cameraDevice.reset();
#else
cameraDevice.reset();
#endif
cameraPreviewComp.reset();
recordingMovie = false;
if (cameraSelectorComboBox.getSelectedId() > 1)
{
#if JUCE_ANDROID || JUCE_IOS
openCameraAsync();
#else
cameraDeviceOpenResult (CameraDevice::openDevice (cameraSelectorComboBox.getSelectedId() - 2), {});
#endif
}
else
{
snapshotButton .setEnabled (cameraDevice != nullptr && ! contentSharingPending);
recordMovieButton.setEnabled (cameraDevice != nullptr && ! contentSharingPending);
resized();
}
}
void openCameraAsync()
{
SafePointer<CameraDemo> safeThis (this);
CameraDevice::openDeviceAsync (cameraSelectorComboBox.getSelectedId() - 2,
[safeThis] (CameraDevice* device, const String& error) mutable
{
if (safeThis)
safeThis->cameraDeviceOpenResult (device, error);
});
}
void cameraDeviceOpenResult (CameraDevice* device, const String& error)
{
// If camera opening worked, create a preview component for it..
cameraDevice.reset (device);
if (cameraDevice.get() != nullptr)
{
#if JUCE_ANDROID
SafePointer<CameraDemo> safeThis (this);
cameraDevice->onErrorOccurred = [safeThis] (const String& cameraError) mutable { if (safeThis) safeThis->errorOccurred (cameraError); };
#endif
cameraPreviewComp.reset (cameraDevice->createViewerComponent());
addAndMakeVisible (cameraPreviewComp.get());
}
else
{
AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon, "Camera open failed",
"Camera open failed, reason: " + error);
}
snapshotButton .setEnabled (cameraDevice.get() != nullptr && ! contentSharingPending);
recordMovieButton.setEnabled (cameraDevice.get() != nullptr && ! contentSharingPending);
resized();
}
void startRecording()
{
if (cameraDevice.get() != nullptr)
{
// The user has clicked the record movie button..
if (! recordingMovie)
{
// Start recording to a file on the user's desktop..
recordingMovie = true;
#if JUCE_ANDROID || JUCE_IOS
recordingFile = File::getSpecialLocation (File::tempDirectory)
#else
recordingFile = File::getSpecialLocation (File::userDesktopDirectory)
#endif
.getNonexistentChildFile ("JuceCameraVideoDemo", CameraDevice::getFileExtension());
#if JUCE_ANDROID
// Android does not support taking pictures while recording video.
snapshotButton.setEnabled (false);
#endif
cameraSelectorComboBox.setEnabled (false);
cameraDevice->startRecordingToFile (recordingFile);
recordMovieButton.setButtonText ("Stop Recording");
}
else
{
// Already recording, so stop...
recordingMovie = false;
cameraDevice->stopRecording();
#if ! JUCE_ANDROID && ! JUCE_IOS
recordMovieButton.setButtonText ("Start recording (to a file on your desktop)");
#else
recordMovieButton.setButtonText ("Record a movie");
#endif
cameraSelectorComboBox.setEnabled (true);
#if JUCE_ANDROID
snapshotButton.setEnabled (true);
#endif
#if JUCE_CONTENT_SHARING
URL url (recordingFile);
snapshotButton .setEnabled (false);
recordMovieButton.setEnabled (false);
contentSharingPending = true;
SafePointer<CameraDemo> safeThis (this);
juce::ContentSharer::getInstance()->shareFiles ({url},
[safeThis] (bool success, const String&) mutable
{
if (safeThis)
safeThis->sharingFinished (success, false);
});
#endif
}
}
}
void takeSnapshot()
{
SafePointer<CameraDemo> safeThis (this);
cameraDevice->takeStillPicture ([safeThis] (const Image& image) mutable { safeThis->imageReceived (image); });
}
// This is called by the camera device when a new image arrives
void imageReceived (const Image& image)
{
if (! image.isValid())
return;
lastSnapshot.setImage (image);
#if JUCE_CONTENT_SHARING
auto imageFile = File::getSpecialLocation (File::tempDirectory).getNonexistentChildFile ("JuceCameraPhotoDemo", ".jpg");
FileOutputStream stream (imageFile);
if (stream.openedOk()
&& JPEGImageFormat().writeImageToStream (image, stream))
{
URL url (imageFile);
snapshotButton .setEnabled (false);
recordMovieButton.setEnabled (false);
contentSharingPending = true;
SafePointer<CameraDemo> safeThis (this);
juce::ContentSharer::getInstance()->shareFiles ({url},
[safeThis] (bool success, const String&) mutable
{
if (safeThis)
safeThis->sharingFinished (success, true);
});
}
#endif
}
void errorOccurred (const String& error)
{
AlertWindow::showMessageBoxAsync (MessageBoxIconType::InfoIcon,
"Camera Device Error",
"An error has occurred: " + error + " Camera will be closed.");
cameraDevice.reset();
cameraSelectorComboBox.setSelectedId (1);
snapshotButton .setEnabled (false);
recordMovieButton.setEnabled (false);
}
void sharingFinished (bool success, bool isCapture)
{
AlertWindow::showMessageBoxAsync (MessageBoxIconType::InfoIcon,
isCapture ? "Image sharing result" : "Video sharing result",
success ? "Success!" : "Failed!");
contentSharingPending = false;
snapshotButton .setEnabled (true);
recordMovieButton.setEnabled (true);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CameraDemo)
};
|