The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: CameraDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases camera features.
  24. dependencies: juce_core, juce_cryptography, juce_data_structures, juce_events,
  25. juce_graphics, juce_gui_basics, juce_gui_extra, juce_video
  26. exporters: xcode_mac, vs2017, linux_make
  27. moduleFlags: JUCE_USE_CAMERA=1
  28. type: Component
  29. mainClass: CameraDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class CameraDemo : public Component,
  37. private CameraDevice::Listener,
  38. private AsyncUpdater
  39. {
  40. public:
  41. CameraDemo()
  42. {
  43. setOpaque (true);
  44. addAndMakeVisible (cameraSelectorComboBox);
  45. updateCameraList();
  46. cameraSelectorComboBox.setSelectedId (1);
  47. cameraSelectorComboBox.onChange = [this] { cameraChanged(); };
  48. addAndMakeVisible (snapshotButton);
  49. snapshotButton.onClick = [this] { takeSnapshot(); };
  50. snapshotButton.setEnabled (false);
  51. addAndMakeVisible (recordMovieButton);
  52. recordMovieButton.onClick = [this] { startRecording(); };
  53. recordMovieButton.setEnabled (false);
  54. addAndMakeVisible (lastSnapshot);
  55. cameraSelectorComboBox.setSelectedId (2);
  56. setSize (500, 500);
  57. }
  58. //==============================================================================
  59. void paint (Graphics& g) override
  60. {
  61. g.fillAll (Colours::black);
  62. }
  63. void resized() override
  64. {
  65. auto r = getLocalBounds().reduced (5);
  66. auto top = r.removeFromTop (25);
  67. cameraSelectorComboBox.setBounds (top.removeFromLeft (250));
  68. r.removeFromTop (4);
  69. top = r.removeFromTop (25);
  70. snapshotButton.changeWidthToFitText (24);
  71. snapshotButton.setBounds (top.removeFromLeft (snapshotButton.getWidth()));
  72. top.removeFromLeft (4);
  73. recordMovieButton.changeWidthToFitText (24);
  74. recordMovieButton.setBounds (top.removeFromLeft (recordMovieButton.getWidth()));
  75. r.removeFromTop (4);
  76. auto previewArea = r.removeFromTop (r.getHeight() / 2);
  77. if (cameraPreviewComp.get() != nullptr)
  78. cameraPreviewComp->setBounds (previewArea);
  79. r.removeFromTop (4);
  80. lastSnapshot.setBounds (r);
  81. }
  82. private:
  83. //==============================================================================
  84. ScopedPointer<CameraDevice> cameraDevice;
  85. ScopedPointer<Component> cameraPreviewComp;
  86. ImageComponent lastSnapshot;
  87. ComboBox cameraSelectorComboBox { "Camera" };
  88. TextButton snapshotButton { "Take a snapshot" };
  89. TextButton recordMovieButton { "Record a movie (to your desktop)..." };
  90. bool recordingMovie = false;
  91. void updateCameraList()
  92. {
  93. cameraSelectorComboBox.clear();
  94. cameraSelectorComboBox.addItem ("No camera", 1);
  95. cameraSelectorComboBox.addSeparator();
  96. auto cameras = CameraDevice::getAvailableDevices();
  97. for (int i = 0; i < cameras.size(); ++i)
  98. cameraSelectorComboBox.addItem (cameras[i], i + 2);
  99. }
  100. void cameraChanged()
  101. {
  102. // This is called when the user chooses a camera from the drop-down list.
  103. cameraDevice .reset();
  104. cameraPreviewComp.reset();
  105. recordingMovie = false;
  106. if (cameraSelectorComboBox.getSelectedId() > 1)
  107. {
  108. // Try to open the user's choice of camera..
  109. cameraDevice.reset (CameraDevice::openDevice (cameraSelectorComboBox.getSelectedId() - 2));
  110. // and if it worked, create a preview component for it..
  111. if (cameraDevice.get() != nullptr)
  112. {
  113. cameraPreviewComp.reset (cameraDevice->createViewerComponent());
  114. addAndMakeVisible (cameraPreviewComp.get());
  115. }
  116. }
  117. snapshotButton .setEnabled (cameraDevice.get() != nullptr);
  118. recordMovieButton.setEnabled (cameraDevice.get() != nullptr);
  119. resized();
  120. }
  121. void startRecording()
  122. {
  123. if (cameraDevice.get() != nullptr)
  124. {
  125. // The user has clicked the record movie button..
  126. if (! recordingMovie)
  127. {
  128. // Start recording to a file on the user's desktop..
  129. recordingMovie = true;
  130. auto file = File::getSpecialLocation (File::userDesktopDirectory)
  131. .getNonexistentChildFile ("JuceCameraDemo", CameraDevice::getFileExtension());
  132. cameraDevice->startRecordingToFile (file);
  133. recordMovieButton.setButtonText ("Stop Recording");
  134. }
  135. else
  136. {
  137. // Already recording, so stop...
  138. recordingMovie = false;
  139. cameraDevice->stopRecording();
  140. recordMovieButton.setButtonText ("Start recording (to a file on your desktop)");
  141. }
  142. }
  143. }
  144. void takeSnapshot()
  145. {
  146. // When the user clicks the snapshot button, we'll attach ourselves to
  147. // the camera as a listener, and wait for an image to arrive...
  148. cameraDevice->addListener (this);
  149. }
  150. // This is called by the camera device when a new image arrives
  151. void imageReceived (const Image& image) override
  152. {
  153. // In this app we just want to take one image, so as soon as this happens,
  154. // we'll unregister ourselves as a listener.
  155. if (cameraDevice.get() != nullptr)
  156. cameraDevice->removeListener (this);
  157. // This callback won't be on the message thread, so to get the image back to
  158. // the message thread, we'll stash a pointer to it (which is reference-counted in
  159. // a thead-safe way), and trigger an async callback which will then display the
  160. // new image..
  161. incomingImage = image;
  162. triggerAsyncUpdate();
  163. }
  164. Image incomingImage;
  165. void handleAsyncUpdate() override
  166. {
  167. if (incomingImage.isValid())
  168. lastSnapshot.setImage (incomingImage);
  169. }
  170. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CameraDemo)
  171. };