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.

191 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucedemo_headers.h"
  18. #if JUCE_USE_CAMERA
  19. //==============================================================================
  20. class CameraDemo : public Component,
  21. public ComboBoxListener,
  22. public ButtonListener,
  23. public CameraDevice::Listener
  24. {
  25. public:
  26. //==============================================================================
  27. CameraDemo()
  28. : cameraSelectorComboBox ("Camera"),
  29. snapshotButton ("Take a snapshot"),
  30. recordMovieButton ("Record a movie file (to your desktop)..."),
  31. recordingMovie (false)
  32. {
  33. setName ("Camera");
  34. addAndMakeVisible (&cameraSelectorComboBox);
  35. createListOfCameras();
  36. cameraSelectorComboBox.setSelectedId (1);
  37. cameraSelectorComboBox.addListener (this);
  38. addAndMakeVisible (&snapshotButton);
  39. snapshotButton.addListener (this);
  40. snapshotButton.setEnabled (false);
  41. addAndMakeVisible (&recordMovieButton);
  42. recordMovieButton.addListener (this);
  43. recordMovieButton.setEnabled (false);
  44. cameraSelectorComboBox.setSelectedId (2);
  45. }
  46. ~CameraDemo()
  47. {
  48. }
  49. void paint (Graphics& g)
  50. {
  51. g.drawImageWithin (lastSnapshot,
  52. getWidth() / 2 + 10, 40,
  53. getWidth() / 2 - 20, getHeight() - 50,
  54. RectanglePlacement::centred, false);
  55. }
  56. void resized()
  57. {
  58. cameraSelectorComboBox.setBounds (10, 4, 250, 24);
  59. snapshotButton.changeWidthToFitText (24);
  60. snapshotButton.setTopLeftPosition (cameraSelectorComboBox.getRight() + 20, 4);
  61. recordMovieButton.changeWidthToFitText (24);
  62. recordMovieButton.setTopLeftPosition (snapshotButton.getRight() + 20, 4);
  63. if (cameraPreviewComp != 0)
  64. cameraPreviewComp->setBounds (10, 40, getWidth() / 2 - 20, getHeight() - 50);
  65. }
  66. void comboBoxChanged (ComboBox*)
  67. {
  68. // This is called when the user chooses a camera from the drop-down list.
  69. cameraDevice = 0;
  70. cameraPreviewComp = 0;
  71. recordingMovie = false;
  72. if (cameraSelectorComboBox.getSelectedId() > 1)
  73. {
  74. // Try to open the user's choice of camera..
  75. cameraDevice = CameraDevice::openDevice (cameraSelectorComboBox.getSelectedId() - 2);
  76. // and if it worked, create a preview component for it..
  77. if (cameraDevice != 0)
  78. addAndMakeVisible (cameraPreviewComp = cameraDevice->createViewerComponent());
  79. }
  80. snapshotButton.setEnabled (cameraDevice != 0);
  81. recordMovieButton.setEnabled (cameraDevice != 0);
  82. resized();
  83. }
  84. void createListOfCameras()
  85. {
  86. cameraSelectorComboBox.clear();
  87. cameraSelectorComboBox.addItem ("No camera", 1);
  88. cameraSelectorComboBox.addSeparator();
  89. StringArray cameras = CameraDevice::getAvailableDevices();
  90. for (int i = 0; i < cameras.size(); ++i)
  91. cameraSelectorComboBox.addItem (cameras[i], i + 2);
  92. }
  93. void buttonClicked (Button* b)
  94. {
  95. if (cameraDevice != 0)
  96. {
  97. if (b == &recordMovieButton)
  98. {
  99. // The user has clicked the record movie button..
  100. if (! recordingMovie)
  101. {
  102. // Start recording to a file on the user's desktop..
  103. recordingMovie = true;
  104. File file (File::getSpecialLocation (File::userDesktopDirectory)
  105. .getNonexistentChildFile ("JuceCameraDemo",
  106. CameraDevice::getFileExtension()));
  107. cameraDevice->startRecordingToFile (file);
  108. recordMovieButton.setButtonText ("Stop Recording");
  109. }
  110. else
  111. {
  112. // Already recording, so stop...
  113. recordingMovie = false;
  114. cameraDevice->stopRecording();
  115. recordMovieButton.setButtonText ("Start recording (to a file on your desktop)");
  116. }
  117. }
  118. else
  119. {
  120. // When the user clicks the snapshot button, we'll attach ourselves to
  121. // the camera as a listener, and wait for an image to arrive...
  122. cameraDevice->addListener (this);
  123. }
  124. }
  125. }
  126. // This is called by the camera device when a new image arrives
  127. void imageReceived (const Image& image)
  128. {
  129. // In this app we just want to take one image, so as soon as this happens,
  130. // we'll unregister ourselves as a listener.
  131. if (cameraDevice != 0)
  132. cameraDevice->removeListener (this);
  133. // This callback won't be on the message thread, so need to lock it before using
  134. // data that may already be in use..
  135. const MessageManagerLock mm;
  136. lastSnapshot = image;
  137. repaint();
  138. }
  139. private:
  140. //==============================================================================
  141. ScopedPointer<CameraDevice> cameraDevice;
  142. ScopedPointer<Component> cameraPreviewComp;
  143. Image lastSnapshot;
  144. ComboBox cameraSelectorComboBox;
  145. TextButton snapshotButton;
  146. TextButton recordMovieButton;
  147. bool recordingMovie;
  148. };
  149. //==============================================================================
  150. Component* createCameraDemo()
  151. {
  152. return new CameraDemo();
  153. }
  154. #endif