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.

192 lines
6.7KB

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