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.

195 lines
6.8KB

  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 ComboBox::Listener,
  23. public Button::Listener,
  24. public CameraDevice::Listener
  25. {
  26. public:
  27. //==============================================================================
  28. CameraDemo()
  29. {
  30. setName (T("Camera"));
  31. cameraDevice = 0;
  32. cameraPreviewComp = 0;
  33. recordingMovie = false;
  34. addAndMakeVisible (cameraSelectorComboBox = new ComboBox (T("Camera")));
  35. createListOfCameras();
  36. cameraSelectorComboBox->setSelectedId (1);
  37. cameraSelectorComboBox->addListener (this);
  38. addAndMakeVisible (snapshotButton = new TextButton (T("Take a snapshot")));
  39. snapshotButton->addButtonListener (this);
  40. snapshotButton->setEnabled (false);
  41. addAndMakeVisible (recordMovieButton = new TextButton (T("Record a movie file (to your desktop)...")));
  42. recordMovieButton->addButtonListener (this);
  43. recordMovieButton->setEnabled (false);
  44. cameraSelectorComboBox->setSelectedId (2);
  45. }
  46. ~CameraDemo()
  47. {
  48. deleteAllChildren();
  49. delete cameraDevice;
  50. }
  51. void paint (Graphics& g)
  52. {
  53. g.drawImageWithin (lastSnapshot,
  54. getWidth() / 2 + 10, 40,
  55. getWidth() / 2 - 20, getHeight() - 50,
  56. RectanglePlacement::centred, false);
  57. }
  58. void resized()
  59. {
  60. cameraSelectorComboBox->setBounds (10, 4, 250, 24);
  61. snapshotButton->changeWidthToFitText (24);
  62. snapshotButton->setTopLeftPosition (cameraSelectorComboBox->getRight() + 20, 4);
  63. recordMovieButton->changeWidthToFitText (24);
  64. recordMovieButton->setTopLeftPosition (snapshotButton->getRight() + 20, 4);
  65. if (cameraPreviewComp != 0)
  66. cameraPreviewComp->setBounds (10, 40, getWidth() / 2 - 20, getHeight() - 50);
  67. }
  68. void comboBoxChanged (ComboBox*)
  69. {
  70. // This is called when the user chooses a camera from the drop-down list.
  71. deleteAndZero (cameraDevice);
  72. deleteAndZero (cameraPreviewComp);
  73. recordingMovie = false;
  74. if (cameraSelectorComboBox->getSelectedId() > 1)
  75. {
  76. // Try to open the user's choice of camera..
  77. cameraDevice = CameraDevice::openDevice (cameraSelectorComboBox->getSelectedId() - 2);
  78. // and if it worked, create a preview component for it..
  79. if (cameraDevice != 0)
  80. addAndMakeVisible (cameraPreviewComp = cameraDevice->createViewerComponent());
  81. }
  82. snapshotButton->setEnabled (cameraDevice != 0);
  83. recordMovieButton->setEnabled (cameraDevice != 0);
  84. resized();
  85. }
  86. void createListOfCameras()
  87. {
  88. cameraSelectorComboBox->clear();
  89. cameraSelectorComboBox->addItem ("No camera", 1);
  90. cameraSelectorComboBox->addSeparator();
  91. StringArray cameras = CameraDevice::getAvailableDevices();
  92. for (int i = 0; i < cameras.size(); ++i)
  93. cameraSelectorComboBox->addItem (cameras[i], i + 2);
  94. }
  95. void buttonClicked (Button* b)
  96. {
  97. if (cameraDevice != 0)
  98. {
  99. if (b == recordMovieButton)
  100. {
  101. // The user has clicked the record movie button..
  102. if (! recordingMovie)
  103. {
  104. // Start recording to a file on the user's desktop..
  105. recordingMovie = true;
  106. File file (File::getSpecialLocation (File::userDesktopDirectory)
  107. .getNonexistentChildFile (T("JuceCameraDemo"),
  108. CameraDevice::getFileExtension()));
  109. cameraDevice->startRecordingToFile (file);
  110. recordMovieButton->setButtonText (T("Stop Recording"));
  111. }
  112. else
  113. {
  114. // Already recording, so stop...
  115. recordingMovie = false;
  116. cameraDevice->stopRecording();
  117. recordMovieButton->setButtonText (T("Start recording (to a file on your desktop)"));
  118. }
  119. }
  120. else
  121. {
  122. // When the user clicks the snapshot button, we'll attach ourselves to
  123. // the camera as a listener, and wait for an image to arrive...
  124. cameraDevice->addListener (this);
  125. }
  126. }
  127. }
  128. // This is called by the camera device when a new image arrives
  129. void imageReceived (const Image& image)
  130. {
  131. // In this app we just want to take one image, so as soon as this happens,
  132. // we'll unregister ourselves as a listener.
  133. if (cameraDevice != 0)
  134. cameraDevice->removeListener (this);
  135. // This callback won't be on the message thread, so need to lock it before using
  136. // data that may already be in use..
  137. const MessageManagerLock mm;
  138. lastSnapshot = image;
  139. repaint();
  140. }
  141. private:
  142. //==============================================================================
  143. CameraDevice* cameraDevice;
  144. ComboBox* cameraSelectorComboBox;
  145. TextButton* snapshotButton;
  146. TextButton* recordMovieButton;
  147. Component* cameraPreviewComp;
  148. bool recordingMovie;
  149. Image lastSnapshot;
  150. };
  151. //==============================================================================
  152. Component* createCameraDemo()
  153. {
  154. return new CameraDemo();
  155. }
  156. #endif